[英]How to set a tag to a row in a custom adapter Android Studio Java
I am trying to set a tag to the row (or at least something in the row, preferably the row though). 我正在尝试为行设置标签(或至少在行中设置某些内容,但最好是该行)。 Then when I click on the row, I can get the tag. 然后,当我单击该行时,可以获得标签。 I thought that this is what I have to do, but the tag is always null, regardless of what I tap on. 我以为这是我要做的,但是无论我点击什么,该标记始终为null。
My code example shows my attempt at setting a tag to a textview, as I have no idea how to set it to the row. 我的代码示例显示了我尝试将标签设置为textview的尝试,因为我不知道如何将其设置为该行。
This is my getView code in my customAdapter class 这是我的customAdapter类中的getView代码
int counter = 0;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View customView = layoutInflater.inflate(R.layout.my_row, parent, false);
ViewHolder holder = new ViewHolder();
holder.name = (TextView)customView.findViewById(R.id.nameTextView);
holder.picture = (ImageView)customView.findViewById(R.id.pictureImageView);
holder.name.setText(allNames.get(position));
holder.picture.setImageResource(allPictures.get(position));
//This does not work ---> holder.name.setTag(counter);
counter ++;
return customView;
}
This is my onClickListener in a different class 这是我在另一个类中的onClickListener
listVIew.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l) {
Toast.makeText(getApplicationContext(), "tag is " + view.getTag(), Toast.LENGTH_SHORT).show();
}
});
Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。
Try this, 尝试这个,
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View customView = layoutInflater.inflate(R.layout.my_row, parent, false);
ViewHolder holder = new ViewHolder();
holder.name = (TextView)customView.findViewById(R.id.nameTextView);
holder.picture = (ImageView)customView.findViewById(R.id.pictureImageView);
holder.name.setText(allNames.get(position));
holder.picture.setImageResource(allPictures.get(position));
//This does not work ---> holder.name.setTag(counter);
customView.setTag(Integer.valueOf(counter));
counter ++;
return customView;
}
Try this: 尝试这个:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
// inflate the layout
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
convertView = layoutInflater.inflate(R.layout.my_row, parent, false);
// set up the ViewHolder
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.nameTextView);
holder.picture = (ImageView) convertView.findViewById(R.id.pictureImageView);
// store the holder with the view.
convertView.setTag(holder);
} else {
// we've just avoided calling findViewById() on resource everytime
// just use the viewHolder
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(allNames.get(position));
holder.picture.setImageResource(allPictures.get(position));
holder.name.setTag(counter);
counter++;
return convertView;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.