Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel Android ListView item from being activated/highlighted in ListView's setOnItemClickListener?

In one of my Activities, I have multiple ListView controls. Say List 1, List 2 & List 3. On List 1 item's click, i load data for List 2 & List 3 from a webservice. And the clicked item's background is highlighted. I achieved this through following selector.

<item android:drawable="@drawable/item_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/item_focused" android:state_focused="false" 
    android:state_pressed="false" 
    android:state_selected="true"/>
<item android:state_activated="true" android:drawable="@drawable/item_selected"/>

As you can see, i have specified the android:state_activated="true" to change the BG of the clicked/tapped item. This works well.

Actually i want to prevent the clicked item from being selected/highlighted if data for some other item is being loaded ... like

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

                // load data if no other call to web service is in progress
                if (_isNotLoadingData) {
                     loadList2AndList3DataUsingWebService();
                } else {
                    //Stop This Item From selected/highlighted
                    //BUT none of the following work. .. Item is always Selected
                    view.setSelected(false);
                    //view.setActivated(false);
                    //view.setEnabled(false);
                }
            }
        });

Above code stops multiple calls to be directed to our web service but i am unable to stop the item from being highlighted/activated if the web service is not called for that ListView item.

How can i acieve this?

EDIT on 8:08 pm 19 Mar, 2012: No proper reply yet ... i am stuck & waiting for any help

like image 382
Aamir Avatar asked Dec 27 '22 04:12

Aamir


1 Answers

I actually encountered the same issue a few days ago. The solution is not to set view.setItemChecked(false), but instead in the .onItemClick()-method, call your listview and set listview.setItemChecked(position, false). This solved it for me.

Example:

listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view,
            int position, long arg3) {

        // load data if no other call to web service is in progress
        if (_isNotLoadingData) {
             loadList2AndList3DataUsingWebService();

        } else {
            ListView lv = (ListView) adapterView;
            lv.setItemChecked(position, false);
        }
    }
});
like image 105
Reinier Avatar answered Jan 13 '23 15:01

Reinier