| Icon(image) | Title(text) | cross(image) |
| | Description(text)| |
| | | coupon(image) |
列表视图。 在这里,我希望获得不同项目的ID,当在列表视图中单独单击,如交叉,优惠券,图标点击,然后我将得到他们的ID ... 我是新手......请帮帮我....
<?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>
我知道如何获取点击列表的ID。我想知道如何获取列表中单击项目的项目ID。
答案 0 :(得分:1)
以下是像我这样的NewBies的答案......
<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" />
我使用了onClick on item并获得了我点击过的位置
final int position = listView.getPositionForView((View) v.getParent());
完整代码: -
public void onCrossClick(View v) {
final int position = listView.getPositionForView((View) v.getParent());
Toast.makeText(this, "click on button " + position, Toast.LENGTH_LONG)
.show();
}
答案 1 :(得分:0)
以下是为列表视图设置侦听器的代码,该侦听器将捕获列表项选择事件。
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);
}
})
答案 2 :(得分:0)
检查以下示例代码:
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");
}
};