I want to make a view where I can select multiple items from listview and also side by side am changing the color of selected list item and saving that item into my arraylist..My list is shown as below as:
But when I used to scroll it.. It is showing me 1 more item selected, even I am not selecting it like:
But I want that only that list item color should change where I will click...
I am using the code as:
private class ItemsAdapter extends ArrayAdapter<String> {
List<String> items;
Context context;
private LayoutInflater inflater;
public ItemsAdapter(Context context, List<String> part_array_list) {
super( context, R.layout.part_list, R.id.label,part_array_list );
inflater = LayoutInflater.from(context) ;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView ;
String item = (String) this.getItem( position );
if ( convertView == null ) {
convertView = inflater.inflate(R.layout.part_list, null);
// Find the child views.
textView = (TextView) convertView.findViewById( R.id.label );
// Optimization: Tag the row with it's child views, so we don't have to
// call findViewById() later when we reuse the row.
convertView.setTag( new ListViewHolder(textView) );
}
// Reuse existing row view
else {
// Because we use a ViewHolder, we avoid having to call findViewById().
ListViewHolder viewHolder = (ListViewHolder) convertView.getTag();
textView = viewHolder.getTextView() ;
}
textView.setText( part_array_list.get(position) );
return convertView;
}
}
/** Holds child views for one row. */
private class ListViewHolder {
private TextView textView ;
public ListViewHolder() {}
public ListViewHolder( TextView textView ) {
this.textView = textView ;
}
public TextView getTextView() {
return textView;
}
public void setTextView(TextView textView) {
this.textView = textView;
}
}
and in OnCreate() method,
final ArrayAdapter<String> part_list_adapter=new ItemsAdapter(AssetSearch.this, part_array_list);
//PartNumber_List.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
PartNumber_List.setAdapter(part_list_adapter);
PartNumber_List.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
ListViewHolder viewHolder = (ListViewHolder) v.getTag();
viewHolder.getTextView().setBackgroundColor(R.color.result_image_border);
String item=(String) part_list_adapter.getItem((int) id);
});