问题
In my application i have a listView. each item of the list view contains a clickable main body(image and some text) and 3 other textViews which are clickable.
when i click on the item it shows the map of the respective area shown on the item, this works fine because i am using onItemClick on the listview in my activity.
But when i want to implement the onClick on the 3 other TextViews marked red in the image i have to implement the onClick in the getView method of the adapter class.
Here comes the problem:
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder = new ViewHolder();
p = values.get(position);
String date = new java.text.SimpleDateFormat("dd/MM/yy")
.format(new java.util.Date(p.timeStamp));
if (vi == null) {
vi = inflater.inflate(R.layout.feed_items, null);
holder.text = (TextView) vi.findViewById(R.id.label);
holder.image = (ImageView) vi.findViewById(R.id.logo);
holder.thankLabel = (TextView) vi.findViewById(R.id.thankLabel);
holder.iwantLabel = (TextView) vi.findViewById(R.id.iWantLabel);
holder.detailsLabel = (TextView) vi
.findViewById(R.id.detailsLabel);
holder.thankLabel
.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
}
});
holder.iwantLabel
.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(activity,
IWantActivity.class);
intent.putExtra("productDetails", p.productName
+ "^" + p.reportedPrice);
activity.startActivity(intent);
}
});
holder.detailsLabel
.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
}
});
}
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
return vi;
}
in my activity when i want the reference of which item is clicked i can use the position parameter, but in case of getView the parameter position gives the newly created item but not the item's iWant i clicked. how to solve this??
回答1:
You can put tags on the labels, and use it as the context for the click handler.
来源:https://stackoverflow.com/questions/11559575/make-different-clickable-views-on-a-listview-item-in-android