Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

notifyDataSetChanged not work

Tags:

android

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;
        }
    }
}
like image 887
Mahdi Rasti Avatar asked Nov 19 '25 18:11

Mahdi Rasti


1 Answers

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

like image 65
meda Avatar answered Nov 22 '25 07:11

meda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!