【问题标题】:Spinner OnItemSelectedListener double select issueSpinner OnItemSelectedListener 双选问题
【发布时间】:2012-04-03 14:08:27
【问题描述】:

我遇到了一个奇怪的问题。我的 OnItemSelectedListener 似乎只工作一次,我的意思是它在单击相应项目时第一次显示我的测试 Toast,但是当我第二次点击相同的项目时它不显示测试 Toast。(它确实有效第二次点击不同的项目时)有什么问题?请帮帮我

部分代码在这里

        //get task object from menu
    taskListArr = new ArrayList<Task>();

    taskListArr = getCurrentTasks(taskListArr);

    myTask=new TaskListAdapter(this, 0, taskListArr);
    ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, TaskModel.sorts);
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sortSpinner.setAdapter(aa);
    sortSpinner.setOnItemSelectedListener(this);



    @SuppressWarnings("unchecked")
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    if(arg2 == 0){
        Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
        Collections.sort(taskListArr);
        taskListView.setAdapter(myTask);
    }
    if(arg2 == 1){
        Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
        Collections.sort(taskListArr, new DateComparator());
        taskListView.setAdapter(myTask);
    }
    if(arg2 == 2){
        Toast.makeText(getApplicationContext(), "3", Toast.LENGTH_SHORT).show();
        Collections.sort(taskListArr, new PriorityComparator());
        taskListView.setAdapter(myTask);
    }
    position = arg2;
}

public void onNothingSelected(AdapterView<?> arg0) {

}

【问题讨论】:

    标签: android android-spinner


    【解决方案1】:

    在安卓开发者网站http://developer.android.com/reference/android/widget/Spinner.html查看微调器

    A view that displays one child at a time and lets the user pick among them. The items in the Spinner come from the Adapter associated with this view.
    

    它一次选择一个孩子。所以再次选择已经选择的孩子不会调用onItemSelected函数。

    【讨论】:

    • 感谢您的解释,但我仍然需要弄清楚如何多次调用它
    【解决方案2】:

    我同意 user936414 的回答,他是对的,但如果你仍然希望你的 toast 再次出现,那么在你的微调器上添加一个触摸监听器,并在 ontouch 事件中添加这个

    line sortSpinner.setOnItemSelectedListener(this);
    

    每次触摸时,您的微调器侦听器都会再次被调用,并且您每次都会获得 on itemselected

    【讨论】:

    • 谢谢,我会试试的,但它似乎不起作用。也许我需要创建一个自定义适配器来处理这个问题。
    • 对不起,我试过但失败了。我使用菜单监听器来解决这个问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多