访问listview适配器的某些元素

时间:2015-06-16 08:42:52

标签: java android listview

我有一个带ListView的Activity。此ListView使用类GetAllEntrysListViewAdapter

进行设置
 @Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final ListCell cell;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.get_all_entry_list_view_cell, null);

        cell = new ListCell();
        cell.note = (TextView) convertView.findViewById(R.id.listViewNote);
        cell.img = (ImageView) convertView.findViewById(R.id.listViewImg);
        cell.likeButton = (ImageButton) convertView.findViewById(R.id.heartImage);

        convertView.setTag(cell);
    }
    else {
        cell = (ListCell)convertView.getTag();
    }

 public static class ListCell {
    private TextView note;
    private ImageView img;
    public ImageButton likeButton;
}

在活动中,我现在想要更改单击的某个列表条目上的likeButton的图像,但是如何才能到达此特定元素?她就是我现在所拥有的:

 GetAllEntrysListViewAdapter.ListCell listCell;
 listCell = new GetAllEntrysListViewAdapter.ListCell();

   getALlEntrysListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// Get the likeButton of this entry and change the Image

2 个答案:

答案 0 :(得分:0)

该部分应该在getView()方法本身中完成。它应该像这样工作。

  1. 在适配器中设置数据
  2. 在getView中为每个项目显示数据
  3. 一旦您喜欢任何项目,请使用新的状态
  4. 更新数据
  5. 现在在您的适配器上调用notifyDatasetChanged()getView()将相应地更新项目视图。
  6. 您应首先阅读ListAdapter,并查看一些示例,了解其工作原理。

答案 1 :(得分:0)

试试这段代码。替换你的

getALlEntrysListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

@Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    // Get the likeButton of this entry and change the Image
}

public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    //first get the elements by doing like
    TextView note = (TextView)view.findViewById(R.id.listViewNote);
    ImageView img = (ImageView) view.findViewById(R.id.listViewImg);
    ImageButton likeButton = (ImageButton) convertView.findViewById(R.id.heartImage);

    likeButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            likeButton.setBackgroundResource(R.id.icon);
        }
    });
}

这将有所帮助......谢谢