简体   繁体   English

Android中ArrayAdapters的奇怪行为

[英]Strange behavior with ArrayAdapters in Android

I've been playing around with ArrayAdapters and I've reached a point where I'm getting different results from two almost identical ArrayLists + ArrayAdapter combinations. 我一直在玩ArrayAdapters,并且已经达到了从两个几乎相同的ArrayLists + ArrayAdapter组合中获得不同结果的地步。

The first one: 第一个:

An ArrayList of 'Restaurant' objects, an ArrayAdapter that uses this ArrayList and a ListView that binds this ArrayAdapter. “餐厅”对象的ArrayList,使用此ArrayList的ArrayAdapter和绑定此ArrayAdapter的ListView。

private ArrayList<Restaurant> model = new ArrayList<Restaurant>();
private ArrayAdapter<Restaurant> restaurantAdapter = null; 
...
public void onCreate(Bundle savedInstanceState){
   ...
   restaurantAdapter = ArrayAdapter<Restaurant>(this, android.R.layout.simple_list_item_1, model);
   ...
   listView.setAdapter(restaurantAdapter);
   ...
}    

The second one: 第二个:

An ArrayList of String objects, an ArrayAdapter that uses this ArrayList and a AutoCompleteTextView that binds this ArrayAdatper. 一个String对象的ArrayList,使用此ArrayList的ArrayAdapter以及绑定此ArrayAdatper的AutoCompleteTextView。

private ArrayList<String> prevAddressList = new ArrayList<String>();
private ArrayAdapter<String> addListAdapter = null; 
...
public void onCreate(Bundle savedInstanceState){
   ...
   addListAdapter = ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, prevAdddressList);
   ...
   autoCompleteField.setAdapter(addListAdapter);
   ...
}    

I have a save button, on click, I'm creating a restaurant object with a name and an address and adding it to the first adapter, additionally, I want to create a list of previously used address so they are "auto completed" next time they are typing it, so I'm taking the text, and adding it to the second adapter. 我有一个保存按钮,单击时,我正在创建一个带有名称和地址的餐厅对象,并将其添加到第一个适配器中,此外,我想创建一个先前使用的地址列表,以便它们“自动完成”。他们输入的时间,所以我要取文字,并将其添加到第二个适配器中。

...onSave = new View.OnClickListener(){
...
restaurantAdapter.add(r); //r is a Restaurant object.
addListAdapter.add(autoCompleteField.getText().toString());
...
}

Now, everything is working properly. 现在,一切正常。 I get the Restaurants displayed in a ListView. 我将餐厅显示在ListView中。 The AutoComplete is working as expected.... but I noticed something when I was checking the values while debugging: AutoComplete可以按预期工作...。但是在调试时检查值时,我注意到了一些东西:

The actual ArrayLists, model (Restaurant) is getting updated after adding an object to the adapter , but prevAddressList (String) is not. 在向适配器添加对象后,实际的ArrayLists模型(餐厅)正在更新,但是prevAddressList(字符串)未更新。

Unless, I set the AutoCompleteTextField empty.... then, the prevAddressList gets updated after adding something to the second adapter. 除非我将AutoCompleteTextField设置为空。...然后,在向第二个适配器中添加内容后,prevAddressList会更新。

Already tried using notifyDataSetChanged(), but it makes no difference (and it is set to true on every adapter by default anyway). 已经尝试使用notifyDataSetChanged(),但是没有什么区别(无论如何,默认情况下在每个适配器上它都设置为true)。

Other behavior that differs between the two adapters is that in the first one (Restaurant), values are going to the mObjects field, while in the second one (String) they are going to mOriginalValues instead. 这两个适配器之间的其他不同之处在于,在第一个适配器(餐厅)中,值将转到mObjects字段,而在第二个适配器(字符串)中,值将转到mOriginalValues。

I'm completely stomped. 我完全被踩了。 The only difference between those two adapters is that one is type "Restaurant" and the other is type "String". 这两个适配器之间的唯一区别是,一个适配器是“ Restaurant”类型,另一个是“ String”类型。

Any ideas? 有任何想法吗? Maybe I'm missing something very obvious? 也许我缺少一些非常明显的东西? Let me know if you need the full code. 让我知道您是否需要完整的代码。

thanks 谢谢

Instead of adding it to the adapter, try adding the object to your list and then calling notifyDataSetChanged on your adapter. 与其将其添加到适配器中,不如尝试将对象添加到列表中,然后在适配器上调用notifyDataSetChanged。 The adapter should pick up your changes and your list of course will have the object you just added. 适配器应该接管您所做的更改,并且您的列表中肯定会有刚添加的对象。

For anyone coming here from google: 对于来自Google的任何人:

Unable to modify ArrayAdapter in ListView: UnsupportedOperationException 无法在ListView中修改ArrayAdapter:UnsupportedOperationException

This might explain the behavior, although I have to test it myself. 尽管我必须自己进行测试,但这可能可以解释这种行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM