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

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

<ListView
  android:id="@+id/listView1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:layout_centerVertical="true" >
    </ListView>

列表中每行的内容

<?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" >

  <RelativeLayout
    android:id="@+id/outpatient_check_hospital"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5.0dip"
    android:layout_marginLeft="12.599976dip"
    android:layout_marginRight="12.599976dip"
    android:layout_marginTop="5.0dip"
    android:gravity="center_vertical" 
    android:background="#AAAAAA">

    <LinearLayout
      android:id="@+id/linear_layout_up"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_margin="10.0dip"
      android:gravity="center"
      android:orientation="horizontal" >

      <ImageView
        android:layout_width="10dip"
        android:layout_height="10dip"
        android:adjustViewBounds="false" />

      <TextView
        android:id="@+id/tv_device_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2.0"
        android:text="名称"
        android:textColor="#ff323232"
        android:textSize="16.0sp"
        android:typeface="monospace" />

      <RadioButton
        android:id="@+id/rb_light"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:text="" />
      
    </LinearLayout>
  </RelativeLayout>

</LinearLayout>

使用列表的页面

public class MainActivity extends Activity {
  private ListView listView;
  private ListViewAdapter adapter;
  private String[] beans = new String[] { "1", "2", "3", "4", "5", "6", "7",
      "8", "9", "10", "11", "12", "13" };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initView();

  }

  private void initView() {
    // TODO Auto-generated method stub
    Log.i("htp", "beans.size:" + beans.length);
    listView = (ListView) findViewById(R.id.listView1);
    adapter = new ListViewAdapter(MainActivity.this, beans);
    listView.setAdapter(adapter);
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  }

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

package com.example.listviewdemo;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {

  private Context context;
  private String[] beans;
  // 用于记录每个RadioButton的状态,并保证只可选一个
  HashMap<String, Boolean> states = new HashMap<String, Boolean>();

  class ViewHolder {

    TextView tvName;
    RadioButton rb_state;
  }

  public ListViewAdapter(Context context, String[] beans) {
    // TODO Auto-generated constructor stub
    this.beans = beans;
    this.context = context;
  }

  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    return beans.length;
  }

  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    return beans[position];
  }

  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    // 页面
    ViewHolder holder;
    String bean = beans[position];
    LayoutInflater inflater = LayoutInflater.from(context);
    if (convertView == null) {
      convertView = inflater.inflate(
          R.layout.assist_device_binding_list_item, null);
      holder = new ViewHolder();
//			holder.rb_state = (RadioButton) convertView
//					.findViewById(R.id.rb_light);
      holder.tvName = (TextView) convertView
          .findViewById(R.id.tv_device_name);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    
    holder.tvName.setText(bean);
    final RadioButton radio=(RadioButton) convertView.findViewById(R.id.rb_light);  
      holder.rb_state = radio;  
    holder.rb_state.setOnClickListener(new View.OnClickListener() {

      public void onClick(View v) {

        // 重置,确保最多只有一项被选中
        for (String key : states.keySet()) {
          states.put(key, false);

        }
        states.put(String.valueOf(position), radio.isChecked());
        ListViewAdapter.this.notifyDataSetChanged();
      }
    });

    boolean res = false;
    if (states.get(String.valueOf(position)) == null
        || states.get(String.valueOf(position)) == false) {
      res = false;
      states.put(String.valueOf(position), false);
    } else
      res = true;

    holder.rb_state.setChecked(res);
    return convertView;
  }
}

http://www.tuicool.com/articles/YjY7je
原文地址:https://www.cnblogs.com/cmblogs/p/4347586.html