【问题标题】:Even though item is not selected OnItemSelected is getting triggered in Spinner即使未选择项目 OnItemSelected 在 Spinner 中被触发
【发布时间】:2015-03-14 15:39:35
【问题描述】:

我有以下微调器代码。我没有收到任何错误,并且微调器正在工作,但是即使我没有选择任何项目,我也遇到了问题,onItemSelected 侦听器中的代码正在执行,但我不希望这种情况发生,直到我选择和来自微调器的项目。请任何人告诉我为什么会这样。非常感谢。欢迎所有建议。提前致谢。

我的微调器代码:

SQLiteDataBaseAdapter adapter = new SQLiteDataBaseAdapter(this);

        Cursor cursor = adapter.getAllSubTaskData();

        // Log.d("Pana", "The value of cursor is " +Integer.parseInt(String.valueOf(cursor.toString())));

        String[] fromFieldNames = new String[]{SQLiteHelper.UID, SQLiteHelper.SUB_TASK_NAME};
        int[] toViewIds = new int[]{R.id.textViewUID, R.id.textViewSubTaskName};

        SimpleCursorAdapter myCursorAdapter;
        myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.custom_spinner_subtask_row, cursor, fromFieldNames, toViewIds, 0);

        subTaskList.setAdapter(myCursorAdapter);


        subTaskList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


                Toast.makeText(view.getContext(), "The position of the item clicked is " + position, Toast.LENGTH_LONG).show();

                SharedPreferences sharedPreferences = getSharedPreferences("MyData1", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("position", Long.toString(id));
                editor.commit();



                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), SubTaskDetail.class);
                intent.putExtra("position", Long.toString(id));  //position starts from 0, but in db row starts from 1
                startActivity(intent);


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

我在我的活动的 onCreate() 方法中编写的这个完整的微调器代码。

【问题讨论】:

    标签: android android-spinner


    【解决方案1】:

    这是微调器的默认功能。当适配器绑定到微调器时,微调器将选择第 0 个位置

    如果你真的想在初始化时阻止它,那么在 onitemselected 中以这种方式进行

    check=check+1 
    if(check>1) {
        // put your code
        }
    

    【讨论】:

    • 是的,它最初没有触发,但是在我单击位置 0 的项目后,它不会被执行,直到我选择位置 0 以外的项目。
    • 有没有办法解决这个问题?我已将检查初始化为 0。
    • 你有没有遇到其他我可以使用的小部件?
    猜你喜欢
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多