我有一个带有图像的自定义ListView,两个TextView和一个Spinner。我的问题是当我滚动ListView时,Spinner失去了它的值。如何在每个单元格中保持Spinner值?
下面的是我的适配器的代码部分:
public View getView(int position, View contentView, ViewGroup parent) {
ViewHolder holder;
View vi = contentView;
item = items.get(position);
if (vi == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)
activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.cell_products, null);
holder.image = (ImageView) vi.findViewById(R.id.imgCellProducts);
holder.tvTextCellProducts = (TextView) vi.findViewById(R.id.tvTextCellProducts);
holder.tvPreciosProducts = (TextView) vi.findViewById(R.id.tvPreciosProductos);
holder.spCellProducts = (Spinner) vi.findViewById(R.id.spCellProducts);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
holder.spCellProducts.setTag(position);
switch (item.getPossibleSize()) {
case 0:
holder.spCellProducts.setVisibility(View.INVISIBLE);
break;
case 1:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(activity.getApplicationContext(),
R.array.sizeSmallMedium, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//holder.spCellProducts.setDescendantFocusability(parent.FOCUS_BEFORE_DESCENDANTS);
holder.spCellProducts.setAdapter(adapter);
break;
case 2:
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(activity.getApplicationContext(),
R.array.sizeSmallMediumBig, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.spCellProducts.setAdapter(adapter2);
break;
}
holder.spCellProducts.setOnItemSelectedListener(this);
if (position == ((Integer) holder.spCellProducts.getTag())) {
holder.spCellProducts.setSelection(item.getSize());
}
holder.tvTextCellProducts.setText(item.getName());
holder.tvPreciosProducts.setText(String.valueOf(item.getPrice()));
switch (item.getIcon()) {
case 0:
break;
case 1:
holder.image.setImageDrawable(vi.getResources().getDrawable(android.R.drawable.alert_dark_frame));
break;
case 2:
holder.image.setImageDrawable(vi.getResources().getDrawable(android.R.drawable.btn_dropdown));
}
return vi;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long arg3) {
items.get(((Integer) parent.getTag())).setSize(position);
((TextView) parent.getChildAt(0)).setTextColor(view.getResources().getColor(R.color.azul_oscuro));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
编辑:已解决