正如我在标题中提到的,我在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。我不知道如何在单击List 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
}
答案 0 :(得分:1)
您需要获取第二个ListView的引用,并且需要将onItemClickListener附加到它。
因为ListFragment会为标识为@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);
}
});
答案 1 :(得分:0)
您需要将ListFragment
更改为Fragment
,然后分别引用两个ListView
并分配他们的听众:
ListView list = (ListView)findViewById(R.id.list);
ListView listCompany = (ListView)findViewById(R.id.listCompany);
list.setAdapter(...)
list2.listCompany(...)
每个ListView
ListFragment
一个ListViews
,一个Fragment
多个ListViews
,但ListFragment