0

Radio Buttons Deselected on scrolling in custom listview i have made custom listview that add run time radiobutton added autimatically but it deselected on scroll

my code given below of adapter and mainclass and activity files

Layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:background="@drawable/gradient11"
              android:layout_width="match_parent" android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RadioButton
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="option one is selected now so you can"
                android:textColor="#00ff00"
                android:id="@+id/op1"/>

            <RadioButton
                android:checked="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="op2"
                android:textColor="#00ff00"
                android:id="@+id/op2"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="op3"
                android:textColor="#00ff00"
                android:id="@+id/op3"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="op4"
                android:textColor="#00ff00"
                android:id="@+id/op4"/>

        </RadioGroup>

    </LinearLayout>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/quest"
        android:textColor="#e2000000"/>

</LinearLayout>

The Adapter file

package com.patel.ravin.com.domparsing;

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.RadioButton;
    import android.widget.TextView;

    import java.util.ArrayList;

    /**
     * Created by lenovo on 08-08-2016.
     */
    public class Adpt extends BaseAdapter
    {
        Context context;
        ArrayList<MyBean> arrayList;

        public Adpt(Context context,ArrayList<MyBean> arrayList)
        {
            this.context=context;
            this.arrayList=arrayList;
        }
        @Override
        public int getCount() {
            return arrayList.size();
        }

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

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

        @Override
        public View getView(int i, View view, ViewGroup parent) {

            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.listview, null);

            TextView txtFName = (TextView) view.findViewById(R.id.qid);
            TextView txtLName = (TextView) view.findViewById(R.id.quest);
            RadioButton op1=(RadioButton)view.findViewById(R.id.op1);
            RadioButton op2=(RadioButton)view.findViewById(R.id.op2);
            RadioButton op3=(RadioButton)view.findViewById(R.id.op3);
            RadioButton op4=(RadioButton)view.findViewById(R.id.op4);


            MyBean myBean = arrayList.get(i);
            txtFName.setText("" + myBean.getQid());
            txtLName.setText("   Answer= " + myBean.getQname());
            op1.setText(myBean.getOp1());
            op2.setText(myBean.getOp2());
            op3.setText(myBean.getOp3());
            op4.setText(myBean.getOp4());

            return view;
        }
    }

The Activity file

