Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android ArrayAdapter items update

Tags:

I have ArrayAdapter with this items structure:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout ... >

         <TextView
             android:id="@+id/itemTextView"
             ... />
</RelativeLayout>

And add this adapter so:

mAdapter = new ArrayAdapter<String>(this, R.layout.item, 
                                            R.id.itemTextView, itemsText);

All is fine but I want to update text in adapter's items. I found a solution

mAdapter.notifyDataSetChanged();

but do not understand how to use it. Help please.

upd My code:

String[] itemsText = {"123", "345", "567"};
ArrayAdapter<String> mAdapter;

onCreate

mAdapter = new ArrayAdapter<String>(this, R.layout.roomitem, 
                                              R.id.itemTextView, itemsText);
setListAdapter(mAdapter);
itemsText = {"789", "910", "1011"};

onClick

mAdapter.notifyDataSetChanged();
//it's dont work
like image 459
Leo Avatar asked Feb 21 '12 15:02

Leo


People also ask

How do I refresh ArrayAdapter?

You can modify existing adapter data and call notifyDataSetChanged(). In your case you should call listView. setAdapter(adapter) in onClick method.

What is an ArrayAdapter in Android?

android.widget.ArrayAdapter<T> You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .

How do I use ArrayAdapter?

Go to app > res > layout > right-click > New > Layout Resource File and create a new layout file and name this file as item_view. xml and make the root element as a LinearLayout. This will contain a TextView that is used to display the array objects as output.


2 Answers

Your problem is a typical Java error with pointers.

In a first step you are creating an array and passing this array to the adapter.

In the second step you are creating a new array (so new pointer is created) with new information but the adapter is still pointing to the original array.

// init itemsText var and pass to the adapter
String[] itemsText = {"123", "345", "567"};
mAdapter = new ArrayAdapter<String>(..., itemsText);

//ERROR HERE: itemsText variable will point to a new array instance
itemsText = {"789", "910", "1011"};

So, you can do two things, one, update the array contents instead of creating a new one:

//This will work for your example
items[0]="123";
items[1]="345";
items[2]="567";

... or what I would do, use a List, something like:

List<String> items= new ArrayList<String>(3);
boundedDevices.add("123");
boundedDevices.add("456");
boundedDevices.add("789");

And in the update:

boundedDevices.set("789");
boundedDevices.set("910");
boundedDevices.set("1011");

To add more information, in a real application normally you update the contents of the list adapter with information from a service or content provider, so normally to update the items you would do something like:

//clear the actual results
items.clear()

//add the results coming from a service
items.addAll(serviceResults);

With this you will clear the old results and load the new ones (think that the new results should have a different number of items).

And off course after update the data the call to notifyDataSetChanged();

If you have any doubt don't hesitate to comment.

like image 23
Carlos Verdes Avatar answered Sep 22 '22 16:09

Carlos Verdes


I think something like this

public void updatedData(List itemsArrayList) {

    mAdapter.clear(); 

    if (itemsArrayList != null){

        for (Object object : itemsArrayList) {

            mAdapter.insert(object, mAdapter.getCount());
        }
    }

    mAdapter.notifyDataSetChanged();

}
like image 53
Luciano Avatar answered Sep 18 '22 16:09

Luciano