Android中ListView与RadioButton结合----自定义单选列表

有时候我们需要制作自定义的单选列表,但是会遇到一些问题,比如多选,假选问题,所以网上找了找资料,整理一个demo出来,贴一下代码:


[html]  view plain copy
  1. <ListView  
  2.        android:id="@+id/listView1"  
  3.        android:layout_width="match_parent"  
  4.        android:layout_height="wrap_content"  
  5.        android:layout_centerHorizontal="true"  
  6.        android:layout_centerVertical="true" >  
  7.    </ListView>  

列表中每行的内容

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <RelativeLayout  
  8.         android:id="@+id/outpatient_check_hospital"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginBottom="5.0dip"  
  12.         android:layout_marginLeft="12.599976dip"  
  13.         android:layout_marginRight="12.599976dip"  
  14.         android:layout_marginTop="5.0dip"  
  15.         android:gravity="center_vertical"   
  16.         android:background="#AAAAAA">  
  17.   
  18.         <LinearLayout  
  19.             android:id="@+id/linear_layout_up"  
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="wrap_content"  
  22.             android:layout_margin="10.0dip"  
  23.             android:gravity="center"  
  24.             android:orientation="horizontal" >  
  25.   
  26.             <ImageView  
  27.                 android:layout_width="10dip"  
  28.                 android:layout_height="10dip"  
  29.                 android:adjustViewBounds="false" />  
  30.   
  31.             <TextView  
  32.                 android:id="@+id/tv_device_name"  
  33.                 android:layout_width="wrap_content"  
  34.                 android:layout_height="wrap_content"  
  35.                 android:layout_weight="2.0"  
  36.                 android:text="名称"  
  37.                 android:textColor="#ff323232"  
  38.                 android:textSize="16.0sp"  
  39.                 android:typeface="monospace" />  
  40.   
  41.             <RadioButton  
  42.                 android:id="@+id/rb_light"  
  43.                 android:layout_width="wrap_content"  
  44.                 android:layout_height="wrap_content"  
  45.                 android:focusable="false"  
  46.                 android:text="" />  
  47.               
  48.         </LinearLayout>  
  49.     </RelativeLayout>  
  50.   
  51. </LinearLayout>  


使用列表的页面

[java]  view plain copy
  1. public class MainActivity extends Activity {  
  2.     private ListView listView;  
  3.     private ListViewAdapter adapter;  
  4.     private String[] beans = new String[] { "1""2""3""4""5""6""7",  
  5.             "8""9""10""11""12""13" };  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.   
  12.         initView();  
  13.   
  14.     }  
  15.   
  16.     private void initView() {  
  17.         // TODO Auto-generated method stub  
  18.         Log.i("htp""beans.size:" + beans.length);  
  19.         listView = (ListView) findViewById(R.id.listView1);  
  20.         adapter = new ListViewAdapter(MainActivity.this, beans);  
  21.         listView.setAdapter(adapter);  
  22.         listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);  
  23.     }  

适配器内容,在这里面处理如何单选问题


[java]  view plain copy
  1. package com.example.listviewdemo;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.List;  
  5.   
  6. import android.content.Context;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.BaseAdapter;  
  11. import android.widget.ImageView;  
  12. import android.widget.LinearLayout;  
  13. import android.widget.RadioButton;  
  14. import android.widget.TextView;  
  15.   
  16. public class ListViewAdapter extends BaseAdapter {  
  17.   
  18.     private Context context;  
  19.     private String[] beans;  
  20.     // 用于记录每个RadioButton的状态,并保证只可选一个  
  21.     HashMap<String, Boolean> states = new HashMap<String, Boolean>();  
  22.   
  23.     class ViewHolder {  
  24.   
  25.         TextView tvName;  
  26.         RadioButton rb_state;  
  27.     }  
  28.   
  29.     public ListViewAdapter(Context context, String[] beans) {  
  30.         // TODO Auto-generated constructor stub  
  31.         this.beans = beans;  
  32.         this.context = context;  
  33.     }  
  34.   
  35.     @Override  
  36.     public int getCount() {  
  37.         // TODO Auto-generated method stub  
  38.         return beans.length;  
  39.     }  
  40.   
  41.     @Override  
  42.     public Object getItem(int position) {  
  43.         // TODO Auto-generated method stub  
  44.         return beans[position];  
  45.     }  
  46.   
  47.     @Override  
  48.     public long getItemId(int position) {  
  49.         // TODO Auto-generated method stub  
  50.         return position;  
  51.     }  
  52.   
  53.     @Override  
  54.     public View getView(final int position, View convertView, ViewGroup parent) {  
  55.         // TODO Auto-generated method stub  
  56.         // 页面  
  57.         ViewHolder holder;  
  58.         String bean = beans[position];  
  59.         LayoutInflater inflater = LayoutInflater.from(context);  
  60.         if (convertView == null) {  
  61.             convertView = inflater.inflate(  
  62.                     R.layout.assist_device_binding_list_item, null);  
  63.             holder = new ViewHolder();  
  64. //          holder.rb_state = (RadioButton) convertView  
  65. //                  .findViewById(R.id.rb_light);  
  66.             holder.tvName = (TextView) convertView  
  67.                     .findViewById(R.id.tv_device_name);  
  68.             convertView.setTag(holder);  
  69.         } else {  
  70.             holder = (ViewHolder) convertView.getTag();  
  71.         }  
  72.           
  73.         holder.tvName.setText(bean);  
  74.         final RadioButton radio=(RadioButton) convertView.findViewById(R.id.rb_light);    
  75.         holder.rb_state = radio;    
  76.         holder.rb_state.setOnClickListener(new View.OnClickListener() {  
  77.   
  78.             public void onClick(View v) {  
  79.   
  80.                 // 重置,确保最多只有一项被选中  
  81.                 for (String key : states.keySet()) {  
  82.                     states.put(key, false);  
  83.   
  84.                 }  
  85.                 states.put(String.valueOf(position), radio.isChecked());  
  86.                 ListViewAdapter.this.notifyDataSetChanged();  
  87.             }  
  88.         });  
  89.   
  90.         boolean res = false;  
  91.         if (states.get(String.valueOf(position)) == null  
  92.                 || states.get(String.valueOf(position)) == false) {  
  93.             res = false;  
  94.             states.put(String.valueOf(position), false);  
  95.         } else  
  96.             res = true;  
  97.   
  98.         holder.rb_state.setChecked(res);  
  99.         return convertView;  
  100.     }  
  101. }  

【原文:http://blog.csdn.net/qq544529563/article/details/38758839?utm_source=tuicool】

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值