Android 根据“Parent”更新微调器微调器的选择 - .notifyDataSetChange() 未触发?
因此,我有两个微调器,我们将第一个微调器称为“父级”(account_type_spinner),将第二个微调器称为“子级”(account_name_spinner)。请注意以下代码中子项 (account_name_spinner) 的 ArrayAdapter 初始化,我向其提供了我之前在以下代码行 (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。显然,每次在父级微调器中进行选择时都会触发此类,代码如下所示:
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 上进一步研究了这个问题,发现我必须在列表更新之前从我的孩子的 ArrayAdapter (account_name_adapter) 中删除 .clear() 或 .remove() 项目,但是,当我尝试“account_name_adapter.clear();”时在通知 ArrayAdapter 之前,我收到一条错误消息,指出该操作非法。有什么想法我做错了吗?
So, I have two spinners, let's call the first spinner Parent (account_type_spinner) and the second spinner Child (account_name_spinner). Please notice in the following code the ArrayAdapter initialization for the Child (account_name_spinner), I am feeding it a string array of account names I previously queried before the following lines of code (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);
Selection in the Parent spinner triggers my "SpinnerSelectionListener" which is simply my class implementing OnItemSelectedListener. This class fires obviously each time a selection is made in the Parent spinner and the code looks like the following:
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.
}
}
The code above should update the Child (account_name_spinner) with INCOME or EXPENSE accounts whenever the Parent's selection changes from INCOME to EXPENSE or vice versa (this is an accounting app). The updating of the Child's spinner list should be facilitated by the "account_name_adapter.notifyDataSetChanged();" however nothing is happening.
I looked into this issue further on StackOverflow and found that I must .clear() or .remove() items from my Child's ArrayAdapter (account_name_adapter) before the list will update, however, when I try "account_name_adapter.clear();" before notifying the ArrayAdapter I get an error that the operation is illegal. Any ideas what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在获得更新的数组(account_name_array)后重新分配适配器
编辑:对于适配器,您不传递引用,因此当您更新数组时,适配器仍然具有相同的数据。调用 .notifyDataSetChanged() 不会使用新数组更新适配器。
You can just reassign the adapter after you got the updated array (account_name_array)
EDIT: To the adapter you don't pass a reference, so when you update the array the adapter still have the same data. Calling .notifyDataSetChanged() do not update the adapter with the new array.