Android的 - 如何检索在ListView适配器持有人元素 [英] Android -- How to retrieve the Holder elements in a ListView Adapter

查看:112
本文介绍了Android的 - 如何检索在ListView适配器持有人元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了使用自定义ArrayAdapter和持有人ListActivity类。我希望能够访问的TextView的存储每一行​​,但都没有成功。下面是相关的ArrayAdapter code:

I have written a ListActivity class that uses a custom ArrayAdapter and Holders. I want to be able to access the TextView's stored in each row, but have been unsuccessful. Here's the pertinent ArrayAdapter code:

编辑:写了一个低效率的黑客来解决它。

wrote an inefficient hack to solve it


class SuperNewAdapter extends ArrayAdapter {

public View getView(int position, View convertView, ViewGroup parent) {
  // RowHolder is a simple inner class that stores two TextViews and a CheckBox
  RowHolder holder = new RowHolder();
  if (convertView == null) {
    convertView = layoutInflater.inflate(R.layout.listrow, null);
    holder.titleText = (TextView) convertView.findViewById(R.id.textTitle);
    holder.bottomText = (TextView) convertView.findViewById(R.id.textDescription;
    holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
    convertView.setTag(holder);
} else {
    holder = (RowHolder) convertView.getTag()
}
  holder.titleText.setText(stringArr1[position]);
  holder.bottomText.setText(stringArr2[position]);

  // Array to futilely attempt to get a reference to each textView
  titleTextArr.add(holder.titleText);
  /////////////////HACK ATTACK! rows is an ArrayList of Holders
  boolean flag=true;
  for (int i = 0; i < rows.size(); i++) {
    if (rows.get(i) == holder)
    flag=false;
   }
  if (flag) rows.add(holder);

  return convertView;
}

我试图存储所述座(是有一个以上的)并也贮存在单独的阵列中TextViews和复选框,但是阵列中的每个元素是相同的元件。该
ListActivity code:

I have tried storing the Holders (is there more than one?) and also storing the TextViews and CheckBoxes in separate arrays, but each element in the array is the same element. The ListActivity code :


public class SuperNewListActivity extends ListActivity {
  //used in the Adapter's getView() to store elements
  public ArrayList titleTextArr = new ArrayList();

公共无效onListItemClick(视图V,INT位置,长的id){
  //始终打印同样的事情,而位置而发生变化
  的System.out.println(titleTextArr.get(位置));
  }
}

public void onListItemClick(View v, int position, long id) { // always prints the same thing while position varies System.out.println(titleTextArr.get(position)); } }


我用convertView.getTag(位置),不果也试过。我如何获得这些部件这样我就可以对其进行修改?我需要我的小部件。小部件。小部件。感谢您寻找。

I also tried using convertView.getTag(position), to no avail. How do I get those widgets so I can modify them? I need my widgets. widgets. widgets. Thanks for looking.

推荐答案

下面应该可以正常工作。从你的getView()方法:

The following should work fine. From your getView() method:

类SuperNewAdapter扩展ArrayAdapter {

class SuperNewAdapter extends ArrayAdapter {

public View getView(int position, View convertView, ViewGroup parent) {
  // RowHolder is a simple inner class that stores two TextViews and a CheckBox
  RowHolder holder;
  if (convertView == null) {
    convertView = layoutInflater.inflate(R.layout.listrow, null);
    holder = new RowHolder();
    holder.titleText = (TextView) convertView.findViewById(R.id.textTitle);
    holder.bottomText = (TextView) convertView.findViewById(R.id.textDescription;
    holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
    convertView.setTag(holder);
  } else {
      holder = (RowHolder) convertView.getTag()
  }
  holder.titleText.setText(stringArr1[position]);
  holder.bottomText.setText(stringArr2[position]);
  return convertView;
}

然后单击时:

public void onListItemClick(View v, int position, long id) {
  Log.i("foo", "Title text: " + ((RowHolder)v.getTag).titleText.getText());
}

如果你只在乎文字,你可以直接在RowHolder存储为一个字符串,所以你并不需要从TextView的部件进行检索。

If you just care about the text, you could store that directly in the RowHolder as a String so you don't need to retrieve it from the TextView widget.

或者您真的不需要在所有使用的持有人对于这一点,因为你原来的数组,你用来填充列表:

Or alternatively you really don't need to use the holder at all for this, since you have the original array you used to populate the list:

public void onListItemClick(View v, int position, long id) {
  Log.i("foo", "Title text: " + stringArr1[position]);
}

这后一种形式真正相匹配的ListView是如何更常用的 - 它显示在一个数组或光标或持有这样的一些数据集;适配器负责将这些数据转化为正确的重新presentation展示给用户;单击某个项目时,你就知道这件事的数据集(无论是位置和id)被点击等都可以直接前往该数据集来检索它的值。

This latter form really matches how ListView is more often used -- it is displaying some data set held in an array or cursor or such; the Adapter is responsible for transforming that data into the correct representation to show to the user; when an item is clicked, you know which thing in the data set (both the position and id) was clicked so can just go directly to the data set to retrieve its value.

这篇关于Android的 - 如何检索在ListView适配器持有人元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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