如何更新gridview中的每个项目

时间:2014-10-14 09:24:00

标签: android gridview

我想更新GridView中的每个项目

GridView

更新后的

将是这样的

Gridview after update

我试过查看tempviewi = gridviewAdapteri.getView(i,null,gridview),然后在(TextView)tempviewi.findViewById(R.id.txtItem)之后可以读取textview它的确定,但是当我setText没有什么

这是我的自定义适配器

 @Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View itemView = convertView;
    ViewHolder holder = null;

    if (itemView == null)
    {
        final LayoutInflater layoutInflater =
                (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemView = layoutInflater.inflate(resourceId, parent, false);

        holder = new ViewHolder();
        holder.imgItem = (ImageView) itemView.findViewById(R.id.MainPic);
        holder.imgInd = (ImageView) itemView.findViewById(R.id.PowerInd);
        holder.txtItem = (TextView) itemView.findViewById(R.id.txtItem);
        holder.txtAddress = (TextView)itemView.findViewById(R.id.texthide);

        itemView.setTag(holder);

    }
    else
    {
        holder = (ViewHolder) itemView.getTag();
    }


    TitoRow item = getItem(position);

    holder.imgItem.setImageDrawable(item.getImage());

    holder.txtItem.setText(item.getTitle());

    holder.txtAddress.setText(item.getAddress());

    holder.imgInd.setImageDrawable(item.GetInd());

    return itemView;
}

3 个答案:

答案 0 :(得分:0)

修改项目标题并在适配器上调用 notifyDataSetChanged()

编辑:

ArrayList<Item> itemList = new ArrayList<Item>();
itemList.add(new Item("title 1"));
itemList.add(new Item("title 2"));

YourCustomAdapter adapter = new YourCustomAdapter(getActivity(), R.layout.your_layout_item, itemList);
gridView.setAdapter(adapter);

//...

//modify item
itemList.get(0).setTitle("New Title");
//call notifyDataSetChanged
adapter.notifyDataSetChanged();

答案 1 :(得分:0)

每次要更新gridview时,都可以使用notifyDataSetChanged()。

答案 2 :(得分:0)

为标题和图像等网格项创建一个模型类

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public int getImage() {
    return image;
}

public void setImage(int image) {
    this.image = image;
}

然后在您的ArrayAdapter GetView方法中添加此

    YourArrayAdapterClass item = data.get(position);
    holder.txtTitle.setText(item.getTitle());
    holder.ivGrid.setImageResource(item.getImage());
    return row;