1

I am working on a groceries list app to learn android, but I cannot seem to figure out how to get the items in the ListView to trigger the onClick event in the ItemClickListener that I set on the listview. The method renderList() is called from the onResume() method, which grabs the list of groceries from the database and fills the ListView. Why does this implementation not work; the toast does not display when I click the list_item?

The list_item is a CheckBox view.

@Override
protected void onResume() {
  super.onResume();
  renderList();
}

protected void renderList(){

    try {
        dbHelper = new DbHelper(getApplicationContext());
        db = dbHelper.getReadableDatabase();
        cursor = db.query(DbHelper.TABLE, null, null, null, null, null, DbHelper.C_ID + " DESC");
        startManagingCursor(cursor);
        adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, FROM, TO);

        groceriesList = (ListView)findViewById(R.id.listView1);
        groceriesList.setAdapter(adapter);
        groceriesList.setOnItemClickListener(clickListen);


     } catch (Exception e) {
         Log.d(TAG, "RenderList Error: ",e);
     }
}

private OnItemClickListener clickListen = new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "pos: "+position, Toast.LENGTH_SHORT).show();
    }   

};
Atomix
  • 2,440
  • 8
  • 36
  • 48

2 Answers2

3

My guess is that you have items in your list rows that are focusable. I would look into setting this to false or in your binding of items, just set their xml attributes to android:focusable="false"

runor49
  • 3,870
  • 1
  • 21
  • 18
  • Technically I think you are right, I had to switch to textView to get it to work. I made a linearLayout with a textView and an image of an unchecked checkbox, and when it gets clicked I will just change the image to a checked checkBox. – Atomix May 18 '11 at 13:56
  • what happens if you use ? – runor49 May 18 '11 at 23:20
  • I ended up using a custom CursorAdapter class to populate the listView – Atomix May 19 '11 at 17:31
1

You probably want to use a ListActivity and then simply override the onListItemClick method. The other issue is probably what you are putting in the list view. See ListView with clickable/editable widget for an overview, but if you have any focusable items in the list view you'll have difficulties getting it to register clicks correctly.

Community
  • 1
  • 1
Femi
  • 64,273
  • 8
  • 118
  • 148
  • If I use a list activity, how do I add and header -and- have it be fixed in place at the top during scrolling? – Atomix May 17 '11 at 18:38