我在自定义适配器列表视图上添加了两个微调器。一切都很好但是当我将新项目添加到列表视图时,前一项目的微调器的值将转换为新项目。在滚动列表视图时,旋转器的值也会旋转。请帮助。
public class CustomAdapter extends BaseAdapter {
private ArrayList<MyMessageDetails> _data;
Context _c;
ProductsItemViewHolder holder;
CustomAdapter (ArrayList<MyMessageDetails> data, Context c){
_data = data;
_c = c;
}
public int getCount() {
// TODO Auto-generated method stub
return _data.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return _data.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
final MyMessageDetails msg = _data.get(position);
if (v == null)
{
LayoutInflater vi = (LayoutInflater)_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listview_item_row, null);
holder = new ProductsItemViewHolder();
holder.image = (ImageView) v.findViewById(R.id.icon);
holder.fromView = (TextView) v.findViewById(R.id.textView1);
holder.spinnersizes = (Spinner) v.findViewById(R.id.spinner1);
holder.spinnercopies = (Spinner) v.findViewById(R.id.spinner2);
String Photo_copies[];
Photo_copies=new String[100];
int x = 1;
while( x < 101 ) {
if(x == 1){
Photo_copies[x-1]= String.valueOf(x) + " Copy";
}else{
Photo_copies[x-1]= String.valueOf(x) + " Copies";
}
x++;
}
String array_spinner[];
array_spinner=new String[5];
array_spinner[0]="4x6|Plastic|RS 20";
array_spinner[1]="option 2";
array_spinner[2]="option 3";
array_spinner[3]="option 4";
array_spinner[4]="option 5";
ArrayAdapter adapter = new ArrayAdapter(_c,android.R.layout.simple_spinner_item, array_spinner);
ArrayAdapter adapter2 = new ArrayAdapter(_c,android.R.layout.simple_spinner_item, Photo_copies);
holder.spinnersizes.setAdapter(adapter);
holder.spinnercopies.setAdapter(adapter2);
holder.spinnercopies.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {
String sizes = holder.spinnersizes.getSelectedItem().toString();
holder.spinnersizes.setTag(position);
String copies = holder.spinnercopies.getSelectedItem().toString();
String mycopies = copies;
String myprice = sizes;
myprice = myprice.substring(myprice.lastIndexOf(" ") + 1);
mycopies = mycopies.substring(0, mycopies.lastIndexOf(" "));
int finalprice = Integer.parseInt(myprice) * Integer.parseInt(mycopies);
holder.fromView.setText(holder.image.getTag().toString() + " Copies:" + mycopies + " Price:" + finalprice);
msg.setCopies(mycopies);
msg.setSize(String.valueOf(finalprice));
// lab_gallery.Calculate_Bill();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
holder.spinnersizes.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {
// your code here
String sizes = holder.spinnersizes.getSelectedItem().toString();
holder.spinnersizes.setTag(position);
String copies = holder.spinnercopies.getSelectedItem().toString();
String mycopies = copies;
String myprice = sizes;
myprice = myprice.substring(myprice.lastIndexOf(" ") + 1);
mycopies = mycopies.substring(0, mycopies.lastIndexOf(" "));
int finalprice = Integer.parseInt(myprice) * Integer.parseInt(mycopies);
holder.fromView.setText(holder.image.getTag().toString() + " Copies:" + mycopies + " Price:" + finalprice);
msg.setCopies(mycopies);
msg.setSize(String.valueOf(finalprice));
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
holder.image.setImageBitmap(msg.getIcon());
holder.fromView.setTag(msg.getUrl());
holder.fromView.setText(msg.getName());
holder.image.setTag(msg.getName());
msg.setCopies("1");
msg.setSize("20");
v.setTag(holder);
} else {
holder = (ProductsItemViewHolder) v.getTag();
}
if (holder.spinnersizes.getTag() != null){
holder.spinnersizes.setSelection(Integer.parseInt(holder.spinnersizes.getTag().toString()));
}
//image.setScaleType(ScaleType.FIT_XY);
return v;
}
static class ProductsItemViewHolder {
ImageView image;
TextView fromView;
Spinner spinnersizes;
Spinner spinnercopies;
}
}
答案 0 :(得分:5)
这是因为视图回收。视图与最后一个状态一起回收。如果你的微调器有选择的东西,并且如果该视图被重用,它也将选择微调器的状态。您需要添加逻辑以保留Spinners的状态。您可以使用行位置和selectedIndex维护字典。
//班级字段
Map<Integer, Integer> myMap = new HashMap<Integer, Integer>();
if (myMap.containsKey(position)) {
spinner.setSelection(myMap.get(position));
}
在微调器中选择项目时保存状态
@Override
public void onItemSelected(AdapterView<?> parentView, View v, int selectedIndex, long id) {
//...
myMap.put(position, selectedIndex);
//...
}