Onitemclick listener on getview method
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)ge开发者_StackOverflowtSystemService(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;
}
}
Try to use the setTag(Object o) and getTag() methods on TextView,it may help you I mean
tv_link.setTag(o);
inside the onClickListener,get that object using v.getTag();
Fields o=(Fields)v.getTag();
LocName.setText(o.getLocationName());
it may solve your problem.
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 .
精彩评论