即使未选择item,OnItemSelected也会在Spinner中触发

时间:2015-03-14 07:35:20

标签: android android-spinner

我有微调器的以下代码。我没有得到任何错误,spinner正在工作,但我遇到了问题,即使我没有选择任何项目,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()方法中编写的完整的微调器代码。

1 个答案:

答案 0 :(得分:0)

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

如果你真的想在初始化时阻止它,那么在onitemselected

中以这种方式这样做
check=check+1 
if(check>1) {
    // put your code
    }