Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android adapter.notifyDataSetChanged() not working?

Tags:

android

I'm writing an app that requires getting input from the user, passing it off to a server, which returns a JSON string, and then displaying its parsed contents in a ListView. I'm currently accomplishing this by extending AsyncTask:

//stripped down version
public class main extends ListActivity {

ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
final EditText input = (EditText)findViewById(R.id.input);


input.addTextChangedListener(new TextWatcher() {    
    public void onTextChanged(CharSequence s, int start, int before, int count) {   
            if(!input.getText().toString().equals("")) {
            new GetDataTask().execute(input.getText().toString());
        }

    }   
});

private class GetDataTask extends AsyncTask<String, Void, ArrayList<String>> {
        protected ArrayList<String> doInBackground(String... query) {

        URL url = new URL("http://myserversaddressgoeshere.com/search/thequerygoeshere");
        URLConnection conn = url.openConnection();
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            ArrayList<String> items = new ArrayList<String>();
        //the code that parses the JSON goes here; it writes some strings to items
        return items;
        }

        protected void onPostExecute(ArrayList<String> items) {
            listItems = items;
        adapter.notifyDataSetChanged();
        }
}

}

(This is my first post so I apologize in advance if my attempt to make that a code block fails)

In the onPostExecute method, after setting listItems equal to items, if I print the contents of listItems, they're exactly what I want. But, for some reason, when executed from onPostExecute, adapter.notifyDataSetChanged() seems to do nothing (when it's called from the main thread, it works fine).

If anyone has any idea of what's going on and/or how to fix it, I'd really appreciate it. Thanks!

like image 317
Chris Leonard Avatar asked Oct 06 '11 00:10

Chris Leonard


People also ask

How does notifyDataSetChanged work on Android?

notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

How do I use notifyDataSetChanged in activity?

Create your own class derived from BaseAdapter and ListAdapter that allows changing of the underlying List data structure. Use the notifyDataSetChanged() every time the list is updated. To call it on the UI-Thread, use the runOnUiThread() of Activity. Then, notifyDataSetChanged() will work.

What does notifyDataSetChanged do in Recyclerview?

What does notifyDataSetChanged() do on recyclerview ? Notify any registered observers that the data set has changed. There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred.


1 Answers

can you try this

    protected void onPostExecute(ArrayList<String> items) {
        listItems.clear();
        listItems.addAll(items);
        adapter.notifyDataSetChanged();
    }

and i do hope you are already doing something this

adapter = new ArrayAdapter<String>(listItems);
like image 84
Samuel Avatar answered Oct 30 '22 19:10

Samuel