Android——如何检索 ListView 适配器中的 Holder 元素

发布于 2024-11-08 11:07:17 字数 1850 浏览 0 评论 0原文

我编写了一个使用自定义 ArrayAdapter 和 Holders 的 ListActivity 类。我希望能够访问存储在每一行中的 TextView,但没有成功。这是相关的 ArrayAdapter 代码:

编辑:写了一个低效的 hack 来解决它


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;
}

我尝试存储 Holders (是否有多个?)并将 TextViews 和 CheckBoxes 存储在单独的数组中,但数组中的每个元素都是相同的元素。这 列表活动代码:


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(position)); } }

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.

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:

EDIT: 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;
}

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();

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


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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洒一地阳光 2024-11-15 11:07:17

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

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 小部件中检索它。

或者,您实际上根本不需要为此使用持有者,因为您拥有用于填充列表的原始数组:

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

后一种形式确实符合 ListView 更常用的方式 - 它正在显示持有的一些数据集在数组或游标等中;适配器负责将该数据转换为正确的表示形式以显示给用户;当单击某个项目时,您知道数据集中的哪个内容(位置和 ID)被单击,因此可以直接转到数据集来检索其值。

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

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;
}

And then when clicked:

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

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]);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文