Search...

Sunday, November 13, 2011

How to group radio buttons in a custom adapter in android


Step1: Create one custom checkable class which extends Relative Layout and implements Checkable interface.

public class MyCustomCheckableView extends RelativeLayout implements Checkable{

 private boolean isChecked;
private List<Checkable> checkableViews;
 
 


 
   public MyCustomCheckableView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
initialise(attrs);
}

public MyCustomCheckableView(Context context, AttributeSet attrs) {
super(context, attrs);
initialise(attrs);
}

public MyCustomCheckableView(Context context, int checkableId) {
super(context);
initialise(null);
}


public boolean isChecked() {
return isChecked;
}

/*
* @see android.widget.Checkable#setChecked(boolean)
*/
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
for (Checkable c : checkableViews) {
c.setChecked(isChecked);
}
}

/*
* @see android.widget.Checkable#toggle()
*/
public void toggle() {
this.isChecked = !this.isChecked;
for (Checkable c : checkableViews) {
c.toggle();
}
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();

final int childCount = this.getChildCount();
for (int i = 0; i < childCount; ++i) {
findCheckableChildren(this.getChildAt(i));
}
}

/**
* Read the custom XML attributes
*/
private void initialise(AttributeSet attrs) {
this.isChecked = false;
this.checkableViews = new ArrayList<Checkable>(5);
}

/**
* Add to our checkable list all the children of the view that implement the
* interface Checkable
*/
private void findCheckableChildren(View v) {
if (v instanceof Checkable) {
this.checkableViews.add((Checkable) v);
}

if (v instanceof ViewGroup) {
final ViewGroup vg = (ViewGroup) v;
final int childCount = vg.getChildCount();
for (int i = 0; i < childCount; ++i) {
findCheckableChildren(vg.getChildAt(i));
}
}

}

}

Step2: Create one layout xml and use the above checkable custom layout .
my_item.xml

<com.MyCustomCheckableView android:id="@+id/RelativeLayout001"
android:layout_width="fill_parent" android:layout_height="50dp"
xmlns:android="http://schemas.android.com/apk/res/android" android:background="#FFFFFF">


<TextView android:text="My Name" android:id="@+id/MyName"
android:singleLine="true" android:ellipsize="end"
android:layout_width="120dp" android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:gravity="left|center_vertical"
android:textAppearance="@style/sans20black" android:layout_marginLeft="10dp" android:focusable="false">
</TextView>

<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton" android:gravity="right|center_vertical"
android:layout_alignParentRight="true" android:focusable="false"/>

<TextView android:text="RceiverId" android:id="@+id/receiverId"
android:singleLine="true" android:ellipsize="end"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="right" android:paddingRight="20dp"
android:layout_below="@+id/MyName" android:layout_toRightOf="@+id/MyModel"
android:layout_alignParentRight="false"
android:layout_alignParentBottom="false" android:textAppearance="@style/sans13grey"
android:layout_marginRight="20dp" android:focusable="false">
</TextView>
<TextView android:layout_width="120dp" android:textStyle="normal"
android:singleLine="true" android:text="My Model" android:gravity="left|center_vertical"
android:ellipsize="end" android:layout_height="wrap_content"
android:id="@+id/MyModel" android:layout_below="@+id/MyName" android:focusable="false"
android:layout_alignParentLeft="true" android:layout_marginLeft="10dp" android:textAppearance="@style/sans13grey"></TextView>


</com.MyCustomCheckableView>


Step3 : In your activity set the following things in your list:
OnCreate(){
myList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
myList.setItemsCanFocus(false);
}

Step4 : Use the custom item XML created in step 2 in your custom adapter.
MyCustomAdapter.java
public View getView(int position, View convertView, ViewGroup parent) {

View row = convertView;
MyListView sView = null;
final String receiverId;


try {

if (row == null) {

row = inflater.inflate(R.layout. my_item.xml, null);
sView = (MyListView) new MyListView(row,_context);
row.setTag(sView);

} else {
sView = (MyListView) row.getTag();
}


                   


return row;

} catch (Exception e) {



return row;
}

}

1 comment:

  1. Please give an example of this line:

    sView = (MyListView) new MyListView(row,_context);

    I don't get the _context part.

    ReplyDelete