notifyDataSetChanged () не работает после добавления элементов в адаптер Listview - PullRequest
0 голосов
/ 03 мая 2018

Я пытаюсь добавить элементы в список из результатов запроса. Тем не менее, список не будет обновляться. У меня есть цикл for, который проходит и добавляет все графства, а в методе add county он будет notifyDataSetChanged(). Однако это не обновляет GUI. Ниже приведен мой пример кода.

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

}

1 Ответ

0 голосов
/ 03 мая 2018

В цикле

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

Добавляет объект Country в объекте стран countryAdapter, используя метод add. Затем, после цикла for строка ниже:

countryAdapter.updateList(countries);

Здесь объект списка "стран", переданный в метод updateList, заменяет объект списка стран класса countryAdapter.

Вы должны обновить объект "страны" основного класса в цикле for.

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

...