2011-08-01 23 views
3

私は2つのスピナーを持っています。最初のスピナーParent(account_type_spinner)と2番目のスピナーChild(account_name_spinner)を呼び出してみましょう。次のコードで子供(account_name_spinner)用ArrayAdapterの初期化を注意してください、私はそれを私が以前に次のコード行(account_name_array)の前に照会アカウント名の文字列配列を供給しています:Androidは「親」スピナーの選択に基づいてスピナーを更新します - .notifyDataSetChange()は起動しませんか?

//---define spinner objects as variables, assign adapters and listeners--- 

    account_type_spinner = (Spinner) findViewById(R.id.account_type_spinner);  
    account_type_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, account_type_array); 
    account_type_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    account_type_spinner.setAdapter(account_type_adapter); 
    account_type_spinner.setOnItemSelectedListener(new SpinnerSelectionListener()); 

    account_name_spinner = (Spinner) findViewById(R.id.account_name_spinner);  
    account_name_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, account_name_array); 
    account_name_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    account_name_spinner.setAdapter(account_name_adapter);  

セレクション親にspinnerが、私の "SpinnerSelectionListener"をトリガーします。これは、単にOnItemSelectedListenerを実装するクラスです。選択は親スピナーとコードで行われるたびに、次のようになります。このクラスの火災は明らかに:

public class SpinnerSelectionListener implements OnItemSelectedListener { 

    public void onItemSelected(AdapterView<?> parent, 
     View view, int pos, long id) {   
     String spinner_selection = parent.getItemAtPosition(pos).toString(); 

     if(spinner_selection.contentEquals(INCOME)) {    
      //---grab Income type accounts from db and build array---    
      db.open(); 
      account_name_array = db.getAccounts(INCOME);  
      account_name_adapter.notifyDataSetChanged(); 

      dr_amount_textview.setVisibility(View.GONE); 
      dr_amount.setVisibility(View.GONE); 
      cr_amount_textview.setVisibility(View.VISIBLE); 
      cr_amount.setVisibility(View.VISIBLE); 
      db.close(); 
     } else { 
      //---grab Expense type accounts from db and build array--- 
      db.open(); 
      account_name_array = db.getAccounts(EXPENSE); 
      account_name_adapter.notifyDataSetChanged(); 

      cr_amount_textview.setVisibility(View.GONE); 
      cr_amount.setVisibility(View.GONE); 
      dr_amount_textview.setVisibility(View.VISIBLE); 
      dr_amount.setVisibility(View.VISIBLE); 
      db.close(); 
     } 
    } 

    public void onNothingSelected(AdapterView<?> parent) { 
     // Do nothing. 
    } 
} 

上記のコードは、いつでも親の選択範囲の変更からの収益または費用勘定と子(account_name_spinner)を更新する必要がありますEXPANSSEへのインセンティブまたはその逆(これは会計アプリケーションです)。子のスピナーリストの更新は、 "account_name_adapter.notifyDataSetChanged();"しかし何も起こっていない。

StackOverflowでさらにこの問題を調べたところ、リストが更新される前に、私の子供のArrayAdapter(account_name_adapter)から.clear()または.remove()項目を更新する必要があることがわかりました。 "account_name_adapter.clear ); " ArrayAdapterに通知する前に、操作が不正であるというエラーが表示されます。私が間違っていることは何ですか?

答えて

1

あなたは(account_name_array)

account_name_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, account_name_array); 
account_name_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
account_name_spinner.setAdapter(account_name_adapter); 

更新配列を得た後、あなただけのアダプターを再割り当てすることができEDIT:アダプターにあなたはとてもアダプターがまだ持っている配列を更新する場合、参照を渡すことはありません。同じデータ。 .notifyDataSetChanged()を呼び出すと、アダプタが新しい配列で更新されません。

+0

本当に新しいアダプタを作成して、再割り当てする必要がありますか?私はデータを変更することができず、元のアダプタに通知することはできません、なぜこれが知っていますか? – AutoM8R

+0

私は前日と同じことを探していて、ソリューションとしての適合の再割り当てだけを見つけました。私は知っている、私はあまりにも驚いています... – BrainCrash

+0

回答ありがとう、私は自分でこれを思いついたことはありません。これは非常に非常に驚いています。実際、私はこれが昨日他の場所で提案されているのを見たと思うし、それは間違っていると全然考えていた。あなたがこの答えを見て、それが偽であると思うなら、それは残念なことにありません。 – AutoM8R

関連する問題