Listview Select And Unselect Item Android
I am able to select items and adding to sharepreference. But once I click on selected item Its not set as unselected. When I select any tag it should add in Sharepreference and whe
Solution 1:
In this case I think you need to manage it from your ArrayAdapter
class.
There you have to remember last clicked item view and position.
Basic idea is onListItemClick
pass the clicked position and itemview to adapter there
we will set it's background with the color you like and changed the last selected to view
background color to default color.
publicclassSampleAdapterextendsArrayAdapter<Object> {
privateintmSelection=0;
publicSampleAdapter(Context context, int resource, int textViewResourceId,
List<Object> objects) {
super(context, resource, textViewResourceId, objects);
}
publicvoidsetSelection(int mSelection, View selectedItemView) {
this.mSelection = mSelection;
if (selectedItemView!= null && lastSelectedRow != null
&& selectedItemView!= lastSelectedRow) {
lastSelectedRow
.setBackgroundResource(R.drawable.bg_normal);
selectedItemView
.setBackgroundResource(R.drawable.bg_selected);
}
this.lastSelectedRow = selectedItemView;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
//Usual code logic here....if (mSelection == position) {
mViewHolder.mRootView
.setBackgroundResource(R.drawable.bg_selected);
lastSelectedRow = mViewHolder.mRootView;
} else {
mViewHolder.mRootView
.setBackgroundResource(R.drawable.bg_normal);
}
return view;
}
privatestaticclassViewHolder {
TextView name;
View mRootView;
}
}
On List Item click you need to pass clicked item and position to adapter.
publicvoidonItemClick(AdapterView<?> arg0, View listItemView,
int position, long id) {
if(myAdapter != null )
{
myAdapter.setSelection(position,listItemView);
}
}
if you want to call this set selection from other point you can call it like this.
myAdapter.setSelection(position,null);
mListViwe.setSelectionFromTop(position, 0);
Post a Comment for "Listview Select And Unselect Item Android"