我有一个使用自定义适配器的Spinners ListView。当我运行我的代码时,一切正常,除非我在屏幕上有更多的Spinners。如果我向下滚动并显示最后一个微调器,我可以按预期使用它们但是当我向上滚动并且它们离开视图时,它们会重置到所选项目始终是第一个的位置,而不管Spinner中的哪个项目是选择。 。为什么是这样?我认为这与getView方法有关,但我不确定如何保存选择,以便在它再次返回视图时被选中?
public class CustomPLNViewAdapter extends BaseAdapter{
private static ArrayList<ArrayList<String>> partLotNumbersArrayList;
private static ArrayList<String> partNames;
private LayoutInflater mInflater;
private Context myContext;
public CustomPLNViewAdapter(Context context, ArrayList<ArrayList<String>> results, ArrayList<String> parts){
partLotNumbersArrayList = results;
partNames = parts;
mInflater = LayoutInflater.from(context);
myContext = context;
}
@Override
public int getCount() {
return partLotNumbersArrayList.size();
}
@Override
public Object getItem(int position) {
return partLotNumbersArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.view_assembly_parts, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.partName);
holder.spinner = (Spinner) convertView.findViewById(R.id.LotNumbers);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(partNames.get(position));
ArrayAdapter<String> adp1=new ArrayAdapter<String>(myContext, android.R.layout.simple_list_item_1, partLotNumbersArrayList.get(position));
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//set the adapter to the spinnner
holder.spinner.setAdapter(adp1);
//if there is only one other part besides "" then set that as default part
if(partLotNumbersArrayList.get(position).size() == 2){
holder.spinner.setSelection(1);
}
holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if(holder.spinner.getSelectedItem().toString() == ""){
Log.v("Spinner", "spinner value is blank");
ViewOrderlineAssemblyActivity.assembly.addPart(new Part((String)holder.txtName.getText(), ""));
}else{
ViewOrderlineAssemblyActivity.assembly.addPart(new Part((String)holder.txtName.getText(), holder.spinner.getSelectedItem().toString()));
Log.v("OnItemSelected", (String)holder.txtName.getText() + " : "+ holder.spinner.getSelectedItem().toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
ViewOrderlineAssemblyActivity.assembly.addPart(new Part((String)holder.txtName.getText(), ""));
Log.v("OnNothingSelected", (String)holder.txtName.getText());
}
});
return convertView;
}
static class ViewHolder {
TextView txtName;
Spinner spinner;
}
}
答案 0 :(得分:0)
ListView
中的视图旨在被回收以限制它们占用的内存量。创建的视图数等于屏幕上可见的列表项数。因此,当一个列表项被推离屏幕并出现一个新项目时,不再可见的列表项将被回收用于刚刚滚动到视图中的项目。
您需要保存微调器的状态(如果用户已更改)并在getView()
方法中恢复该状态。您可以将状态保存在一个简单的对象数组中,这些对象包含微调器的状态(选定的值等)。然后在getView()
中,您可以检查是否为该微调器保存了任何状态(在列表中的位置编入索引),如果它在那里,则从保存状态设置其值。