2011-08-01 96 views
3

所以,我有兩個調整器,我們先調用第一個微調器Parent(account_type_spinner)和第二個微調器Child(account_name_spinner)。請在下面的代碼注意到一個ArrayAdapter初始化爲兒童(account_name_spinner),我餵養它,我下面的代碼行前在過去查詢帳戶名稱的字符串數組(account_name_array):Android基於「Parent」微調器的選擇更新微調器 - .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);  

選擇在父微調控制器觸發我的「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)與收入或支出賬戶,只要從父母的選擇變化收入費用,反之亦然(這是一個會計應用程序)。 「兒童的微調列表」的更新應該通過「account_name_adapter.notifyDataSetChanged();」然而沒有發生。

我在StackOverflow上進一步研究了這個問題,發現在列表更新之前,我必須從我的Child's ArrayAdapter(account_name_adapter)中清除()或.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); 

後編輯:適配器,你不及格的參考,所以當你更新數組適配器仍然有相同的數據。調用.notifyDataSetChanged()不會使用新數組更新適配器。

+0

你真的必須創建並重新分配新的適配器嗎?我很驚訝我無法更改數據並通知原來的適配器,你知道這是爲什麼嗎? – AutoM8R

+0

我幾天前在尋找同樣的東西,發現只有改編的解決方案。我知道,我也很驚訝...... – BrainCrash

+0

答案接受,謝謝你,我永遠不會拿出這個在我自己的。非常非常驚訝的是這種情況。事實上,我相信我昨天在其他地方看到了這一點,我完全認爲這是錯誤的。所以,如果你正在看這個答案,並認爲它是假的,那不是不幸的。 – AutoM8R