Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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中选中checbox,如何更改复选框背景_Android - Fatal编程技术网

如果我们在android中选中checbox,如何更改复选框背景

如果我们在android中选中checbox,如何更改复选框背景,android,Android,这是我的课 public class AdapterContact extends BaseAdapter { private Context context; private ArrayList<Datamodel> arrModel; public ArrayList<Datamodel> arrSelectedModel; // private ArrayList<Boolean> arrC

这是我的课

public class AdapterContact extends BaseAdapter {
        private Context context;
        private ArrayList<Datamodel> arrModel;
        public ArrayList<Datamodel> arrSelectedModel;
        // private ArrayList<Boolean> arrCheckboxState;
        ViewHolder holder;
        ImageLoader imageloader;

        public AdapterContact(Context context, ArrayList<Datamodel> arrModel) {
            this.context = context;
            this.arrModel = arrModel;
            arrSelectedModel = new ArrayList<Datamodel>();
            imageloader = new ImageLoader(context);

        }

        @Override
        public int getCount() {
            return arrModel.size();
        }

        @Override
        public Object getItem(int position) {
            return arrModel.get(position);
        }

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                holder = new ViewHolder();
                LayoutInflater mInflater = (LayoutInflater) context
                        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                convertView = mInflater.inflate(R.layout.row_contact, null);
                holder.checkBox = (CheckBox) convertView
                        .findViewById(R.id.rowcontact_checkbox);
                holder.txtContactName = (TextView) convertView
                        .findViewById(R.id.rowcontact_txtName);
                holder.imgProfilepic = (ImageView) convertView
                        .findViewById(R.id.rowcontact_imgProfile);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.txtContactName.setText(arrModel.get(position).contactName);

            holder.checkBox
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            int position = (Integer) buttonView.getTag();

                            if (isChecked) {
                                // arrCheckboxState.set(position, true);
                                arrModel.get(position).setChecked(true);
                                arrSelectedModel.add(arrModel.get(position));
                            } else {
                                arrModel.get(position).setChecked(false);
                                // arrCheckboxState.set(position, false);
                                arrSelectedModel.remove(arrModel.get(position));
                            }
                        }
                    });
            holder.checkBox.setTag(position);
            holder.checkBox.setChecked(arrModel.get(position).isChecked);
            if (arrModel.get(position).isActive) {
                holder.checkBox.setVisibility(View.GONE);
            }

            AQuery aq = new AQuery(context);
            aq.id(holder.imgProfilepic).image(arrModel.get(position).picturePath,
                    false, false, 0, R.drawable.imge);
            String url2 = arrModel.get(position).picturePath + "?t="
                    + System.currentTimeMillis();
            aq.id(holder.imgProfilepic).image(url2, false, false, 0,
                    R.drawable.imge);
            convertView.setBackgroundColor(Color.parseColor("#ffffff"));
            return convertView;
        }

        class ViewHolder {
            TextView txtContactName;
            ImageView imgProfilepic;
            CheckBox checkBox;
        }
    }
