列表视图中的旋转器

时间:2011-06-14 19:29:17

标签: java android list spinner reset

我正在开发一款识别树木的应用。因此,我让用户通过从微调器中选择适当的特性来回答有关给定特征的问题。因为树有很多特征,所以我有一整套的微调器。到现在为止还挺好。不幸的是,一旦用户将它们从屏幕上滚动,微调器就会重置。至关重要的是,我可以避免这种情况!

所以:我怎么能避免这个?

非常感谢您的帮助!

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. } }

2 个答案:

答案 0 :(得分:0)

您需要在单独的数据结构中跟踪微调器的状态,并在再次绑定该行时,使用保存的状态初始化微调器。

答案 1 :(得分:0)

改为使用Spinner.setSelection(i,true)。

Kudos去了Sagi http://sagistech.blogspot.com/2010/12/spinnersetselection-doesnt-work.html