【问题标题】:Spinners in List View列表视图中的微调器
【发布时间】:2011-06-14 19:29:16
【问题描述】:

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

那么:我该如何避免这种情况?

非常感谢您的帮助!

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

公共类 MyOnItemSelectedListener 实现 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!
  • 在你上面的代码中,当一个项目被选中时你的 MyOnItemSelectedListener 会做什么?我问是因为我认为在设置侦听器后立即调用 setSelection(2) 时它会被触发。
  • 我认为,它是否被解雇并不重要。我所做的就是将选择保存到一个列表中。我在上面添加了 MyOnItemSelectedListener。如果它在调用 setSelection 时触发,它会在列表中设置错误,但这并不能解释为什么它会在屏幕上显示选择 0。

标签: java android list spinner reset


【解决方案1】:

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

【讨论】:

  • 感谢您的快速回答。但是当再次绑定微调器时,会调用什么方法呢?我可以在哪里(以及如何)重新初始化微调器的状态?
  • 这取决于您用于列表的适配器类型。如果您只是实现一个 ListAdapter,您将在 getView() 中进行绑定。如果您使用的是 CursorAdapter,它还有一个将被调用的 bindView() 方法。
【解决方案2】:

改用 Spinner.setSelection(i, true)。

向 Sagi 致敬 http://sagistech.blogspot.com/2010/12/spinnersetselection-doesnt-work.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    相关资源
    最近更新 更多