如何在自定义适配器中将标签设置为行 Android Studio Java

How to set a tag to a row in a custom adapter Android Studio Java

我正在尝试为该行设置一个标签(或至少为该行中的某些内容,最好是该行)。然后当我点击该行时,我可以获得标签。我以为这是我必须做的,但无论我点击什么,标签总是空的。

我的代码示例展示了我尝试将标签设置到文本视图,因为我不知道如何将它设置到行。

这是我的 customAdapter 中的 getView 代码 class

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

这是我在另一个 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();

        }
    });

如有任何帮助,我们将不胜感激。谢谢

试试这个,

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

试试这个:

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