notifyDataSetChanged() 没有更新我的微调器的数组
问题:如果我在 onCreate
之前设置变量,我可以使用 notifyDataSetChanged()
来更新稍后使用该数组的适配器吗?
我在 onCreate
之前煽动我的 city_values 数组
- 这是让脚本不显示任何错误的唯一方法。但是,一旦用户从其微调器中选择了一个状态,就应该使用 notifyDataSetChanged()
来更新附加 city_values 数组的适配器。下面是我的代码的一小部分。我认为我的问题与 city_value 设置得太早有关。我该如何解决这个问题?
public class SearchActivity extends Activity{
ArrayAdapter<String> adapter2;
String city_values[] = new String[]{"Please select a state first."};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_layout)
adapter2 = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, city_values);
adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
cityspinner.setAdapter(adapter2);
//On select of State spinner use item value to query and get citys reassign those values back to city_values and then tell adapter2 notifyDataSetChanged()
for (int i=0; i<jsonArray.length(); i++)
{
String styleValue = jsonArray.getJSONArray(i).getString(0);
Log.d(TAG, styleValue);
city_spinner[i] = styleValue;
}
city_values = city_spinner;
adapter2.notifyDataSetChanged();
QUESTION: If I set a variable prior to the onCreate
can I use notifyDataSetChanged()
to update an adapter that uses that array later?
I am instigating my city_values array
prior to my onCreate
-This is the only way I can get the script not to show any errors. But once the user selects a state from its spinner it should use notifyDataSetChanged()
to update the adapter that attaches the city_values array. Below is a small section of my code. I think my issue has to do with the city_value being set to early. How can I get around this?
public class SearchActivity extends Activity{
ArrayAdapter<String> adapter2;
String city_values[] = new String[]{"Please select a state first."};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_layout)
adapter2 = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, city_values);
adapter2.setDropDownViewResource(R.layout.city_spinner_layout);
cityspinner.setAdapter(adapter2);
//On select of State spinner use item value to query and get citys reassign those values back to city_values and then tell adapter2 notifyDataSetChanged()
for (int i=0; i<jsonArray.length(); i++)
{
String styleValue = jsonArray.getJSONArray(i).getString(0);
Log.d(TAG, styleValue);
city_spinner[i] = styleValue;
}
city_values = city_spinner;
adapter2.notifyDataSetChanged();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好先声明一个ArrayList,然后添加ArrayList中的内容,并将数据设置到适配器并通知。
现在,如果您想更新此 cityspinner 所选项目上的另一个微调器,您可以以相同的方式获取另一个 ArrayList 并在其中添加项目并设置适配器。
取一个
ArrayList; city_spinner_array = new ArrayList;
现在,您将在
city_spinner_array
中获得新值。因此,设置适配器就像之前的微调器一样。It would be better to declare an ArrayList and then add the content in ArrayList and set the data to the adapter and notify.
Now if you want to update another spinner on this cityspinner selected item, you can take another ArrayList in the same way and add the items in that and set the Adapter.
Take an
ArrayList<String> city_spinner_array = new ArrayList<String>;
And, now you will have your new values in
city_spinner_array
. So, set the adapter and you did for the previous spinner.