Android -- How to retrieve the Holder elements in a ListView Adapter
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 TextView开发者_StackOverflows 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.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.
精彩评论