我的活动中有一个微调器,从数据库填充,如果活动获得了额外的Intent,我想根据额外选择从微调器中设置一个Item。例如 - 如果我有一个额外的“CURR_NOTE_CATEGORY”并且它的值是2,我想要选择微调器中的第二个项目。没有额外的东西 - 只是普通的微调器。 所以我有这个代码:
int curr_note_category;
ArrayAdapter<String> adapter;
NotesManager manager = new NotesManager(this);
ArrayList<Category> arrListCategories;
ArrayList<String> arrListCategoriesString = new ArrayList<String>();
Spinner spCategories;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_edit_note);
spCategories = (Spinner) findViewById(R.id.spCategories);
if(getIntent().getExtras()!=null){
// Get Extras
curr_note_category = getIntent().getExtras().getInt("CURR_NOTE_CATEGORY");
spCategories.setSelection(curr_note_category);
}
manager.getAllCategories();
arrListCategories = manager.getAllCategories();
for (int i = 0; i < arrListCategories.size(); i++)
{
Category currCategory = arrListCategories.get(i);
arrListCategoriesString.add(currCategory.getCategory_name().toString());
}
adapter=new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, arrListCategoriesString);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCategories.setAdapter(adapter);
spCategories.setOnItemSelectedListener(spinnerListener);
}
我正在使用常规微调器,但是当有Intent Extra时,我从未选择任何项目。那怎么办呢?
答案 0 :(得分:2)
请在setOnItemSelectedListener();
adapter=new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, arrListCategoriesString);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCategories.setAdapter(adapter);
spCategories.setOnItemSelectedListener(spinnerListener);
if(getIntent().getExtras()!=null)
{
// Get Extras
curr_note_category = getIntent().getExtras().getInt("CURR_NOTE_CATEGORY");
spCategories.setSelection(curr_note_category);
}