CustomAdapter中的CheckBox for ListView

时间:2015-12-17 11:23:37

标签: android listview

我有一个带有自定义适配器的ListView。现在我为每个元素添加了一个CheckBox以获得“多重检查列表视图”,但使用以下代码:

import java.util.ArrayList;

class CustomListAdapter extends ArrayAdapter<String> {
    String doubleTab = "\t\t";
    CheckBox elementChecker;
    ArrayList<Boolean> isChecked;

    ArrayList<Integer> checkedPositions = new ArrayList<Integer>();

public CustomListAdapter(Context context, String[] dataListFinal) {
        super(context, R.layout.list_item_datalist ,dataListFinal);
    }
    public String allElementsAdapter = "";

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater iteminflater = LayoutInflater.from(getContext());
        View customView = iteminflater.inflate(R.layout.list_item_datalist, parent, false);

        ImageView image = (ImageView) customView.findViewById(R.id.list_icon_product);
        TextView textViewlabel = (TextView) customView.findViewById(R.id.list_item_datalist_label_textview);
        TextView textViewdetails1 = (TextView) customView.findViewById(R.id.list_item_datalist_textview_details_1);
        elementChecker = (CheckBox) customView.findViewById(R.id.checkBox_Item);

        String singleListItem = getItem(position);
        String[] singleListItemArray = singleListItem.split("\t");
        String id = singleListItemArray[0];
        String product = singleListItemArray[1];
        String label = singleListItemArray[2];



        allElementsAdapter = product + label + serial + mac + daaid + bill;

        switch (product) {
            case "Pc":
                image.setImageResource(R.drawable.icon_pc_circle);
                break;
            case "Laptop":
                image.setImageResource(R.drawable.icon_laptop_circle);
                break;

        }

        String details1 = product;

        textViewlabel.setText(label);
        textViewdetails1.setText(details1);

        elementChecker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (elementChecker.isChecked()){
                    Log.d("SELECTED", "yes");
                }else {
                    Log.d("SELECTED", "no");
                }
            }
        });

        return customView;
    }

    public String getAllElements(){
        return allElementsAdapter;
    }

}

OnClickListener仅观看最后一个条目。有人可以帮助我解释我必须做些什么吗?

2 个答案:

答案 0 :(得分:1)

您必须在getView

中声明CheckBox elementChecker;

答案 1 :(得分:1)

您始终会覆盖elementChecker

    Checkbox elementChecker = (CheckBox) customView.findViewById(R.id.checkBox_Item);

将是一个解决方案。但是你必须在某个地方保存复选框的状态......