Mahdi Rasti
Mahdi Rasti

Reputation: 67

notifyDataSetChanged not work

I have problem with notifyDataSetChanged,i read some another post but can't help me and i have problem yet, i call that after my listview setadapter but mylist hasn't any change! this is my code, please help me, Thanks

public class PostDataAsyncTask extends AsyncTask<String, String, String> {

    // this will post our text data

    protected void onPreExecute() {
        super.onPreExecute();
        // do stuff before posting data
    }

    @Override
    protected String doInBackground(String... strings) {
        try {
            postTextandGetRespons("http://demo.codeofaninja.com/tutorials/json-example-with-php/index.php");

            JSONObject JsonOb = new JSONObject(responseString);

            JSONArray messages = JsonOb.getJSONArray("Users");
            for ( int i=0; i<= f;i++){

                JSONObject c = messages.getJSONObject(i);

                firstname = c.getString("firstname");
                lastname = c.getString("lastname");
                username = c.getString("username");

                items.add(new item(firstname,lastname,username));
            }
            adapter.notifyDataSetChanged();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String responseStr) {
        if ( setAdapter == true) {
            lv = (ListView) findViewById(R.id.listView_asabani);
            adapter = new adapter_common(getBaseContext(), items);
            lv.setAdapter(adapter);
            setAdapter = false;
        }
    }
}

Upvotes: 3

Views: 85

Answers (1)

meda
meda

Reputation: 45500

Move adapter.notifyDataSetChanged(); to onPostExecute() that's where UI code should run.

Upvotes: 1

Related Questions