正如我在标题中提到的,我在 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 中实现接口 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
}