0

This is my class

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

this is my 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>

i am printing data in listview with checkbox selection when item not checked or dfault then checkbox background image will x and if we checked or enable check box of any item then background image should change i am tied to set dfault image in xml and after checked background is not changing please check code where am doing wrong . rowcontact_checkbox id of check box please suggest me

Edge
  • 925
  • 5
  • 15
  • 31
  • http://stackoverflow.com/questions/7783787/how-to-change-default-images-of-checkbox Following link may help you – Anjali Jan 22 '15 at 07:41

2 Answers2

3

USE THIS CHECKBOX 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>

AND ADD THESE LINES FOR UR CHECKBOX

android:background="@drawable/checkbox_selector"
android:button="@android:color/transparent"

use this code in to ur listview insted in adapter

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();

            }
        });
M S Gadag
  • 2,057
  • 1
  • 12
  • 15
  • but i am unable to Hide and enable check box checked what we have to change in my code – Edge Jan 22 '15 at 08:02
  • u want to hide checkbox when its checked? – M S Gadag Jan 22 '15 at 08:05
  • yes http://snag.gy/ipyWi.jpg check this image by deafualt i want set checkbox background x and after check y if again unchecked x – Edge Jan 22 '15 at 08:06
  • where i will Put this code in adpater class please tell me coz listview,setonclick is not working Properly – Edge Jan 22 '15 at 08:47
  • its not working i want check and uncheck bacground image of checkbox should change on listview item please see my image u ll get – Edge Jan 22 '15 at 08:50
  • CheckBox chkbox = (CheckBox) view .findViewById(R.id.chkboxid); chkbox.performClick(); what about checkboxid this id we can get in baseadapter not in main actvity @ M S Gadag – Edge Jan 22 '15 at 09:13
  • id of checkbox in ur item xml...and by using `view.` u ll get the specific position checkbox oly pls implement the code n try.... – M S Gadag Jan 22 '15 at 09:16
  • http://paste.ofcode.org/phpKjCXDbKLXctDZ9E8NcL check this code andc heck initView() method i have applied listview item clcik funcation like ur way but Exception is coming and this http://paste.ofcode.org/xZa47ZriFVcAerM8WHTJTU coming please chek where am doing wrong – Edge Jan 22 '15 at 09:28
  • u r using onclick method... u should use setOnItemClickListener..check my ans again pls... – M S Gadag Jan 22 '15 at 09:33
  • http://paste.ofcode.org/35vJvLjDgQeMbdqKLFLBEV4 this code apply but still not chnaging check box background when i click on its item where am doing wrong – Edge Jan 22 '15 at 09:40
  • r u chaged ur checkbor background to checkbox_selector? – M S Gadag Jan 22 '15 at 09:43
  • thanx can u please tell me ur email id – Edge Jan 22 '15 at 09:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69381/discussion-between-edge-and-m-s-gadag). – Edge Jan 22 '15 at 09:58
1

Set your checkbox background to a selector, put it under drawable/your_background.xml. The selector will decide what background to display when the checkbox is checked/unchecked/disabled etc... You can try android-holo-colors to quickly generate one

<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>
hidro
  • 12,333
  • 6
  • 53
  • 53
  • but i am unable to Hide and enable check box checked what we have to change in my code – Edge Jan 22 '15 at 08:03