Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView的CustomAdapter中的复选框_Android_Listview - Fatal编程技术网

Android ListView的CustomAdapter中的复选框

Android ListView的CustomAdapter中的复选框,android,listview,Android,Listview,我有一个带有自定义适配器的ListView。现在,我为每个元素添加了一个复选框,以获得“多重检查列表视图”,但代码如下: import java.util.ArrayList; class CustomListAdapter extends ArrayAdapter<String> { String doubleTab = "\t\t"; CheckBox elementChecker; ArrayList<Boolean> isChecked;

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

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;
    }

}
import java.util.ArrayList;
类CustomListAdapter扩展了ArrayAdapter{
字符串doubleTab=“\t\t”;
复选框元素检查器;
对ArrayList进行了检查;
ArrayList checkedPositions=新建ArrayList();
公共CustomListAdapter(上下文上下文,字符串[]dataListFinal){
super(上下文,R.layout.list\u item\u datalist,datalistfail);
}
公共字符串等位基因dapter=“”;
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
LayoutInflater iteminflater=LayoutInflater.from(getContext());
View customView=iteminflater.inflate(R.layout.list\u item\u datalist,父项,false);
ImageView image=(ImageView)customView.findViewById(R.id.list\u icon\u product);
TextView textViewlabel=(TextView)customView.findViewById(R.id.list\u item\u datalist\u label\u TextView);
TextView textViewdetails1=(TextView)customView.findViewById(R.id.list\u item\u datalist\u TextView\u details\u 1);
elementChecker=(复选框)customView.findViewById(R.id.CheckBox\u项);
字符串singleListItem=getItem(位置);
字符串[]singleListItemArray=singleListItem.split(“\t”);
字符串id=singleListItemArray[0];
String product=singleListItemArray[1];
字符串标签=singleListItemArray[2];
AllegmentsAdapter=产品+标签+序列号+mac+数据辅助+账单;
交换机(产品){
案例“Pc”:
image.setImageResource(R.drawable.icon\u pc\u圆圈);
打破
“笔记本电脑”案例:
image.setImageResource(R.drawable.icon\u laptop\u circle);
打破
}
字符串details1=产品;
textViewlabel.setText(标签);
textViewdetails1.setText(details1);
elementChecker.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(elementChecker.isChecked()){
Log.d(“选定”、“是”);
}否则{
日志d(“选定”、“否”);
}
}
});
返回自定义视图;
}
公共字符串getAllegements(){
返回等位基因;
}
}

OnClickListener
只监视最后一个条目。有人能帮我解释一下我必须做什么吗?

你必须声明
CheckBox-elementChecker在getView内部

您总是覆盖
元素检查器

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

这将是一个解决办法。但是您必须将复选框的状态保存到某个地方…

您所说的“OnClickListener仅监视最后一个条目”是什么意思;在getViewBut中,您面临的问题是什么?您没有说?@MurtazaKhursheedHussain如果我检查/取消检查其中一个元素,我只会得到最后一个元素的日志消息。如果取消选中最后一个元素,则日志条目始终显示:“SELECTED no”(选择的否),如果选中,则无论我是否选中或取消选中getView方法中的另一个元素Declare Checbox,日志条目始终显示:“SELECTED yes”(选择的是)。这是空虚的开始,伙计,就是这样!但是你能告诉我为什么吗?是不是因为在我的“版本”中没有为每个元素创建复选框?这是下一步;)