公共类AdapterContact扩展了BaseAdapter{
私人语境;
私有ArrayList模型;
公共ArrayList arrSelectedModel;
//私有ArrayList arrCheckboxState;
视窗座;
图像加载器;
公共AdapterContact(上下文、ArrayList和arrModel){
this.context=上下文;
this.arrModel=arrModel;
arrSelectedModel=newarraylist();
imageloader=新的imageloader(上下文);
}
@凌驾
public int getCount(){
返回arrModel.size();
}
@凌驾
公共对象getItem(int位置){
返回模型。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
if(convertView==null){
holder=新的ViewHolder();
LayoutInflater mInflater=(LayoutInflater)上下文
.getSystemService(活动.布局\充气机\服务);
convertView=mInflater.充气(R.layout.row_触点,空);
holder.checkBox=(复选框)convertView
.findViewById(R.id.rowcontact_复选框);
holder.txtContactName=(TextView)convertView
.findViewById(R.id.rowcontact_txtName);
holder.imgProfilepic=(图像视图)convertView
.findViewById(R.id.rowcontact\U imgProfile);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.txtContactName.setText(arrModel.get(position.contactName));
holder.checkBox
.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,
布尔值(已检查){
int position=(整数)按钮view.getTag();
如果(已检查){
//arrCheckboxState.set(位置,true);
arrModel.get(位置).setChecked(真);
arrSelectedModel.add(arrModel.get(position));
}否则{
arrModel.get(位置).setChecked(false);
//arrCheckboxState.set(位置,false);
arrSelectedModel.remove(arrModel.get(position));
}
}
});
holder.checkBox.setTag(位置);
holder.checkBox.setChecked(arrModel.get(position.isChecked));
if(arrModel.get(position.isActive){
holder.checkBox.setVisibility(View.GONE);
}
AQuery aq=新的AQuery(上下文);
aq.id(holder.imgProfilepic).image(arrModel.get(position).picturePath,
假,假,0,R.可拉深。imge);
字符串url2=arrModel.get(位置).picturePath+“?t=”
+System.currentTimeMillis();
aq.id(holder.imgProfilepic).image(url2,false,false,0,
R.drawable.imge);
convertView.setBackgroundColor(Color.parseColor(“#ffffff”));
返回视图;
}
类视图持有者{
TextView txtContactName;
ImageView imgProfilepic;
复选框;
}
}
这是我的XMl:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="70dip"
    android:background="#ffffff"
    android:orientation="vertical" >

    <com.lociiapp.utils.RoundedImageView
        android:id="@+id/rowcontact_imgProfile"
        android:layout_width="46dip"
        android:layout_height="46dip"
        android:layout_centerVertical="true"
        android:layout_marginBottom="7dip"
        android:layout_marginLeft="7dip"
        android:layout_marginTop="7dip" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dip"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/rowcontact_imgIcon"
        android:layout_toRightOf="@+id/rowcontact_imgProfile"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/rowcontact_txtName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:layout_marginTop="17dip"
            android:text="Usama Sadiq"
            android:textColor="#3c3b3b"
            android:textSize="17dip" />
    </RelativeLayout>

    <CheckBox
        android:id="@+id/rowcontact_checkbox"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:button="@drawable/ic_launcher" />

</RelativeLayout>


我在listview中打印数据时选中复选框,当项目未选中或出现故障时,复选框背景图像将显示x,如果我们选中或启用了任何项目的复选框,则背景图像应更改。我绑定到xml中的set-dfault图像,选中后背景未更改,请检查错误代码。rowcontact\u复选框id的复选框请建议我

将您的复选框背景设置为
选择器
,将其置于
可绘制/your\u background.xml
下。
选择器
将决定选中/取消选中/禁用复选框时显示的背景。。。您可以尝试快速生成一个

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
          android:drawable="@drawable/your_checked_background" />
    <item android:drawable="@drawable/your_unchecked_background" />
</selector>

将复选框背景设置为
选择器
,将其置于
可绘制/your_background.xml
下。
选择器
将决定选中/取消选中/禁用复选框时显示的背景。。。您可以尝试快速生成一个

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
          android:drawable="@drawable/your_checked_background" />
    <item android:drawable="@drawable/your_unchecked_background" />
</selector>

使用此复选框选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/checkbox_checked" android:state_checked="true"/>
 <!-- checked -->
    <item android:drawable="@drawable/checkbox_unchecked" android:state_checked="false"/>
 <!-- unchecked -->

</selector>
在适配器中安装的列表视图中使用此代码

lstview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterview, View view,
                    int position, long id) {

                CheckBox chkbox = (CheckBox) view
                        .findViewById(R.id.chkboxid);
                chkbox.performClick();

            }
        });
lstview.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView,视图,
内部位置,长id){
复选框chkbox=(复选框)视图
.findviewbyd(R.id.chkboxid);
chkbox.performClick();
}
});

使用此复选框选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/checkbox_checked" android:state_checked="true"/>
 <!-- checked -->
    <item android:drawable="@drawable/checkbox_unchecked" android:state_checked="false"/>
 <!-- unchecked -->

</selector>
在适配器中安装的列表视图中使用此代码

lstview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterview, View view,
                    int position, long id) {

                CheckBox chkbox = (CheckBox) view
                        .findViewById(R.id.chkboxid);
                chkbox.performClick();

            }
        });
lstview.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView,视图,
内部位置,长id){
复选框chkbox=(复选框)视图
.findviewbyd(R.id.chkboxid);
chkbox.performClick();
}
});

以下链接可能会帮助您以下链接可能会帮助您,但我无法隐藏并启用复选框选中我们必须在我的代码中更改的内容您想隐藏吗