2

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.

The first one:

An ArrayList of 'Restaurant' objects, an ArrayAdapter that uses this ArrayList and a ListView that binds this ArrayAdapter.

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.

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. The AutoComplete is working as expected.... but I noticed something when I was checking the values while debugging:

The actual ArrayLists, model (Restaurant) is getting updated after adding an object to the adapter , but prevAddressList (String) is not.

Unless, I set the AutoCompleteTextField empty.... then, the prevAddressList gets updated after adding something to the second adapter.

Already tried using notifyDataSetChanged(), but it makes no difference (and it is set to true on every adapter by default anyway).

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.

I'm completely stomped. The only difference between those two adapters is that one is type "Restaurant" and the other is type "String".

Any ideas? Maybe I'm missing something very obvious? Let me know if you need the full code.

thanks

cumanzor
  • 174
  • 2
  • 9

2 Answers2

0

Instead of adding it to the adapter, try adding the object to your list and then calling notifyDataSetChanged on your adapter. The adapter should pick up your changes and your list of course will have the object you just added.

Scott Merritt
  • 1,284
  • 2
  • 13
  • 24
  • Already tried that. In fact, at first I was directly modifying the array and that's what I originally thought was causing the issue. I've hence moved on to another topic but will sure revisit this in the future as the workaround is pretty easy, but for the sake of understanding how Android works I **need** to know what wis causing the inconsistent behavior (why were the values stored in different fields in both adapters and/or when the value was just an empty string, etc). – cumanzor Feb 08 '12 at 08:14
0

For anyone coming here from google:

Unable to modify ArrayAdapter in ListView: UnsupportedOperationException

This might explain the behavior, although I have to test it myself.

Community
  • 1
  • 1
cumanzor
  • 174
  • 2
  • 9