Mahendra Liya
Mahendra Liya

Reputation: 13218

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.

Upvotes: 4

Views: 5694

Answers (4)

moberme
moberme

Reputation: 689

I had this problem also. I solved it by Removing this from my TextView in the the layout XML.

    android:textIsSelectable="true"

Upvotes: 1

Korniltsev Anatoly
Korniltsev Anatoly

Reputation: 3686

The solution is to override protected void onListItemClick (ListView l, View v, int position, long id) method.

Upvotes: 2

Blaker
Blaker

Reputation: 819

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.

Upvotes: 4

letroll
letroll

Reputation: 1068

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    String mString = parent.getItemAtPosition(position).toString();
    //will give you the text of current line or
    int i = parent.getItemAtPosition(position);
    //if you want just position number
}

Upvotes: 2

Related Questions