gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:50:48 26 4
gpt4 key购买 nike

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

我的代码示例显示了我尝试将标签设置为 TextView ,因为我不知道如何将其设置为行。

这是我的 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;
}

这是我在不同类中的 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;
}

关于java - 如何在自定义适配器 Android Studio Java 中将标签设置为行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43857879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com