如何从listview获取检查项目

时间:2014-02-04 15:55:02

标签: android listview checkbox

这是我的代码,从另一个活动接收数组列表并将其显示在列表视图中。

    public class ItemFavouriteList extends Activity{

    ListView favouritelist;
    LazyAdapter3 adapter;
    ArrayList<HashMap<String, String>> itemfavouriteALL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_favourite_list);

        favouritelist = (ListView) findViewById(R.id.listView1);
        itemfavouriteALL = ItemsView.itemfavourite;

        adapter = new LazyAdapter3(ItemFavouriteList.this,itemfavouriteALL);

        favouritelist.setAdapter(adapter);
    }
}

这是列表中每行的布局“xml”的代码。

  <?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="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!-- ListRow Left sied Thumbnail image -->


    <!-- Title Of Song -->

    <TextView
        android:id="@+id/item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="category name"
        android:textColor="#040404"
        android:textSize="25dip"
        android:textStyle="bold"
        android:typeface="sans" />

    <TextView
        android:id="@+id/itemId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/item_name"
        android:layout_marginTop="1dip"
        android:textColor="@android:color/transparent"
        android:textSize="10dip" />

    <!-- Artist Name -->

    <TextView
        android:id="@+id/item_desc"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/item_name"
        android:layout_marginTop="1dip"
        android:layout_toLeftOf="@+id/ck_final_lits"
        android:text="Just "
        android:textColor="#343434"
        android:textSize="20dip" />

    <!-- Rightend Duration -->


    <!-- Rightend Arrow -->

    <CheckBox
        android:id="@+id/ck_final_lits"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/item_name"
        android:layout_alignBottom="@+id/item_name"
        android:layout_alignParentRight="true"
        android:text="@string/empty" />

    <TextView
        android:id="@+id/item_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/item_desc"
        android:layout_alignBottom="@+id/item_desc"
        android:layout_alignParentRight="true"
        android:text="@string/wwww1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

这是我正在使用的适配器。

    public class LazyAdapter3 extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;

    public LazyAdapter3(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_item3, null);

        TextView itemid = (TextView)vi.findViewById(R.id.itemId);
        TextView itemname = (TextView)vi.findViewById(R.id.item_name); 
        TextView itemdesc = (TextView)vi.findViewById(R.id.item_desc); 
        TextView itemprice = (TextView)vi.findViewById(R.id.item_price);

        HashMap<String, String> showdata = new HashMap<String, String>();
        showdata = data.get(position);

        // Setting all values in listview
        itemid.setText(showdata.get(ItemsView.TAG_ITEM_ID));
        itemname.setText(showdata.get(ItemsView.TAG_ITEM_NAME_EN));
        itemdesc.setText(showdata.get(ItemsView.TAG_ITEM_DESC));
        itemprice.setText(showdata.get(ItemsView.TAG_ITEM_PRICE));
        return vi;
    }
}

如何获取每个选中复选框的商品ID?

1 个答案:

答案 0 :(得分:0)

在CheckBox上添加OnCheckedChangeListener,您可以在getView中执行此操作。然后创建一个ArrayList并将checked / unchecked项添加/删除到新的ArrayList。