【发布时间】: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() 方法中编写的这个完整的微调器代码。
【问题讨论】: