Note that there are some explanatory texts on larger screens.

plurals
  1. POChange background of multiple selected item in android custom listview
    primarykey
    data
    text
    <p>I have a custom listview in my app in which I want to implement deleting of multiple elements in using actionmode . A person can select multiple items by activating action mode using long press on an item like happens in watsapp. I am able to select multiple items but the problem which I am facing is while changing the background of selected list item. I tried activate and selected mode but they are changing background of random elements in my list item. Scrolling over list changing the background of random items and they are changing on each scroll. Weird thing i observed.</p> <p>I tried changing background both programatically or using xml but same thing is happening in both the cases.</p> <p>The code of my drawable file which i had applied to top element of my list view items view</p> <pre><code>&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_pressed="true" android:drawable="@android:color/transparent"&gt;&lt;/item&gt; &lt;item android:state_selected="true" android:drawable="@android:color/transparent"&gt; &lt;/item&gt; &lt;item android:state_focused="true" android:drawable="@android:color/transparent"&gt;&lt;/item&gt; &lt;item android:state_activated="true" android:drawable="@color/holored"&gt;&lt;/item&gt; &lt;item android:drawable="@drawable/list_view_item_shadow"&gt;&lt;/item&gt; &lt;/selector&gt; </code></pre> <p>Long Click Listener in List View</p> <pre><code>detailsListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView&lt;?&gt; adapterView, View view, int position, long id) { view.setActivated(true); onListItemSelect(position, view); return true; } }); private void onListItemSelect(int position,View view) { trasactionDetailAdapter.toggleSelection(position,view); } </code></pre> <p>I am using SparseBooleanArray to check the item selection, mSelectedItemsIds is my object of SparseBooleanArray.</p> <p>Inside my adapter :</p> <pre><code>public void toggleSelection(int position,View view) { selectView(position, !mSelectedItemsIds.get(position), view); } public void selectView(int position, boolean value,View view) { changeBackgroundColor(view,value); notifyDataSetChanged(); } private void changeBackgroundColor(View view,boolean hasSelectedItem){ if(hasSelectedItem){ int color=context.getResources().getColor(R.color.holoblue); view.setBackgroundColor(color); }else{ view.setBackgroundResource(R.drawable.listview_item_backgroundstate); } } </code></pre> <p>But even I am long pressing one item in list this code is changing the background color of multiple random items in listview, Am I paassing the wrong view to change the background ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload