1

I am unable to get Toast message as onItemClick is never called. Log-cat doesn't show any error. Please check out my code and correct me, if I am going wrong anywhere. I have used array adapter.

public class Open extends ListActivity {
    DbAdapter mDb = new DbAdapter(this);

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        mDb.close();
        super.onDestroy();
    }

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

        ListView listContent = (ListView) findViewById(android.R.id.list);

        mDb.open();
        Cursor cursor =mDb.fetchAllPrograms();
        startManagingCursor(cursor);

        String[] from = new String[] { DbAdapter.KEY_TITLE };
        int[] to = new int[] { R.id.textViewName };

        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, 
    R.layout.row, cursor, from, to);
        listContent.setAdapter(cursorAdapter);

        //Onclick ListView setlistener
        listContent.setTextFilterEnabled(true);
        listContent=getListView();


            listContent.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View 

    view,int position, long id) {
                    Toast.makeText(getApplicationContext(),
   "Working!", 
                    Toast.LENGTH_LONG).show();    
                }
            });

    }
}

Row.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:id="@+id/textViewName"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginTop="10dp"
     android:layout_marginBottom="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:textColor="#0000FF"
    android:textIsSelectable="true"
     />
Neha Somani
  • 102
  • 2
  • 11

2 Answers2

3

I was running into a similar issue. Not only was my onItemClick method being ignored, but also my List Selector wasn't working (android:listSelector.)

Turns out it was the android:textIsSelectable attribute on my TextView row elements. Try setting it to false.

Jargo
  • 86
  • 3
  • what should be the value for listSelector? – Neha Somani Mar 08 '13 at 04:36
  • did you asking use of listSelector? if yes then answer is [here](http://stackoverflow.com/questions/4365893/how-to-change-color-of-android-list-item-on-click-or-selection) and [here](http://stackoverflow.com/questions/2562051/listview-item-background-via-custom-selector) – Gunaseelan Mar 08 '13 at 04:46
  • thanks for your helpful answer. Except colors, what could be the value for listSelector? – Neha Somani Mar 08 '13 at 04:51
  • The listSelector attribute isn't required for onItemClick to work, but it is useful if you want your clicked item to stay highlighted. – Jargo Mar 08 '13 at 04:59
  • The listSelector attribute is set on ListView XML elements in your layouts, and points to an XML file in your res/drawables folder. The one I used looks like this: – Jargo Mar 08 '13 at 05:07
1

In my case the issue was in android:longClickable="true". After removing this line from xml click listener started to work again.

gorodezkiy
  • 3,299
  • 2
  • 34
  • 42