0
| 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。

4

3 回答 3

1

这是像我这样的新手的答案......

<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 并获得了它被点击的位置,我使用了

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();        
}
于 2013-03-12T10:26:33.830 回答
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");


            }
        };
于 2013-03-08T10:38:32.507 回答
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);                                
        }
    })
于 2013-03-08T09:51:53.390 回答