Android根据“父”微调器选择更新微调器 - .notifyDataSetChanged()没有触发?

时间:2011-08-01 18:34:39

标签: android spinner android-arrayadapter

所以,我有两个微调器,让我们调用第一个微调器Parent(account_type_spinner)和第二个微调器Child(account_name_spinner)。请在下面的代码中注意Child的ArrayAdapter初始化(account_name_spinner),我给它提供了一个字符串数组的帐户名,我之前在下面的代码行(account_name_array)之前查询过:

//---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的类。每次在Parent微调器中进行选择时,此类显然会触发,代码如下所示:

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.
    }
}

每当Parent的选择从INCOME更改为EXPENSE时,上面的代码应使用INCOME或EXPENSE帐户更新Child(account_name_spinner),反之亦然(这是一个会计应用程序)。应该通过“account_name_adapter.notifyDataSetChanged();”来促进更新Child的微调器列表。但是没有发生任何事情。

我在StackOverflow上进一步研究了这个问题,发现我必须在列表更新之前从我的Child的ArrayAdapter(account_name_adapter)中使用.clear()或.remove()项,但是,当我尝试“account_name_adapter.clear()时;”在通知ArrayAdapter之前,我收到一个错误,指出操作是非法的。我有什么想法我做错了吗?

1 个答案:

答案 0 :(得分: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()不会使用新数组更新适配器。