我正在開發一個應用程序來識別樹。因此,我讓用戶通過從微調器中選擇適當的特性來回答關於給定特性的問題。由於一棵樹有很多特徵,我有一個完整的spinners列表。到現在爲止還挺好。不幸的是,只要用戶將它們從屏幕上滾動下來,紡紗廠就會重置。這是至關重要的,我可以避免這一點!列表視圖中的Spinners
所以:我該如何避免這種情況?
我非常感謝您的幫助!
public class SearchAdapter extends ArrayAdapter<SearchItem>{
SearchAdapter() {
super(SearchWizard.this, R.layout.question);
}
public View getView(int position, View convertView, ViewGroup parent) {
View ret = convertView;
if (ret == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ret = inflater.inflate(R.layout.question, null);
}
SearchItem item = getItem(position);
String question = item.getQuestion();
TextView question_text = (TextView) ret.findViewById(R.id.question_search);
assert(question_text != null);
question_text.setText(question);
Spinner question_spinner = (Spinner) ret.findViewById(R.id.question_spinner);
assert(question_spinner != null);
question_spinner.setPrompt(question);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(SearchWizard.this,android.R.layout.simple_spinner_item, item.getOptions());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
question_spinner.setAdapter(adapter);
question_spinner.setOnItemSelectedListener(new MyOnItemSelectedListener(position));
question_spinner.setSelection(2);
return ret;
}
}
enter code herepublic class MyOnItemSelectedListener implements OnItemSelectedListener {
public int position;
public MyOnItemSelectedListener(int p) {
position = p;
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
LSsearchCriteria.set(position,Herbalist.encodeoptions(parent.getItemAtPosition(pos).toString()));
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public int position;
public MyOnItemSelectedListener(int p) {
position = p;
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
LSsearchCriteria.set(position,Herbalist.encodeoptions(parent.getItemAtPosition(pos).toString()));
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
您可以發佈您的代碼,請?我們不知道你是否有額外的聽衆或者弄亂了你的代碼。 – 2011-06-14 19:32:06
我發佈了源代碼。請注意,微調器在選擇2時正確初始化。一旦它再次滾動並在屏幕上顯示,就會顯示選擇0! – RangerJo 2011-06-16 14:38:56
在上面的代碼中,當選擇一個項目時,MyOnItemSelectedListener會做什麼?我問,因爲我認爲在設置監聽器之後立即調用setSelection(2)時它會被解僱。 – 2011-06-16 14:47:38