简体   繁体   中英

notifyDataSetChanged() not working after adding items in Listview adapter

I am trying to add items to a list view from the results of a request. However, the list will not update. I have a for loop that runs through and adds all the counties and in the add county method it will notifyDataSetChanged() . However, this does not update the GUI . Below is my sample code.

            new Response.Listener<JSONArray>() {

                // Takes the response from the JSON request
                @Override
                public void onResponse(JSONArray response) {
                    try {
                        for (int i = 0; i < response.length(); i++) {
                            JSONObject countryObject = response.getJSONObject(i);;
                            String flagURI= ""+countryObject.getString("flag");
                            String studentOne = ""+countryObject.getString("studentOne");
                            String studentTwo = ""+countryObject.getString("studentTwo");
                            String teacher = ""+ countryObject.getString("teacher");
                            String name = ""+ countryObject.getString("Name");
                            rez=rez+""+countryAdapter.getCount();
                            countryAdapter.add(new Country(name, teacher, studentOne, studentTwo,0,0,flagURI));;
                        }
                        tvName = (TextView) findViewById( R.id.tvName );
                        tvName.setText( "one");
                        countryAdapter.updateList(countries);
                        tvName.setText( rez);
                    }
                    // Try and catch are included to handle any errors due to JSON
                    catch (JSONException e) {
                        // If an error occurs, this prints the error to the log
                        tvName = (TextView) findViewById( R.id.tvName );
                        tvName.setText( "BAD NEWS1" + e);
                        e.printStackTrace();
                    }
                    catch (Exception e) {
                        tvName = (TextView) findViewById( R.id.tvName );
                        tvName.setText( "BAD NEWS3" + e);
                    }
                    }
            },


 public class CountryAdapter extends BaseAdapter {
    LayoutInflater mInflater;
    String score;
    ArrayList<Country> countries;


    public CountryAdapter(Context c,Country[] country) {
        this.countries = new ArrayList<Country>( Arrays.asList(country));;
        mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void updateList(Country[] data) {
        this.countries = new ArrayList<Country>( Arrays.asList(data));;
        this.notifyDataSetChanged();
    }
    public void add(Country data) {
        this.countries.add(data);
    }

}

In the for loop

countryAdapter.add(new Country(name, teacher, studentOne, studentTwo,0,0,flagURI));;

It add the Country object in the countries object of countryAdapter using add method. Then, after for loop the line below:

countryAdapter.updateList(countries);

Here the "countries" list object passed in the updateList method, replace the countries list object of the countryAdapter class.

You have to update main class "countries" object in the for loop.

countries.add(new Country(name, teacher, studentOne, studentTwo,0,0,flagURI));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM