【问题标题】:notifyDataSetChanged() not working after adding items in Listview adapter在 Listview 适配器中添加项目后 notifyDataSetChanged() 不起作用
【发布时间】:2018-05-03 05:25:42
【问题描述】:

我正在尝试根据请求的结果将项目添加到列表视图。但是,该列表不会更新。我有一个贯穿并添加所有县的 for 循环,在添加县方法中它将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);
    }

}

【问题讨论】:

  • 适配器中的getview() 覆盖方法在哪里?

标签: java android listview adapter


【解决方案1】:

在for循环中

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

它使用 add 方法将 Country 对象添加到 countryAdapter 的国家对象中。 然后,在 for 循环之后,下面的行:

countryAdapter.updateList(countries);

这里是updateList方法中传入的“国家”列表对象,替换countryAdapter类的国家列表对象。

您必须在 for 循环中更新主类“国家”对象。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 2015-02-27
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多