简体   繁体   English

在Listview适配器中添加项目后notifyDataSetChanged()不起作用

[英]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() . 我有一个for循环,贯穿并添加所有县,在add county方法中,它将notifyDataSetChanged() However, this does not update the GUI . 但是,这不会更新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 在for循环中

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. 使用add方法将Country对象添加到countryAdapter的国家对象中。 Then, after for loop the line below: 然后,在for循环之后,下面的行:

countryAdapter.updateList(countries);

Here the "countries" list object passed in the updateList method, replace the countries list object of the countryAdapter class. 在这里,在updateList方法中传递的“国家”列表对象替换了countryAdapter类的国家列表对象。

You have to update main class "countries" object in the for loop. 您必须在for循环中更新主类“国家”对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用 notifyDataSetChanged 更新我的 listView 自定义列表适配器但不工作 - Trying to update my custom list adapter of listView using notifyDataSetChanged but not working notifyDataSetChanged ListView 适配器文件中的错误 - Error in notifyDataSetChanged ListView Adapter File ListView 适配器在添加项目时的工作 - Working of ListView Adapter on Adding Item notifyDataSetChanged()不适用于自定义适配器视图 - notifyDataSetChanged() not working with custom Adapter View 屏幕旋转适配器notifyDataSetChanged不起作用 - Screen rotation adapter notifyDataSetChanged not working 我的adapter.notifyDataSetChanged()不起作用 - My adapter.notifyDataSetChanged() is not working 尽管我使用了Adapter.notifyDataSetChanged(),但在更改数据集后,Android ListView不会刷新 - Android ListView not refreshing after dataset changed, though I have used the Adapter.notifyDataSetChanged() onbackpressed()后notifydatasetchanged()无法正常工作 - notifydatasetchanged() not working after onbackpressed() 在添加和编辑列表视图项时如何使用自定义适配器? - How to use custom adapter while adding and editing listview items? Adapter.notifyDataSetChanged()在onCreate()中不起作用 - Adapter.notifyDataSetChanged() not working inside onCreate()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM