4

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.

Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
  • 1
    Someone else will probably turn up to solve this soon, but just mentioning: placing a breakpoint in `onListItemClick()` and running in debug mode would allow you to make sure of whether the method is being called at all, which might narrow down the list of possible problems. – Sven Viking May 25 '11 at 17:27
  • @Viking: I added a breakpoint at `onListItemClick()` and I found that the control does not reach there when I click the listview row..Can you tell me what could be the probable mistake? – Mahendra Liya May 25 '11 at 17:35
  • I found the solution for this problem here: http://stackoverflow.com/questions/5551042/onitemclicklistener-not-working-in-listview-android – Oded Regev Mar 01 '14 at 15:27

4 Answers4

4

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.

Blaker
  • 769
  • 1
  • 8
  • 12
2

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

Korniltsev Anatoly
  • 3,676
  • 2
  • 26
  • 37
2
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
}
Abizern
  • 146,289
  • 39
  • 203
  • 257
letroll
  • 1,059
  • 1
  • 14
  • 36
1

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

    android:textIsSelectable="true"
moberme
  • 669
  • 7
  • 13