Android 如何将 setTag 和 getTag 与自定义适配器一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23600599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use setTag and getTag with custom adapter
提问by Jonatan
I get stucked and I need help. I'm trying to use set and get Tag but i can't get how it works for this action:
我被卡住了,我需要帮助。我正在尝试使用 set 和 get Tag 但我不知道它是如何用于此操作的:
- I'm using list view to show images loaded to extended adapter
- The custom Adapter inflate a layout with imageview_1, textview_1 and button_1
- On my principal activity, I have a "Public Void OnClickHandler" for button_1 and was configurated on layout with "android:onClick", so when button is clicked it do something
- When button_1 is clicked, I want to get the text from textview_1 from that specific view and then load a different image. I want to to this using get and set TAGS, so I need to do the reference with button_1 and imageview_1. here my snipped code. Thank you in advance
- 我正在使用列表视图来显示加载到扩展适配器的图像
- 自定义适配器使用 imageview_1、textview_1 和 button_1 来膨胀布局
- 在我的主要活动中,我有一个用于 button_1 的“Public Void OnClickHandler”,并在布局上使用“android:onClick”进行配置,因此当单击按钮时,它会执行某些操作
- 单击 button_1 时,我想从该特定视图中获取 textview_1 中的文本,然后加载不同的图像。我想使用 get 和 set TAGS 来做到这一点,所以我需要用 button_1 和 imageview_1 做参考。这是我截取的代码。先感谢您
The custom Adapter
自定义适配器
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.itemstartsession, null);
holder = new ViewHolder();
holder.image = (WebView)convertView.findViewById(R.id.img_session);
//holder.image.setTag(position);
holder.code = (TextView)convertView.findViewById(R.id.code_item_session_text);
//holder.code.setTag(position);
holder.share=(ImageButton)convertView.findViewById(R.id.share_item_session_button);
holder.share.setTag(position);
convertView.setTag(holder);
// Check if my setTag is ok for button and get the reference to get
//text from textview and the referece to webview, then I gonna load a url
} else {
holder=(ViewHolder)convertView.getTag();
}
StoreDataForBA storeItem= (StoreDataForBA) getItem(position);
holder.image.loadUrl(storeItem.getImage());
holder.code.setText(storeItem.getCode());
return convertView;
}
This is my getter and setter for data, very easy
这是我的数据 getter 和 setter,非常简单
public StoreDataForBA( String image, String code) {
this.setImage(image);
this.setCode(code);
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
My principal Activity snipped
我的主要活动剪断了
public void shareOnClickHandler(View v) {
// plz here i need the code to get the text from textview and also get the
// reference of the webview, so i can do something like
// StoreDataForBA data = (StoreDataForBA)v.getTag();
// image2.loadUrl("http://image2")..... I'm not sure, thank you
}
采纳答案by Fahad
your code is little bit confusing, so I give you a sample
你的代码有点混乱,所以我给你一个例子
Sample Tag class
示例标签类
public class MyTag
{
String code;
String image;
String web_ref;
public MyTag()
{
code=null;
image=null;
web_ref=null;
}
public MyTag(String cod,String img,String wref)
{
code=cod;
image=img;
web_ref=wref;
}
}
you want to get this values when clicked on button right ? So put this tag class object as tag on button in getView of your custom adapter
您想在单击右侧按钮时获得此值吗?所以把这个标签类对象作为标签放在自定义适配器的 getView 按钮上
MyTag myTag=new MyTag("code","image","web_ref");
holder.button.setTag(myTag);
since you get the view clicked as argument to the your function
因为您将视图单击为函数的参数
public void shareOnClickHandler(View v)
{
myTag=(MyTag)v.getTag();
text=myTag.code;
image2.loadUrl("http://"+myTag.image);//..... I'm not sure, thank you
webview.loadUrl(mytag.web_ref);
}
I think you get the idea, try to implement your code with this idea
我想你明白了,试着用这个想法来实现你的代码
回答by Biraj Zalavadia
You are very close to your answer. Just Follow the changes and complete your answer
你非常接近你的答案。只需按照更改并完成您的答案
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if ((convertView == null) || (convertView.getTag() == null)) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setTag(holder);
return convertView;
}
回答by Shereef Marzouk
Answer 1:
答案 1:
What you want to do it:
你想做什么:
above or right after this line: holder.code.setText(storeItem.getCode());
add the following: holder.share.setTag(storeItem.getCode());
在此行上方或之后:holder.code.setText(storeItem.getCode());
添加以下内容:holder.share.setTag(storeItem.getCode());
and in the onClick:
并在 onClick 中:
public void shareOnClickHandler(View v) {
String code = v.getTag().toString();
// plz here i need the code to get the text from textview and also get the
// reference of the webview, so i can do something like
// StoreDataForBA data = (StoreDataForBA)v.getTag();
// image2.loadUrl("http://image2")..... I'm not sure, thank you
}
Or
或者
Answer 2:
答案 2:
You may not need to use setTag
and getTag
if you do the following
您可能不需要使用setTag
,getTag
如果您执行以下操作
- Go to your xml and remove the onclick attribute from the button we are going to use it in the Java instead
Use the following getView
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); if (convertView == null) { convertView = mInflater.inflate(R.layout.itemstartsession, null); holder = new ViewHolder(); holder.image = (WebView)convertView.findViewById(R.id.img_session); holder.code = (TextView)convertView.findViewById(R.id.code_item_session_text); holder.share=(ImageButton)convertView.findViewById(R.id.share_item_session_button); convertView.setTag(holder); // Check if my setTag is ok for button and get the reference to get //text from textview and the reference to webview, then I gonna load a url } else { holder=(ViewHolder)convertView.getTag(); } final StoreDataForBA storeItem= (StoreDataForBA) getItem(position); // final to use inside click holder.image.loadUrl(storeItem.getImage()); holder.code.setText(storeItem.getCode()); final ViewHolder fh = holder; // it needs to be final to use inside of clicklistener holder.share.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String text = holder.code.getText().toString(); // I hope that this is what you need. String text2 = storeItem.getCode(); //use either but I prefer this. } }); return convertView; }
- 转到您的 xml 并从我们将在 Java 中使用它的按钮中删除 onclick 属性
使用以下 getView
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); if (convertView == null) { convertView = mInflater.inflate(R.layout.itemstartsession, null); holder = new ViewHolder(); holder.image = (WebView)convertView.findViewById(R.id.img_session); holder.code = (TextView)convertView.findViewById(R.id.code_item_session_text); holder.share=(ImageButton)convertView.findViewById(R.id.share_item_session_button); convertView.setTag(holder); // Check if my setTag is ok for button and get the reference to get //text from textview and the reference to webview, then I gonna load a url } else { holder=(ViewHolder)convertView.getTag(); } final StoreDataForBA storeItem= (StoreDataForBA) getItem(position); // final to use inside click holder.image.loadUrl(storeItem.getImage()); holder.code.setText(storeItem.getCode()); final ViewHolder fh = holder; // it needs to be final to use inside of clicklistener holder.share.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String text = holder.code.getText().toString(); // I hope that this is what you need. String text2 = storeItem.getCode(); //use either but I prefer this. } }); return convertView; }