开发者

Android adapter.notifyDataSetChanged() not working?

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开发者_JAVA技巧 really appreciate it. Thanks!


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