Search code examples
androidlistviewhighlightsimplecursoradapterfocusable

ListView Android does not allow selector to work nor does it allows selections


Okay I know this may sound like any other issues regarding the ListView and ListActivity issues but this is not, I am going to go crazy as I have in the past week gone on and on searching all post to find any solution to this that may help me. Already tried ListView item background via custom selector and GetView Vs. BindView in a custom CursorAdapter? every other possible blogs sites...

Okay the issue, My listView for one 1 will not allows selection/fire events and 2. will not change the row highlight when an item is clicked. I have a standard TestActivity extending ListActivity and I have implemented a Custom TestCursorAdapter extends SimpleCursorAdapter

Now in XML side of things I have on test_list_view.xml and second one row_view.xml Now I followed the above mentions answers and implement selector as row_selector.xml. But the issue is for one the rows does not accept any click or focus events. I implemented a basic toast to indicate if there is anything happening

@Override
protected void onListItemClick(ListView l, View v , int postion, long idontnow){
    Toast.makeText(getApplicationContext(), "Hello World",  Toast.LENGTH_SHORT ).show();
}

part of test_list_view.xml

<ListView android:id="@android:id/list" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawSelectorOnTop="true" 
    android:cacheColorHint="#00000000"
    android:focusable="true" 
    android:listSelector="@color/row_selector" 
    android:background="@color/Transparent"/>

part of selector.xml (basically copied and pasted from one of the above mentioned links)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" 
        android:drawable="@color/Transparent" />
    <item 
        android:state_focused="true"
        android:state_enabled="false"
        android:state_pressed="true" 
        android:drawable="@color/green" />
    <item 
        android:state_focused="true"
        android:state_enabled="false"      
        android:drawable="@color/green" />
    <item 
        android:state_focused="true"
        android:state_pressed="true" 
        android:drawable="@color/blue" />
    <item 
        android:state_focused="false"
        android:state_pressed="true" 
        android:drawable="@color/blue" />
    <item 
        android:state_focused="true" 
        android:drawable="@color/green" />
    <item 
        android:state_selected="true" 
        android:drawable="@color/blue" />
    <item 
        android:drawable="@color/Transparent" />
</selector>

I followed all options that I am aware of and still nothing. The only major difference is guys I am not using getView instead I am using bindView.

Any help guys will be highly appreciated.

thanks in advance.

Sanj


Solution

  • I suggest that you remove the android:focusable="true" from the ListView.

    Additionally, you can use the selector as the background color of the items that are displayed in the ListView. Also be sure that those elements are not calling onClick or onTouch or something like that, and let the ListView handle the click events.

    I hope this helps

    EDIT:

    for your problem with selecting the item use

    lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                yourMethod();
            }
        });