• 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
  • 相关阅读:
    provider: Shared Memory Provider, error: 0
    用户 'sa' 登录失败。 (Microsoft SQL Server,错误: 18456)
    从sqlite 迁移到mysql报错,处理 D:seafile-server_5.0.3_win32seafile-server-5.0.3seahubsql 重新执行这个SQL
    org.apache.jasper.runtime.ELContextImpl cannot be cast to org.apache.jasper.el.ELContextImpl
    java.lang.NoClassDefFoundError: org/apache/log4j/spi/ThrowableInformation
    无法完成操作。服务无法在此时接受控制信息。
    MyEclipse出现Unable to install breakpoint in...
    ERROR 1364 (HY000): Field 'id' doesn't have a default value
    各地都在搞大数据,你的家乡有啥特色没——解读2017年地方政府大数据报告
    HTML5桌面通知:notification
  • 原文地址:https://www.cnblogs.com/cmblogs/p/4347586.html
Copyright © 2020-2023  润新知