1

我的活动中有一个微调器,从数据库填充,如果活动获得带有附加功能的 Intent,我想从根据额外选择的微调器中设置一个项目。例如 - 如果我有一个额外的“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 时,从来没有选择过的项目。那么怎么做呢?

4

1 回答 1

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);
}
于 2012-08-25T10:07:13.677 回答