如何获得视图列表视图中的android系统中的一个项目? [英] How to get view for an item in listview in android?

查看:116
本文介绍了如何获得视图列表视图中的android系统中的一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能基于其在适配器的位置,而不是在ListView中可见的意见得到一个项目的看法?

Is it possible to get an item view based on its position in the adapter and not in the visible views in the ListView?

我知道类似于getChildAt()和getItemIdAtPosition(功能),但它们提供基于内部的ListView可见视图的信息。我也知道,Android的回收意见,这意味着我只能在ListView可见意见工作。

I am aware of functions like getChildAt() and getItemIdAtPosition() however they provide information based on the visible views inside ListView. I am also aware that Android recycles views which means that I can only work with the visible views in the ListView.

我的目标是让每个项目的通用标识,因为我使用的CursorAdapter所以我没有计算项目的位置相对于有形的物品。

My objective is to have a universal identifier for each item since I am using CursorAdapter so I don't have to calculate the item's position relative to the visible items.

推荐答案

下面是我如何做到了这一点。在我的(自定义)适配器类:

Here's how I accomplished this. Within my (custom) adapter class:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View view = null;
  if (convertView == null) {
    LayoutInflater inflater = context.getLayoutInflater();
    view = inflater.inflate(textViewResourceId, parent, false);
    final ViewHolder viewHolder = new ViewHolder();
    viewHolder.name = (TextView) view.findViewById(R.id.name);
    viewHolder.button = (ImageButton) view.findViewById(R.id.button);

    viewHolder.button.setOnClickListener
      (new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        int position = (int) viewHolder.button.getTag();
        Log.d(TAG, "Position is: " +position);
      }
    });

    view.setTag(viewHolder);
    viewHolder.button.setTag(items.get(position));

  } else {
    view = convertView;
    ((ViewHolder) view.getTag()).button.setTag(items.get(position));
  }


  ViewHolder holder = (ViewHolder) view.getTag();

  return view;
}

从本质上讲,关键是要设置和检索通过 setTag getTag 方法中的位置索引。该项目变量是指的ArrayList 包含我自定义(适配器)的对象。

Essentially the trick is to set and retrieve the position index via the setTag and getTag methods. The items variable refers to the ArrayList containing my custom (adapter) objects.

另请参见的教程深入的例子。让我知道如果你需要我澄清什么。

Also see this tutorial for in-depth examples. Let me know if you need me to clarify anything.

这篇关于如何获得视图列表视图中的android系统中的一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