更改值时GridView项不会更改

时间:2014-02-12 08:47:41

标签: java android gridview arraylist

我有这个课程

public class Item {
    String image;
    String accesslevel;
    String username;
    String userjob;
    String usercompany;
    String usermail;
    Boolean etat;
    int userapartid;
    public Item(String image, String username, String userjob, String usercompany, String usermail,int userapartid,Boolean etat,String accesslevel) {
        super();
        this.userapartid=userapartid;
        this.image = image;
        this.username = username;
        this.userjob = userjob;
        this.usercompany = usercompany;
        this.usermail = usermail;
        this.etat=etat;
        this.accesslevel=accesslevel;
    }
    public String getImage() {
        return image;
    }
    public void setImage(String image) {
        this.image = image;
    }
    public String getusername() {
        return username;
    }
    public void setusername(String username) {
        this.username = username;
    }
    public String getusercompany() {
        return usercompany;
    }
    public void setusercompany(String usercompany) {
        this.usercompany = usercompany;
    }
    public String getuserjob() {
        return userjob;
    }
    public void setuserjob(String userjob) {
        this.userjob = userjob;
    }
    public String getusermail() {
        return usermail;
    }
    public void setusermail(String usermail) {
        this.usermail = usermail;
    }
    public Boolean getetat() {
        return etat;
    }
    public void setetat(Boolean etat) {
        this.etat = etat;
    }

    public int getuserapartid() {
        return userapartid;
    }
    public void setuserapartid(int userapartid) {
        this.userapartid = userapartid;
    }


    public String getaccesslevel() {
        return accesslevel;
    }
    public void setaccesslevel(String accesslevel) {
        this.accesslevel = accesslevel;
    }

}

我的主应用程序中有一个由Item填充的GridView和一个包含该项目的arraylist

gridArray2 = new ArrayList<Item>();

for(int i=0;i<gridArray.size();i++){                                             
     __Item = new Item(gridArray.get(i).getImage(),gridArray.get(i).getusername(),gridArray.get(i).getuserjob(),gridArray.get(i).getusercompany(),gridArray.get(i).getusermail(),gridArray.get(i).getuserapartid(),gridArray.get(i).getetat(),gridArray.get(i).getaccesslevel());



gridArray2.add(__Item);

       }
 customGridAdapter = new CustomGridViewAdapter(MainActualite.this, R.layout.row_grid_access, gridArray2);
gridView.setAdapter(customGridAdapter);

我的问题是当我这样做时:

gridView2.get(2).setetat(true);

项目中etat的值不会更改。我该怎么做才能改变arraylist中项目的价值?

谢谢!

2 个答案:

答案 0 :(得分:0)

当您更改提供给适配器的数据时(在这种情况下,您正在更新GridArray2提供数据的customGridAdapter),您始终需要确保调用{{1让notifyDataSetChanged()的适配器知道底层数据集已经改变。

有关此功能的更多信息,请访问:http://developer.android.com/reference/android/widget/ArrayAdapter.html#notifyDataSetChanged()

答案 1 :(得分:0)

基本上,要更新gridview中的数据,您必须更新数据对象(可以是数组,列表或其他内容。但通常是列表)。更改内容(例如,列表)后,您必须致电customGridAdapter.notifyDataSetChanged();

无论如何,您无需重新实例化适配器。