0

1 つのスピナーから selectedItemPosition を取得し、2 番目のスピナーで使用する必要があります。この値を動的に変更したい。

スピナーを使用するクラスのコードは次のとおりです。

int item;
Spinner spinner_cat;
Spinner spinner_subcat;
.....
    spinner_cat = (Spinner) findViewById(R.id.spinner1);
    List<String> category = dbAdapter.getAllCategory();
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, category);
    dataAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner_cat.setAdapter(dataAdapter);
    spinner_cat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent,
                        View view, int position, long id) {
                    ExpenseDB.this.item = spinner_cat
                            .getSelectedItemPosition() + 1;
                    Log.i("Is it work", "in method " + item);

                }

                public void onNothingSelected(AdapterView<?> arg0) {

                }
            });

    spinner_subcat = (Spinner) findViewById(R.id.spinner2);
    List<String> sub_category = dbAdapter.getAllSubCategory(item); // here it must change dynamically
    ArrayAdapter<String> subDataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, sub_category);
    subDataAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner_subcat.setAdapter(subDataAdapter);
    dbAdapter.close();

}

}

4

1 に答える 1

1
spinner_cat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent,
                        View view, int position, long id) {
        ExpenseDB.this.item = spinner_cat.getSelectedItemPosition() + 1;
        fillsubCategory();
    }
 });
...
void fillsubCategory() {
    spinner_subcat = (Spinner) findViewById(R.id.spinner2);
    List<String> sub_category = dbAdapter.getAllSubCategory(item); 
    ArrayAdapter<String> subDataAdapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_spinner_item, sub_category);
    subDataAdapter
         .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner_subcat.setAdapter(subDataAdapter);
}
于 2012-07-26T13:04:03.397 に答える