1

I have a layout having a toggle button and a listview. The toggle button is outside the listview.Listview contains texts and checkboxes. How could I check and uncheck the checkbox inside the getView() of my CustomAdapter class on clicking the toggle button in android.

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF" >



    <LinearLayout
        android:id="@+id/linlay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">

    <LinearLayout
        android:id="@+id/linlay1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:orientation="vertical">


        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="@drawable/border_set" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linlay2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Linlay1"
        android:background="#ffffff"
        android:gravity="center_vertical" 
        >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="Notifications"
            android:textColor="#000000"
            android:textSize="18sp"
            android:textStyle="bold" />



        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right" >

            <ToggleButton
                android:id="@+id/toggleButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:checked="true"
                android:text="ToggleButton" />

        </LinearLayout>

     </LinearLayout>


     <LinearLayout
        android:id="@+id/linlay3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Linlay2"
        android:background="#ffffff"
        android:orientation="vertical" >

         <TextView
             android:layout_width="fill_parent"
             android:layout_height="2dp"
             android:background="@drawable/border_set" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="Please choose your area of intrest"
            android:textSize="16sp"
            android:textColor="#000000"/>



     </LinearLayout>


    <LinearLayout
        android:id="@+id/linlay4"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/linlay3"
        android:orientation="vertical" 
        android:background="@drawable/border_set">

        <ListView
            android:id="@+id/listView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:cacheColorHint="#00000000"
            android:listSelector="@android:color/transparent" />

    </LinearLayout>


    </LinearLayout>

</RelativeLayout>

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RLfirst"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:paddingBottom="3dp"
    android:paddingTop="3dp" >

    <RelativeLayout
        android:id="@+id/RLthird"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:paddingBottom="5dp" >

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="title"
            android:textColor="#0000ff"
            android:textSize="15sp"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/RLsecond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" >

        <ImageView
            android:id="@+id/RightArrowimg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="7dp"
            android:scaleType="fitXY"
            android:visibility="invisible"
            android:layout_alignParentRight="true"
            android:src="@drawable/red_arrow_small" />

        <CheckBox
        android:id="@+id/chk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:visibility="visible"
        android:layout_alignParentTop="true"
        android:scaleX=".60"
        android:scaleY=".60" 
        android:layout_below="@id/RightArrowimg" 
        android:enabled="true"/>
    </RelativeLayout>
</RelativeLayout>

Test.class

    public class Test extends Activity{
    ListView listView1;
    ToggleButton toggleButton;
    Boolean flag=true;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            listView1 = (ListView) findViewById(R.id.listView1);
            toggleButton=(ToggleButton)findViewById(R.id.toggleButton);
            toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
                    if (isChecked) {
                        flag=true;

                    } else {
                        flag=false;

                    }

                }
            });
            ArrayList<String> arr=new ArrayList<String>();
            arr.add("a");
            arr.add("b");

            MyAdapter madap = new MyAdapter(Test.this,arr);
            listView1.setAdapter(madap);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public class MyAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        private Context cont;
        int layout_xml;
        ArrayList<String> arr;

        public MyAdapter(Context context, ArrayList<String> arr) {
            this.cont = context;
            this.arr = arr;
            mInflater = LayoutInflater.from(context);
        }
        public View getView(final int position, View convertView,ViewGroup parent) {
            try {

                if (convertView == null) {
                    layout_xml = R.layout.list_row;
                    convertView = mInflater.inflate(layout_xml, null);
                }

                TextView tvtitle = (TextView) convertView.findViewById(R.id.tv_title);
                ImageView RightArrowimg=(ImageView)convertView.findViewById(R.id.RightArrowimg);
                final CheckBox chk=(CheckBox)convertView.findViewById(R.id.chk);

                chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(flag){

                            chk.setEnabled(true);
                            chk.setClickable(true);

                        }else{

                            chk.setEnabled(false);
                            chk.setClickable(false);
                            chk.setChecked(false);

                        }

                    }
                });


            } catch (Exception e) {
                e.printStackTrace();
            }


            return convertView;
        }

        @Override
        public long getItemId(int position) {

            return 0;
        }

        @Override
        public int getCount() {

            return arr.size();

        }

        @Override
        public Object getItem(int position) {
            // return position;
            return null;
        }

    }
}
  • For `CheckBox` in `ListView`, maybe better you refer to this sample (http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html), – Xcihnegn Mar 20 '15 at 14:29

1 Answers1

0

Without any code, I can only guess what you are doing...

If you have some sort of ArrayList with the state of each checkbox within the ListView, you can set the state of each to false/true and then the ArrayAdapters notifyDataSetChanged() to update the list view data.

Gary Bak
  • 4,746
  • 4
  • 22
  • 39
  • I suppose you can get help from my answer to the post [Single selection in RecyclerView](http://stackoverflow.com/questions/28972049/single-selection-in-recyclerview/29030776?noredirect=1#comment46498087_29030776) – Xcihnegn Mar 19 '15 at 16:53
  • Hey thanks for replaying.But did not get anything.shareing my code below.please help me. – rupalin senapati Mar 20 '15 at 13:22