gpt4 book ai didi

android - 2 ListView在一个ListFragment中只有一个onListItemClick

转载 作者:行者123 更新时间:2023-11-30 03:37:05 25 4
gpt4 key购买 nike

正如我在标题中提到的,我在一个 ListFragment 中有 2 个 ListView(我们称它们为 A 和 B)并且只有一个 onListItemClick。

当我点击列表 A 的一行时,我的 onListItemClick 被调用正常。但是当我点击列表 B 的一行时,没有任何反应。

这是我的:

<!-- LIST A -->
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

<!-- Separator -->
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>

<!-- LIST B -->
<ListView
android:id="@+id/listCompany"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

这是我的 ListFragment:

public class ClientsActivity extends ListFragment {
OnClientSelectedListener mClientListener;


public interface OnClientSelectedListener {
public void onEmployeeSelected(int id);
public void onCompanySelected(int id);
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mClientListener = (OnClientSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " you must implement OnClientSelectedListener ");
}
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

Log.e("onListItemClick","called with " + position + " : "+l.getId() + " and " + android.R.id.list);
int id = position;
if (l.getId() == android.R.id.list) {
mClienteListener.onEmployeeSelected(id);
} else if (l.getId() == R.id.listCompany) {
mClienteListener.onCompanySelected(id);
}
}

然后在我的主要 FragmentActivity 中实现接口(interface) OnClientSelectedListener。我认为这必须与我的 ListViews 中的 android:id 一起使用。我不知道如何在单击列表 B 的一行时调用 onListItemClick。我们将不胜感激。

解决方案:

在我的 ListFragment 的 onCreateView 中,我有:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.activity_clientes, container, false);

//LIST A
listEmploees(view);

//LIST B
listCompanies(view);
...
}

private void listCompanies(View view){
CompanyqliteDao companyDao = new CompanyqliteDao ();
//I get all the companies from DB
mCursorCompany = companyDao.listCompany(getActivity());

if(mCursorCompany.getCount()>0){
String[] from = new String[] { "name", "phone"};
int[] to = new int[] { R.id.textViewNameIzq, R.id.textViewNameCent };
ListView lvCompanies = (ListView) view.findViewById (R.id.listCompany);

//I have a custom adapter
ListClientCursorAdapter notes = new ListClientsCursorAdapter(contexto, R.layout.activity_fila_cliente, mCursorCompany, from, to, 0);
lvCompanies .setAdapter(notes);


lvCompanies.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
onListItemClick(lvCompanies,view,position,arg);
}
});

}
}


private void listEmploees(View view){
EmployeeSqliteDao employeeDao = new EmployeeSqliteDao();
//Get the list from DB
mCursorEmployee = employeeDao.listEmployees(getActivity());

if(mCursorEmployee.getCount()>0){
String[] from = new String[] { "name", "phone"};
int[] to = new int[] { R.id.textViewNameIzq, R.id.textViewNameCent};
ListView lvEmployees = (ListView) view.findViewById (android.R.id.list);

ListClientsCursorAdapter notes = new ListClientesCursorAdapter(context, R.layout.activity_fila_cliente, mCursorEmployee, from, to, 0);
lvEmployees.setAdapter(notes);
//NOTE THAT I DONT HAVE TO SET A LISTENER FOR THIS ONE, AUTOMATICALLY
// THE onListItemClick IS CALLED

}

最佳答案

您需要获取第二个 ListView 的引用,并且需要将 onItemClickListener 附加到它。

因为 ListFragment 将为具有 id @id/android:list 的 listView 附加 onItemClckListener

尝试在onCreateView中获取引用

编辑:

  ListView list2=view.findViewById(R.id.listCompany);
list2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
onListItemClick(adapter,view,position,arg);
}
});

关于android - 2 ListView在一个ListFragment中只有一个onListItemClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16498893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com