Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Listview适配器中添加项后notifyDataSetChanged()不工作_Java_Android_Listview_Adapter - Fatal编程技术网

Java 在Listview适配器中添加项后notifyDataSetChanged()不工作

Java 在Listview适配器中添加项后notifyDataSetChanged()不工作,java,android,listview,adapter,Java,Android,Listview,Adapter,我正在尝试将请求结果中的项目添加到列表视图中。但是,该列表将不会更新。我有一个for循环,它贯穿并添加所有县,在addcounty方法中,它将notifydatasetchange()。但是,这不会更新GUI。下面是我的示例代码 new Response.Listener<JSONArray>() { // Takes the response from the JSON request @Ov

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

}
newresponse.Listener(){
//获取JSON请求的响应
@凌驾
公共void onResponse(JSONArray响应){
试一试{
对于(int i=0;i
在for循环中

countryAdapter.add(new Country(name, teacher, studentOne, studentTwo,0,0,flagURI));;
它使用add方法在countryAdapter的countries对象中添加Country对象。 然后,在for loop之后,在下面的行中:

countryAdapter.updateList(countries);
在这里,updateList方法中传递的“countries”列表对象替换countryAdapter类的countries列表对象

您必须更新for循环中的主类“countries”对象

countries.add(新国家(姓名、教师、学生姓名、学生姓名、0,0、flagURI))

适配器中的
getview()
overide方法在哪里?