Friday 14 September 2012

android: Listview with radio button (only one radio button selected when we click on listview Item)



main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
   
        <ListView
            android:id="@+id/lv1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:cacheColorHint="#f8f8ff"
            android:divider="#000000"
            android:fadeScrollbars="true"
            android:choiceMode="singleChoice"
            android:fastScrollEnabled="true"
            android:footerDividersEnabled="true"
            android:textFilterEnabled="true"
            android:textStyle="normal" />
       
        <Button  android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="get"
            android:id="@+id/Button01" />
   

</LinearLayout>


list_single_check.xml 


?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativeLayout1"
     android:layout_width="fill_parent" android:layout_height="wrap_content"  
     xmlns:android="http://schemas.android.com/apk/res/android">
   
     <LinearLayout android:layout_centerVertical="true"
         android:layout_width="fill_parent" android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:orientation="vertical" android:gravity="center_vertical">
       
         <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content" android:id="@+id/tv_MainText"
              android:textAppearance="?android:attr/textAppearanceMedium" />
         <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content" android:id="@+id/tv_SubText"
              android:textAppearance="?android:attr/textAppearanceSmall" />
     </LinearLayout>

     <RadioButton android:layout_height="wrap_content"
         android:id="@+id/rb_Choice" android:layout_width="wrap_content"  android:layout_centerVertical="true"
         android:layout_alignParentRight="true" android:gravity="center_vertical"
         android:focusable="false"  android:clickable="false"
        ></RadioButton>

HomeActivity

public class HomeActivity extends Activity {
HomeActivity m_this;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
  
    m_this = this;
    
    //begin init test data
    final ArrayList<HashMap<String, Object>> m_data = new ArrayList<HashMap<String, Object>>();
    
    HashMap<String, Object> map1 = new HashMap<String, Object>();
    map1.put("maintext", "Large text 1");
    map1.put("subtext", "small text 1A");        
    m_data.add(map1);
    
    HashMap<String, Object> map2 = new HashMap<String, Object>();
    map2.put("maintext", "Large text 2");// no small text of this item!
    m_data.add(map2);
    
    HashMap<String, Object> map3 = new HashMap<String, Object>();
    map3.put("maintext", "Large text 3");
    map3.put("subtext", "small text 3");
    m_data.add(map3);
    
    for (HashMap<String, Object> m :m_data) //make data of this view should not be null (hide )
m.put("checked", false);
  //end init data
    
    
    final ListView lv = (ListView) m_this.findViewById(R.id.lv1);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    
    
    final SimpleAdapter adapter = new SimpleAdapter(m_this,
    m_data,
            R.layout.listitem_single_check,
            new String[] {"maintext", "subtext", "checked"}, 
            new int[] {R.id.tv_MainText, R.id.tv_SubText, R.id.rb_Choice}); 
    
    adapter.setViewBinder(new ViewBinder()
{
    public boolean setViewValue(View view, Object data, String textRepresentation)
    {
if (data == null) //if 2nd line text is null, its textview should be hidden 
{
view.setVisibility(View.GONE);
return true;
}
view.setVisibility(View.VISIBLE);
return false;
}

});
    

    // Bind to our new adapter.
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
RadioButton rb = (RadioButton) v.findViewById(R.id.rb_Choice);
if (!rb.isChecked()) //OFF->ON
{
for (HashMap<String, Object> m :m_data) //clean previous selected
m.put("checked", false);
m_data.get(arg2).put("checked", true);
adapter.notifyDataSetChanged();
}
}        
});
    
    //show result
    ((Button)m_this.findViewById(R.id.Button01)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int r = -1;
for (int i = 0; i < m_data.size(); i++) //clean previous selected
{
HashMap<String, Object> m = m_data.get(i);
Boolean x = (Boolean) m.get("checked");
if (x == true)
{
r = i;
break; //break, since it's a single choice list
}
}
new AlertDialog.Builder(m_this).setMessage("you selected:"+r).show();
}
});
    
}  
}



No comments:

Post a Comment