问题
| Icon(image) | Title(text) | cross(image) |
| | Description(text)| |
| | | coupon(image) |
Its a list view. Here i want to get id of different items when clicked separately in list view like cross, coupon, icon is clicked then i will get their id... I'm a newbie...Please help me out....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/list_item_iv_icon"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="@string/app_name"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<ImageView
android:id="@+id/list_item_iv_icon_cross"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:clickable="true"
android:contentDescription="@string/app_name"
android:src="@drawable/cross_selector" />
<TextView
android:id="@+id/list_item_tv_title"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_toRightOf="@+id/list_item_iv_icon"
android:gravity="left"
android:textColor="#CC0033"
android:textIsSelectable="false"
android:textSize="20sp" />
<TextView
android:id="@+id/list_item_tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/list_item_tv_title"
android:layout_toRightOf="@+id/list_item_iv_icon"
android:gravity="left"
android:textColor="#3399FF"
android:textIsSelectable="false"
android:textSize="14sp" />
<ImageView
android:id="@+id/list_item_iv_type"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignBottom="@id/list_item_iv_icon"
android:layout_alignParentRight="true"
android:contentDescription="@string/app_name" />
</RelativeLayout>
I know how to get id of the clicked list. i want to know is how to get item id of a clicked item in list.
回答1:
Here is the answer for NewBies like Me...
<ImageView
android:id="@+id/list_item_iv_icon_cross"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:clickable="true"
android:contentDescription="@string/app_name"
android:src="@drawable/cross_selector"
android:onClick="onCrossClick" />
i used onClick on item and to get position in which it has been clicked i used
final int position = listView.getPositionForView((View) v.getParent());
full code :-
public void onCrossClick(View v) {
final int position = listView.getPositionForView((View) v.getParent());
Toast.makeText(this, "click on button " + position, Toast.LENGTH_LONG)
.show();
}
回答2:
The following is the code to set a listener for the list view which would capture the list item selection event.
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> list, View view, int position,
long id) {
// To get the item from the list use the position value
Object yourObject = yourAdapter.getItem(position);
}
})
回答3:
Check below Sample code :
public class ListViewAdapter_test extends BaseAdapter {
private LayoutInflater mInflater;
public ListViewAdapter_test(Context con) {
// TODO Auto-generated constructor stub
mInflater = LayoutInflater.from(con);
}
public int getCount() {
// TODO Auto-generated method stub
return a_product_id.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
// return product_id1.size();
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
// return product_id1.get(position).hashCode();
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
final ListContent holder;
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.scan_row1, null);
holder = new ListContent();
holder.name = (TextView) v.findViewById(R.id.sc_textname);
holder.name1 = (TextView) v.findViewById(R.id.sc_review);
holder.ratings = (RatingBar) v.findViewById(R.id.sc_ratingBar1);
holder.total_rate = (Button) v.findViewById(R.id.button1);
holder.img_p = (ImageView) v.findViewById(R.id.image_prod);
// holder.total_rate.setOnClickListener(mOnTitleClickListener1);
v.setTag(holder);
} else {
holder = (ListContent) v.getTag();
}
holder.total_rate.setOnClickListener(mOnTitleClickListener3);
holder.img_p.setOnClickListener(mOnTitleClickListener_image);
return v;
}
}
static class ListContent {
ImageView img_p;
TextView name1;
TextView name;
RatingBar ratings;
Button total_rate;
}
public OnClickListener mOnTitleClickListener3 = new OnClickListener() {
public void onClick(View v) {
final int position = list_v
.getPositionForView((View) v.getParent());
Log.d("you are click on Ratings","you are click on Ratings");
}
};
public OnClickListener mOnTitleClickListener_image = new OnClickListener() {
public void onClick(View v) {
final int position = list_v
.getPositionForView((View) v.getParent());
Log.d("you are click on image view","you are click on image view");
}
};
来源:https://stackoverflow.com/questions/15290360/remove-id-of-item-clicked-in-list-view