1

I am developing an application.I have a button to add some users when I click a button it displays a list of radio buttons which are nothing but users(contacts) where I can select multiple users my problem is that I can select any user a select but i cannot deselect the user which is already selected I have used

buttonView.setEnabled(false);

this statement but I am unable to deselect can any help me.

I have used arrayadapter to display in list view. this is the following code

Main class

public class Contactselectuser extends Activity {
    Contactadapter contactadapteruser;
    private ArrayList<String> listexample;
    Button conform;
    private ListView list;
    ImageButton close;
    boolean check;
    RadioButton rb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.selectrepresentative);
        conform = (Button) findViewById(R.id.Confirm);
        list = (ListView) findViewById(R.id.selectrecipientslist);
        listexample=new ArrayList<String>();
        getlist();

        close=(ImageButton)findViewById(R.id.close);
        close.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

              finish(); 
            }
        });
      contactadapteruser = new Contactadapter(Contactselectuser.this,              
                                    R.layout.selectrepresentative, listexample);
                   list.setAdapter(contactadapteruser);
      private void getlist() {
        // TODO Auto-generated method stub
        listexample.add("Rep. Name (R)");

        listexample.add("Rep. Name (R)");

        listexample.add("Rep. Name (R)");

        listexample.add("Rep. Name (R)");

        listexample.add("Rep. Name (R)");

        listexample.add("Rep. Name (D)");

        listexample.add("Rep. Name (D)");

        listexample.add("Rep. Name (R)");

        listexample.add("Rep. Name (R)");

        listexample.add("Rep. Name (D)");

        listexample.add("Rep. Name (R)");

        listexample.add("Rep. Name (D)");

        listexample.add("Rep. Name (R)");

    }

        }

Adapter class New adapter class

   public class Contactadapter extends ArrayAdapter<String> {
    private Context context;
    boolean value;
    ListView list;
    private ArrayList<String> listexample;
        public Contactadapter(Context con, int layout, ArrayList<String> listexample) {
            super(con, com.arivoli.amev.R.layout.selectrepresentative, listexample);
            this.context = con;
            this.listexample = listexample;
            }

            public View getView(final int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View rowView = inflater.inflate(R.layout.selectradio, parent, false);
                RadioButton rb=(RadioButton) rowView.findViewById(R.id.radioButton1);
                rb.setText(listexample.get(position));
                value = rb.isChecked();
                //Toast.makeText(context, "status"+value, 5000).show();
                rb.setTag(value);
                rb.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        RadioButton radio=(RadioButton)v;


                       if(radio.isChecked()){
                           radio.getTag();
                           value = radio.isChecked();
                           radio.setChecked(value);
                           value=false;
                       }else
                       {
                           radio.getTag();
                           radio.setChecked(false);
                            value=true; 
                       }
                       }
                });

                return rowView;


            }


}
Sanjeev
  • 292
  • 2
  • 5
  • 24

4 Answers4

1

I you want to be able to unselect your choices, you should use CheckBox rather that radiobox, and unselect it by using setSelected(true/false);

If you want to keep your RadioButtons, try so replace buttonView.setEnabled(false); by buttonView.setChecked(false);

Damien R.
  • 3,383
  • 1
  • 21
  • 32
  • I have tried it when do that i am unable to select radio button means it is unchecked but i can get the toast message when click on it – Sanjeev Dec 20 '13 at 09:42
0