package com.patel.ravin.com.domparsing;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.patel.ravin.com.domparsing.AsyncTask.AsyncTaskLoader;
    import com.patel.ravin.com.domparsing.AsyncTask.OnAsyncResult;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import java.util.ArrayList;
    import java.util.HashMap;

    public class MainActivity extends AppCompatActivity {

        TextView textView;
        ListView listView1;
        Adpt adpt;

         ArrayList<MyBean>  arrayList=null;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

           // textView= (TextView) findViewById(R.id.tv1);
            listView1=(ListView)findViewById(R.id.llv);

    //        Adpt adpt=new Adpt(getApplicationContext(),arrayList);


            //listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,arrayList));
            OnAsyncResult onAsyncResult=new OnAsyncResult() {
                @Override
                public void onAsyncResult(String result) {

                    Log.e("h", result.toString());

                    try {
                      //  textView.setText(""+result.toString());
                     //   String co=result.toString();

                        JSONArray jsonArray=new JSONArray(result);
                        MyBean myBean;
                        arrayList = new ArrayList<>();

                        for(int i=1;i<=jsonArray.length();i++)
                        {
                            JSONObject jsonObject=jsonArray.getJSONObject(i);


                            myBean = new MyBean();
                            myBean.setQid(jsonObject.getString("que"));
                            myBean.setQname(jsonObject.getString("ans"));

                            myBean.setOp1(jsonObject.getString("a"));
                            myBean.setOp2(jsonObject.getString("b"));
                            myBean.setOp3(jsonObject.getString("c"));
                            myBean.setOp4(jsonObject.getString("d"));

                            arrayList.add(myBean);
                            listView1.setAdapter(new Adpt(getApplicationContext(),arrayList));
                        }
                        //JSONObject   object = new JSONObject(result);

                        //String contact = object.getString("que");



                      //  textView.setText(co);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                }
            };

            AsyncTaskLoader asyncTaskLoader=new AsyncTaskLoader(MainActivity.this,onAsyncResult,null,"http://quiz/jsonapi.php");
            asyncTaskLoader.execute();


        }


    }

Radio Buttons Deselected on scrolling in custom listview i have made custom listview that add run time radiobutton added autimatically but it deselected on scroll

Ricardo Vieira
  • 1,738
  • 1
  • 18
  • 26

1 Answers1

0

This is a very common problem in Android with Listviews and Radio buttons.

First, I would recommend you to check this post: Using radio button in custom listview in Android

Now, I'm going to tell you which solution fits for me. In my case I use GridView, but it also works for ListView.

In your Adapter's class you have to have a variable for the selected item and his index, it could be something like:

private int mSelectedPosition = -1;
private RadioButton mSelectedRB;

Then, define a function to retrieve this information:

public int getItemSelected(){
    return  mSelectedPosition;
}

Now, In order to make it works properly, you have to user a ViewHolder (in my case every item in the GridView has a Image and a RadioButton), so you define your ViewHolder in the Adapter's class as well:

private class ViewHolder {
    ImageView image;
    RadioButton radio;
}

Then, in your getView function, you have to work with the ViewHolder. In the code I write comments to follow.

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView; // assign the view 
    ViewHolder holder; // declare the ViewHolder

    if(view == null){
        // cutom layout for each row (item) of the ListView
        view = mLayoutInflater.inflate(R.layout.item_list_company, parent, false);
        holder = new ViewHolder();

        // initialize the ViewHolder's field
        holder.image = (ImageView) view.findViewById(R.id.c1);
        holder.radio = (RadioButton)view.findViewById(R.id.cN1);

        view.setTag(holder); // set the tag
    }else{ // already initialized
        holder = (ViewHolder)view.getTag(); // so we only set the tag
    }

    // on click triggered for each RadioButton in the ListView
    holder.radio.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(position != mSelectedPosition && mSelectedRB != null){
                mSelectedRB.setChecked(false); // uncheck the last one
            }

            mSelectedPosition = position; // change the item selected index 
            mSelectedRB = (RadioButton)v; // assign the new item selected
        }
    });

    // just to control the right item checked
    if(mSelectedPosition != position){
        holder.radio.setChecked(false);
    }else{
        holder.radio.setChecked(true);
        if(mSelectedRB != null && holder.radio != mSelectedRB){
            mSelectedRB = holder.radio;
        }
    }

    return view;
}

Finally, in the custom layout (item_list_company.xml in my case) for each item in the ListView, I have the following code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:orientation="vertical"
   android:layout_height="match_parent">
   <ImageView android:id="@+id/c1"
       android:layout_width="wrap_content"
       android:layout_height="100dp"
       android:layout_gravity="center"
       android:scaleType="centerInside" />
   <RadioButton
       android:id="@+id/cN1"
       android:layout_width="wrap_content"
       android:layout_height="50dp"
       android:buttonTint="@color/colorPrimary"
       android:textColor="@color/colorAccent"
       android:focusable="false"
       android:layout_gravity="left"
       android:clickable="false"
       android:focusableInTouchMode="false"
       android:textSize="20dp"/>
</LinearLayout>

Special attention for this three attributes of the RadioButton:

android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"

So, with all of this, you only have to set your adapter and ListView in your Activity and call to the right function to retrieve the selected item:

ArrayList<MyBean> arrayList = new ArrayList<>();
ListView listView1 = (ListView)findViewById(R.id.llv);
Adpt adpt = new Adpt(getApplicationContext(), arrayList);

// add data to the ArrayList

adpt.notifyDataSetChanged(); // notify for the new data in the ArrayList

// retrieve the item selected
int selected = adpt.getItemSelected();

Hope it helps!

Community
  • 1
  • 1
David Corral
  • 4,085
  • 3
  • 26
  • 34