How to reset ListView with ArrayAdapter after retrieving data

I use the ListAdapter to populate the ListView as follows:

static final String[] PROBLEMS = new String[] {"one", "two", "three" };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);        

    setListAdapter(new ArrayAdapter<String>(this, R.layout.my_problems, PROBLEMS));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

and after that I make a remote call on my server to get more data for this list with AsyncTask call, and when I get the data from the server, I don’t know how to populate and reset View the list. So far, I have something like this:

    @Override
    protected void onPostExecute(String result) 
    {       
            // Unwrap the stuff from the JSON string                
            String problem_title = null;
            String problem_id = null;

            try
            {
                JSONArray obj = new JSONArray(result);
                JSONObject o = obj.getJSONObject(0);                    

                Log.d( "Title: " , "" + o.getString("problem_title") );       
                Log.d( "id: " , "" + o.getString("problem_id") );      

                problem_title = o.getString("problem_title");
                problem_id = o.getString("problem_id");
            }
            catch ( Exception e )
            {
            }

            // Now not sure what to do :)
            // How do I reset the list that I had set up above?
                }

I can cast the result to appropriately structured data in the reset list, but I'm not sure how to do it. Can someone help? :)

+5
source share
3 answers

I use it that way

    values = new ArrayList<String>();
    //put anything you want in values as start
    adapter = new ArrayAdapter<String>(this,R.layout.notification, values);
    setListAdapter(adapter);

then

    //change values array with your new data then update the adapter
    adapter.notifyDataSetChanged();

then the contents of the list will change during the execution of this function

+16
source

, . -

adapter.clear();
for(int i = 0;i<categoriesArray.length;i++){
    adapter.add(categoriesArray[i]);
} 

, , , .

adapter.notifyDataSetChanged();

API 11 , addAll() . .

adapter.clear();
adapter.addAll(categoriesArray);
+7
adapter.notifyDataSetChanged();

is a good option for this, but sometimes it does not work as we need. In this case, you can install the adapter again and your list will be updated. But this is not a good opportunity, because it displays the entire list again, so this leads to poor performance. And your application is slowing down.

+3
source

All Articles