1

我用谷歌搜索,我试图做到这一点,但无法成功实施。

我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:id="@android:id/list"></ListView>
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal"
        android:text="buyButtonClickProduct" 
        android:id="@+id/buyButtonClickProduct"></Button>
</LinearLayout>

我有一个自定义适配器:

private final List<Products> list;
private final Activity context;

public ProductsCustomAdapter(Activity context, List<Products> list) {
    super(context, R.layout.coposite_product_info, list);
    this.context = context;
    this.list = list;
}

static class ViewHolder {
    protected TextView productInfoText;
    protected CheckBox checkbox;
    protected ImageView imageOfProduct;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.coposite_product_info, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.imageOfProduct = (ImageView) view.findViewById(R.id.productImageView);
        viewHolder.productInfoText = (TextView) view.findViewById(R.id.productInfoText);
        viewHolder.checkbox = (CheckBox) view.findViewById(R.id.productCheck);
        viewHolder.checkbox
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        Products element = (Products) viewHolder.checkbox
                                .getTag();
                        element.setChecked(buttonView.isChecked());

                    }
                });
        view.setTag(viewHolder);
        viewHolder.checkbox.setTag(list.get(position));
    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.productInfoText.setText(list.get(position).getInformation());
    holder.imageOfProduct.setImageBitmap(list.get(position).getProductImage());
    holder.checkbox.setChecked(list.get(position).isChecked());
    return view;
}

我想从这些检查项目,但我无法接触到它们。当我在我的主类上调试它时,它listview.getCheckedItemPositions();总是为我转“null”

这实际上并没有崩溃,只是总是显示“null”,但我可以在我的列表视图中看到我的所有数据。

SparseBooleanArray checkedItems = shopListView.getCheckedItemPositions();
4

0 回答 0