-3

I found this code that works very good and I have to create something of very similar but with RadioButton. What would I have to change?

    public class InteractiveArrayAdapter extends ArrayAdapter<Model> {

    private final List<Model> list;
    private final Activity context;

    public InteractiveArrayAdapter(Activity context, List<Model> list) {
        super(context, R.layout.rowbuttonlayout, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.rowbuttonlayout, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
            viewHolder.checkbox
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            Model element = (Model) viewHolder.checkbox
                                    .getTag();
                            element.setSelected(buttonView.isChecked());

                        }
                    });
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
    }
}



    public class Model {

    private String name;
    private boolean selected;

    public Model(String name) {
        this.name = name;
        selected = false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

}

"rowbuttonlayout.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="wrap_content" >

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="30px" >
    </TextView>

    <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px" >
    </CheckBox>

</RelativeLayout>

I found all this code on this website.

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
michoprogrammer
  • 1,159
  • 2
  • 18
  • 45

1 Answers1

1

I think you'll find all required info in this already answered question :

listview with radio group error

The accepted answer of @Dante includes all the required code for you to rewrite the custom array adapter to use radiobuttons.

EDIT

According to your comment I am not sure you really know how to write a custom list with Custom Adapter, view Holder and Object.

Romain Guy explains listview in details here

http://www.google.com/events/io/2010/sessions/world-of-listview-android.html

and the related pdf

http://dl.google.com/googleio/2010/android-world-of-listview-android.pdf

Once you handel customadapter viewholder etc you simply have to have your list implements checkable. as for example here

How to use RadioGroup in ListView custom adapter?

If you don't know how to set a custom list view here is an example, but it's not directly related to your question :

// The Adapter
class myObjectAdapter extends ArrayAdapter<myObject> {
    myObjectAdapter() {
        super(MyActivity.this, R.layout.my_row_layout,
                listItems);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        myObjectHolder holder = null;
        if (convertView == null) {
            LayoutInflater inflater = getLayoutInflater();
            convertView = inflater.inflate(R.layout.my_row_layout,
                    parent, false);
            holder = new myObjectHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (myObjectHolder) convertView.getTag();
        }
        holder.populateFrom(listItems.get(position));
        return (convertView);
    }
}

// The Holder
static class myObjectHolder {
    private TextView myText = null;        

    myObjectHolder(View row) {
        myText = (TextView) row.findViewById(R.id.MyTextView);            
    }

    void populateFrom(myObject r) {
        myText.setText(r.getText());            
    }
}

// The Object
class myObject {        
    public final String myText;        

    public myObject(String myText) {            
        this.myText = myText;            
    }

    public String getText() {
        return myText;
    }                
}

assuming you have an xml file called my_row_layout which contains a textview called MyTextView. In your case you will replace the textview with a radiobutton and according to the link given above implemnt checkable and it'll work.

Hope it helps

Community
  • 1
  • 1
HpTerm
  • 8,151
  • 12
  • 51
  • 67
  • There is a problem in the Dante's solution. He creates exactly 5 RadioButton! I have to create the same RadioButton dynamycally because I don't know the number. How can I do? – michoprogrammer Feb 21 '12 at 14:52
  • http://stackoverflow.com/questions/7329856/how-to-use-radiogroup-in-listview-custom-adapter <- This post doesn't help me. Your example is very useful and this http://www.google.com/events/io/2010/sessions/world-of-listview-android.html is helpful to understand the question. Now I created the same thing with the radiobutton but I don't know why it is possible check to many button and not only one... I guess that the problem is that I don't set the radiogroup, but I don't know where I can set it! I'm sorry for the newbie question but I'm using android only from 2 week... – michoprogrammer Feb 21 '12 at 20:47
  • Ok great, you now have a list with radiobutton, and you need to have only 1 checked at a time. Even though you say "stackoverflow.com/questions/7329856/… <- This post doesn't help me" I think it should help, you are now in the same state. You have to do 2 things. use CHOICE_MODE_SINGLE and implement checkable in your view row. – HpTerm Feb 22 '12 at 06:58