0

How to replace the checkbox in listview with radiobuttons that can be only selected one at a time. Any idea ? Below is my working code

enter image description here

MainActivity.java

public class MainActivity extends ExpandableListActivity
{
    private static final String LOG_TAG = "ElistCBox2";
    private ColorAdapter expListAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        ArrayList<String> groupNames = new ArrayList<String>();
        groupNames.add( "grey" );
        groupNames.add( "blue" );
        groupNames.add( "yellow" );
        groupNames.add( "red" );
        ArrayList<ArrayList<Color>> colors = new ArrayList<ArrayList<Color>>(); 
        ArrayList<Color> color = new ArrayList<Color>();
        color.add( new Color( "lightgrey","#D3D3D3", false ) ); 
        color.add( new Color( "dimgray","#696969", true ) ); 
        color.add( new Color( "sgi gray 92","#EAEAEA", false ) );
        colors.add( color );
        color = new ArrayList<Color>();
        color.add( new Color( "dodgerblue 2","#1C86EE",false ) );
        color.add( new Color(  "steelblue 2","#5CACEE",false ) ); 
        color.add( new Color( "powderblue","#B0E0E6", true ) );
        colors.add( color );
        color = new ArrayList<Color>();
        color.add( new Color( "yellow 1","#FFFF00",true ) );
        color.add( new Color( "gold 1","#FFD700",false ) ); 
        color.add( new Color( "darkgoldenrod 1","#FFB90F", true ) );
        colors.add( color );
        color = new ArrayList<Color>();
        color.add( new Color( "indianred 1","#FF6A6A",true ) );
        color.add( new Color( "firebrick 1","#FF3030",false ) ); 
        color.add( new Color( "maroon","#800000", false ) );
        colors.add( color );

        expListAdapter = new ColorAdapter( this,groupNames, colors );
        setListAdapter( expListAdapter );
    }

    public void onContentChanged  () {
        super.onContentChanged();
        Log.d( LOG_TAG, "onContentChanged" );
    }

    public boolean onChildClick(
            ExpandableListView parent, 
            View v, 
            int groupPosition,
            int childPosition,
            long id) {
        Log.d( LOG_TAG, "onChildClick: "+childPosition );
        CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
        if( cb != null )
            cb.toggle();
        return false;
    }
}

Color.java

public class Color {
    public String color = null;
    public String rgb = null;
    public boolean state = false;

    public Color( String color, String rgb, boolean state ) {
        this.color = color;
        this.rgb = rgb;
        this.state = state;
    }

    public String getColor() {
        return color;
    }

    public String getRgb() {
        return rgb;
    }

    public boolean getState() {
        return state;
    }

}

ColorAdapter.java

public class ColorAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<String> groups;
    private ArrayList<ArrayList<Color>> colors;
    private LayoutInflater inflater;

    public ColorAdapter(Context context, 
                        ArrayList<String> groups,
                        ArrayList<ArrayList<Color>> colors ) { 
        this.context = context;
        this.groups = groups;
        this.colors = colors;
        inflater = LayoutInflater.from( context );
    }

    public Object getChild(int groupPosition, int childPosition) {
        return colors.get( groupPosition ).get( childPosition );
    }

    public long getChildId(int groupPosition, int childPosition) {
        return (long)( groupPosition*1024+childPosition );  // Max 1024 children per group
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View v = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.child_row, parent, false); 
        Color c = (Color)getChild( groupPosition, childPosition );
        TextView color = (TextView)v.findViewById( R.id.childname );
        if( color != null )
            color.setText( c.getColor() );
        TextView rgb = (TextView)v.findViewById( R.id.rgb );
        if( rgb != null )
            rgb.setText( c.getRgb() );
        CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
        cb.setChecked( c.getState() );
        return v;
    }

    public int getChildrenCount(int groupPosition) {
        return colors.get( groupPosition ).size();
    }

    public Object getGroup(int groupPosition) {
        return groups.get( groupPosition );        
    }

    public int getGroupCount() {
        return groups.size();
    }

    public long getGroupId(int groupPosition) {
        return (long)( groupPosition*1024 );  // To be consistent with getChildId
    } 

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View v = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.group_row, parent, false); 
        String gt = (String)getGroup( groupPosition );
        TextView colorGroup = (TextView)v.findViewById( R.id.childname );
        if( gt != null )
            colorGroup.setText( gt );
        return v;
    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    } 

    public void onGroupCollapsed (int groupPosition) {} 
    public void onGroupExpanded(int groupPosition) {}


}

child_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView android:id="@+id/childname"
         android:paddingLeft="50px"
         android:focusable="false"
         android:textSize="14px"
         android:textStyle="italic"
         android:layout_width="150px"
         android:layout_height="wrap_content"/>

    <TextView android:id="@+id/rgb"
         android:focusable="false"
         android:textSize="14px"
         android:textStyle="italic"
         android:layout_width="100px"
         android:layout_height="wrap_content"/>

    <CheckBox android:id="@+id/check1"
           android:focusable="false"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"/>

</LinearLayout>

group_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView android:id="@+id/childname"
         android:paddingLeft="50px"
         android:textSize="14px"
         android:textStyle="italic"
         android:layout_width="150px"
         android:layout_height="wrap_content"/>

</LinearLayout>

main.xml

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent">

     <ExpandableListView android:id="@+id/android:list"
               android:layout_width="fill_parent" 
               android:layout_height="fill_parent"/>

     <TextView android:id="@+id/android:empty"
               android:layout_width="fill_parent" 
               android:layout_height="fill_parent"
               android:text="@string/main_no_items"/>
</LinearLayout>
Christine
  • 329
  • 1
  • 4
  • 13

3 Answers3

0

In the child row xml file replace checkbox tag with this:

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton android:id="@+id/radio_pirates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pirates"
        android:onClick="onRadioButtonClicked"/>
    <RadioButton android:id="@+id/radio_ninjas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ninjas"
        android:onClick="onRadioButtonClicked"/>
</RadioGroup>

and in the activity override this method

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_pirates:
            if (checked)
                // Pirates are the best
            break;
        case R.id.radio_ninjas:
            if (checked)
                // Ninjas rule
            break;
    }
}
saurabh64
  • 363
  • 1
  • 4
  • 24
  • Thanks @saurabh. My query is the same radio button will be under other expandable view, would not that create a conflict ? Do i have to create that many radio buttons for all expandable lists ? – Christine May 08 '15 at 09:34
  • You might as well have to try and see – saurabh64 May 08 '15 at 09:47
0

below is my code. Pleas use radiogroup and then add radiobutton dynamically. This way only one is selected at one time.

  for (int row = 0; row < 1; row++) {
        LinearLayout l1 = new LinearLayout(this);
        l1.setOrientation(LinearLayout.HORIZONTAL);

        for (int i = 1; i <= number; i++) {
            RadioButton rb= new RadioButton(this);
            rb.setId((row * 2) + i);
            rb.setText("Radio " + rdbtn.getId());
            l1.addView(rdbtn);
        }
        ((ViewGroup) findViewById(R.id.radiogroup)).addView(l1);
Sam
  • 1,237
  • 1
  • 16
  • 29
0

In your child_expense_amount.xml use RadioButton instead of CheckBox.

Since you'll be using radio button, you'll manually (programmatically) need to set it on/off because single selection is supported in radio group. For manually turning it on/off (toggle selection) , use listeners like onClick or onSelected, in where toggle selection.