Note that there are some explanatory texts on larger screens.

plurals
  1. PO2 ListView in a ListFragment only one onListItemClick
    text
    copied!<p>As I mentioned in the title, I have 2 ListView (lets call them A and B) in a ListFragment and only one onListItemClick.</p> <p>When I click on a row of List A, my onListItemClick is called fine. But when I click on a row of List B, nothing happens.</p> <p>This is what I have:</p> <pre><code>&lt;!-- LIST A --&gt; &lt;ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="wrap_content" &gt; &lt;/ListView&gt; &lt;!-- Separator --&gt; &lt;View android:layout_width="fill_parent" android:layout_height="1dp" android:background="@android:color/darker_gray"/&gt; &lt;!-- LIST B --&gt; &lt;ListView android:id="@+id/listCompany" android:layout_width="match_parent" android:layout_height="wrap_content" &gt; &lt;/ListView&gt; </code></pre> <p>This is my ListFragment:</p> <pre><code>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); } } </code></pre> <p>And then in my main FragmentActivity I implement the interface OnClientSelectedListener. I think this has to be with android:id in my ListViews. I dont know how to invoke onListItemClick when a row of List B is clicked. Any help will be apreciated.</p> <p><strong>SOLUTION:</strong> </p> <p>In the onCreateView of my ListFragment I have:</p> <pre><code> 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()&gt;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&lt;?&gt; 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()&gt;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 } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload