getview 方法上的 Onitemclick 侦听器
我正在尝试实现打开一个自定义对话框,其中包含适配器列表中的相关信息。在这里我使用 onclicklistener,它工作正常,我得到了自定义对话框,我的问题是我没有得到正确的信息。如果我单击对话框中列表中的任何项目,它将显示最后一个项目的详细信息。
生成列表时,它显示 logcat 中的位置。但是当我尝试单击详细信息文本视图时,它占据了最后一个项目的位置。
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if(v == null){
LayoutInflater vl = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vl.inflate(R.layout.listItem, null);
}
Fields o = results.get(position);
if (o != null) {
TextView iv = (TextView)v.findViewById(R.id.toptext);
TextView tv_link = (TextView)v.findViewById(R.id.toptext1);
ImageView tv_Image = (ImageView)v.findViewById(R.id.Locimage);
tv_link.setText("Details >>");
tv_link.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.locationdetails);
dialog.setTitle("Title");
System.out.println("Position "+pos);
TextView LocName = (TextView) dialog.findViewById(R.id.LocDescName);
LocName.setText(o.getLocationName());
ImageView LocDescImage = (ImageView) dialog.findViewById(R.id.LocDescImage);
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(o.getLocationImage()).getContent());
LocDescImage .setImageBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dialog.show();
}
});
}
DbLoc.close();
return v;
}
}
I'm trying to implement to open a custom dialog box having related info from the adapter list. here I'm using onclicklistener, it is working fine i'm getting custom dialog box, my problem is i'm not getting the correct info. If i click on any item on the list in dialog box it is showing the last item details.
At the time of generating the list it is showing the positions in logcat. But when i'm trying to click on details textview it is taking the last item position.
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if(v == null){
LayoutInflater vl = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vl.inflate(R.layout.listItem, null);
}
Fields o = results.get(position);
if (o != null) {
TextView iv = (TextView)v.findViewById(R.id.toptext);
TextView tv_link = (TextView)v.findViewById(R.id.toptext1);
ImageView tv_Image = (ImageView)v.findViewById(R.id.Locimage);
tv_link.setText("Details >>");
tv_link.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.locationdetails);
dialog.setTitle("Title");
System.out.println("Position "+pos);
TextView LocName = (TextView) dialog.findViewById(R.id.LocDescName);
LocName.setText(o.getLocationName());
ImageView LocDescImage = (ImageView) dialog.findViewById(R.id.LocDescImage);
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(o.getLocationImage()).getContent());
LocDescImage .setImageBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dialog.show();
}
});
}
DbLoc.close();
return v;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在 TextView 上使用 setTag(Object o) 和 getTag() 方法,它可能会帮助你
我的意思是
在 onClickListener 内部,使用 v.getTag() 获取该对象;
它可能会解决你的问题。
Try to use the setTag(Object o) and getTag() methods on TextView,it may help you
I mean
inside the onClickListener,get that object using v.getTag();
it may solve your problem.
这是因为 tv_link.setOnClickListener 内部的 int:pos 没有被正确管理。
为什么你没有在这里添加与之相关的代码。
无论如何,现在如果通过 tv_link.setTag(your_pbject) 传递单个对象就足以满足您的要求,请执行它,否则创建内部类,该内部类将实现 View.onClickListener 并在为每个设置此 onclickListenet 时通过构造函数传递相关数据看法 。
This is because int:pos inside tv_link.setOnClickListener is not managed properly .
why you did not add code related to it here .
anyway now if passing single object by tv_link.setTag(your_pbject) will be enough as per your requirment , go through it , else create inner class which will implement View.onClickListener and pass related data through constructor at the time of setting this onclickListenet for each view .