Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset the ListView with the ArrayAdapter after fetching data

I am using an ListAdapter to populate a ListView like this:

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 am making a remote call to my server to get more data for that list with an AsyncTask call, and when I get the data back from the server I don't know how to populate and reset the ListView. 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 make the result into appropriately structured data to reset the list but not sure how that is done. Can someone please help? :)

like image 880
GeekedOut Avatar asked Mar 10 '12 15:03

GeekedOut


People also ask

How can you update a ListView dynamically?

This example demonstrates how do I dynamically update a ListView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I reset my Android phone adapter?

Find and tap Settings > System > Advanced > Reset options > Reset network settings.

How do you connect an adapter with ListView write down the codes for it?

Attaching the Adapter to a ListView // Construct the data source ArrayList<User> arrayOfUsers = new ArrayList<User>(); // Create the adapter to convert the array to views UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers); // Attach the adapter to a ListView ListView listView = (ListView) findViewById(R. id.


2 Answers

I use it this 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 listview content will change at the time you execute this function

like image 103
Hesham Saeed Avatar answered Nov 01 '22 14:11

Hesham Saeed


You do not need to reinitialize the adapter, if you just want to change the data set. Try the following code -

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

after that if required, you can notify the change to the adapter too, although it shouldn't be necessary.

adapter.notifyDataSetChanged();

if you're targetting API level 11 or above, you can use addAll() method on adapter. Since it's more efficient.

adapter.clear();
adapter.addAll(categoriesArray);
like image 45
noob Avatar answered Nov 01 '22 13:11

noob