我想创建一个视图,我可以从列表视图中选择多个项目,并且并排更改所选列表项的颜色并将该项目保存到我的数组列表中。我的列表如下所示:
但是当我用来滚动它时..它显示我选择了另外 1 个项目,即使我没有像这样选择它:
但我希望只有那个列表项颜色应该改变我将点击的位置......
我将代码用作:
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;
}
}
在 OnCreate() 方法中,
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);
});