我发生了一件非常奇怪的事情。所以我有一个listview,它有两种不同类型的行。一种类型的行中有四个微调器。列表视图的所有内容都可以正常工作,除非列表中只有几个项目,否则微调器不会选择任何项目。起初我认为这是listview更新的一个问题,但是在进一步检查时,无论我选择哪个项目,OnItemSelectedListener的位置总是为0。我通过谷歌和stackoverflow进行了广泛的搜索,似乎没有什么能解决它。
我的listview适配器中的getView()方法的一部分
mAdapter = new MathSpinnerAdapter(this.context, R.layout.spinner_entry, stringList);
final Scan_Answers answer = getItem(position);
final TextView sectionNumber = (TextView) convertView.findViewById(R.id.questionNumber);
sectionNumber.setText("Section " + answer.section_number + " - Question " + answer.answer_id);
ImageView answerImage = (ImageView) convertView.findViewById(R.id.answerPic);
answerImage.setImageBitmap(answer.getAnswer_image());
final Spinner answerA = (Spinner) convertView.findViewById(R.id.answerA);
final Spinner answerB = (Spinner) convertView.findViewById(R.id.answerB);
final Spinner answerC = (Spinner) convertView.findViewById(R.id.answerC);
final Spinner answerD = (Spinner) convertView.findViewById(R.id.answerD);
answerA.setAdapter(mAdapter);
answerB.setAdapter(mAdapter);
answerC.setAdapter(mAdapter);
answerD.setAdapter(mAdapter);
if(answer.getNewValue() != null && !answer.getNewValue().isEmpty()) {
String a, b, c, d;
a = answer.getNewValue().substring(0, 1);
b = answer.getNewValue().substring(1, 2);
c = answer.getNewValue().substring(2, 3);
d = answer.getNewValue().substring(3, 4);
answerA.setSelection(mAdapter.getPosition(a),false);
answerB.setSelection(mAdapter.getPosition(b),false);
answerC.setSelection(mAdapter.getPosition(c),false);
answerD.setSelection(mAdapter.getPosition(d), false);
notifyDataSetChanged();
}
int count = 0;
answerA.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
answer.setNewValue(answerA.getSelectedItem().toString() + answerB.getSelectedItem().toString() + answerC.getSelectedItem().toString() + answerD.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
answerB.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
answer.setNewValue(answerA.getSelectedItem().toString() + answerB.getSelectedItem().toString() + answerC.getSelectedItem().toString() + answerD.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
answerC.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
answer.setNewValue(answerA.getSelectedItem().toString() + answerB.getSelectedItem().toString() + answerC.getSelectedItem().toString() + answerD.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
answerD.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
answer.setNewValue(answerA.getSelectedItem().toString() + answerB.getSelectedItem().toString() + answerC.getSelectedItem().toString() + answerD.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
我的微调器适配器
public class MathSpinnerAdapter extends ArrayAdapter<String> {
ArrayList<String> list;
Context context;
public MathSpinnerAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
list = objects;
this.context = context;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.spinner_entry, parent, false);
TextView label = (TextView) convertView.findViewById(R.id.text);
label.setText(list.get(position));
return convertView;
}
}
当listview中只有几个项目时,我无法弄清楚为什么onItemSelectedListener的位置始终为0。请帮忙!
更新:
我已经删除了onItemSelectedListeners,因为它们似乎并不是完全必要的,而且这一行导致了问题。
answer.setNewValue(answerA.getSelectedItem().toString() + answerB.getSelectedItem().toString() + answerC.getSelectedItem().toString() + answerD.getSelectedItem().toString());
这很奇怪,因为我所做的就是将所有的微调器连接在一起并将其设置为Scan_answers中的String值。我还在试图解决这个问题,如果我找出原因,我会更新
更新2:
所以我认为这可能与getSelectedItemPosition() of a spinner always returns 0
有关这个错误并不总是发生在我的情况下,除非我的列表视图中有3个或更少的项目并且它不会滚动很多。