Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Listview's onItemClick() event not getting called

Following is my code :

public class TestActivity extends ListActivity {

    private Cursor cursor;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        fillData(); 
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
        Object o = this.getListAdapter().getItem(position);
        String keyword = o.toString();
        Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
                .show();
    }

    private void fillData() {
        TermsData_DataHelper termsDataHelper = new TermsData_DataHelper(
                TestActivity.this);
        cursor = termsDataHelper.fetchAllCategories();
        startManagingCursor(cursor);

        String[] names = new String[] { "name" };
        int[] to = new int[] { R.id.label };

        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter categories = new SimpleCursorAdapter(this,
                R.layout.terms_row, cursor, names, to);
        setListAdapter(categories);
    }
}

When I click on a row in the listview, I don't see any Toast. Can anybody tell me where am I making a mistake.

Thanks in advance.

like image 335
Mahendra Liya Avatar asked Jan 19 '23 16:01

Mahendra Liya


1 Answers

I was having a similar problem and found that after removing android:clickable="true" from my list items, the onListItemClick() was being triggered just fine. I hope this helps.

like image 101
Blaker Avatar answered Jan 29 '23 06:01

Blaker