instead of using

 if (value == false) {

previously set rb.setTag(value)

and rb.getTag() inside the method and use.

Tamilselvan Kalimuthu
  • 1,534
  • 1
  • 13
  • 28
  • list view elements are like dynamic views if 20 elements are there in a list and 10 can be shown in screen , so it will hold only those 10 elements and update the 10 elements while user moving the list up or down so inside adapter class having some variable values like position value or some other values will not be respective to that particular element. so if u want a variable inside an adapter class u can use setTag() and get the value using getTag(). http://stackoverflow.com/a/5291891/1937802 – Tamilselvan Kalimuthu Dec 20 '13 at 09:42
  • pls tell where should i add rb.setTag(value) – Sanjeev Dec 20 '13 at 09:43
  • settag after set the value for the rb and gettag inside the method u r using – Tamilselvan Kalimuthu Dec 20 '13 at 09:44
  • i have used rb.setTag(value); after before rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {} and i have used getTag(); in changeoption method but i am getting error he method getTag() is undefined for the type new CompoundButton.OnCheckedChangeListener(){} – Sanjeev Dec 20 '13 at 09:51
  • get tag is for the item. rb.getTag – Tamilselvan Kalimuthu Dec 20 '13 at 09:57
  • Hey onclick method is working for me thanks but only once how can i make use of setTag and getTag – Sanjeev Dec 20 '13 at 10:07
  • good.. and i have given my ans to know about the list view . http://android.amberfog.com/?p=296 will give u better understanding about list view accept his ans if works – Tamilselvan Kalimuthu Dec 20 '13 at 10:10
  • i will do that but it is working only once means onclick it will be enable and another click it will disable and if i click on any item it will not work i have added a comment in ketan can you please read it and tell me where i am wrong – Sanjeev Dec 20 '13 at 10:15
  • hm for that only i have suggested as using settag and gettag. it will overcome the list view recycling problems – Tamilselvan Kalimuthu Dec 20 '13 at 10:35
  • hey i will post my new adapter class can you please tell me where should i write the setTag and getTag methods.I have written settag and get tag methods also please help me i want learn android properly – Sanjeev Dec 20 '13 at 10:43
  • hey i have added new adapter class pls tell me where i am wrong – Sanjeev Dec 20 '13 at 10:46
  • if(radio.isChecked()){ replace this with if(rb.getTag){ and try – Tamilselvan Kalimuthu Dec 20 '13 at 10:49
  • it is giving me this error cannot convert from Object to boolean can i write radio.getTag – Sanjeev Dec 20 '13 at 10:53
  • it is an obj u have to convert it to boolean Boolean.parseBoolean(rb.getTag().toString()); – Tamilselvan Kalimuthu Dec 20 '13 at 10:54
  • i have copied the statement Boolean.parseBoolean(rb.getTag().toString()); into if condition it is giving me error Multiple markers at this line - Cannot refer to a non-final variable rb inside an inner class defined in a different method - Syntax error on token ";", delete this token – Sanjeev Dec 20 '13 at 10:58
  • make the rb as final and remove that ; – Tamilselvan Kalimuthu Dec 20 '13 at 11:00
0

use onClickListner() instead of onCheckChangeListener

rb.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            RadioButton radio=(RadioButton)v;

            //do something with radio
            radio.setChecked(value);
        }
    });
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
  • value = rb.isEnabled(); i have used this statement before on click method public void onClick(View v) { RadioButton radio=(RadioButton)v; //do something with radio if(radio.isChecked()){ radio.setChecked(value); value=false; }else { radio.setChecked(value); value=true; } } }); thanks buddy its working but i can check and uncheck only once – Sanjeev Dec 20 '13 at 10:06
  • heyy help me how should i may value variable as local – Sanjeev Dec 20 '13 at 10:10
  • why do you use `setEnabled` and `isEnabled`? Just use `setChecked` and `isChecked`. – Ketan Ahir Dec 20 '13 at 10:11
  • ok but when i scroll down selected radio buttons are unchecked why it is happening – Sanjeev Dec 20 '13 at 10:17
  • It happens because of listview recycling. see here [ListView Recycling Problem](http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works) – Ketan Ahir Dec 20 '13 at 10:21
  • how to over come it what should i do – Sanjeev Dec 20 '13 at 10:24
  • for that please do some google search and then post a new fresh question. – Ketan Ahir Dec 20 '13 at 10:31
  • i have updated my new adapter class can you please check it and tell me where i am going wroung – Sanjeev Dec 20 '13 at 11:10
0

Below your check i.e radio.isChecked()

YOu have done this :- value = radio.isChecked(); radio.setChecked(value);

It will set the value the same as it was before. Shouldn't it be radio.setChecked(!value); This code will deselect if user is selected.