android Listview checkbox 单选

1、

package com.example.checkboxdemo;



import java.util.ArrayList;
import java.util.List;


import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;


public class MainActivity extends ActionBarActivity
{
    private ListView     listView;    //


    private ListAdapter  adapter;     // adapter


    private List<String> mList = null; // 数据源


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listView = (ListView) this.findViewById(R.id.lv_single_list);
        mList = new ArrayList();
        mList.add("aaaaaa");
        mList.add("bbbbbb");
        mList.add("cccccc");


        adapter = new ListAdapter(this, mList);
        listView.setAdapter(adapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        adapter.notifyDataSetChanged();


    }


}

2、

package com.example.checkboxdemo;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;


public class ListAdapter extends BaseAdapter
{


    private Context               mContext;
    private LayoutInflater        inflater;


    private List<String>          mList;                                   // 获取数据源


    private Map<Integer, Boolean> mIsSelectedMap;
    private List<String>          beSelectedData = new ArrayList<String>(); // 已经选择的数据集合


    public ListAdapter(Context context, List list)
    {
        this.mContext = context;
        this.mList = list;
        initLayoutInflater();
        mIsSelectedMap = new HashMap<Integer, Boolean>();
        for (int i = 0; i < mList.size(); i++)
        {
            mIsSelectedMap.put(i, false);
        }
        // 清除已经选择的项
        if (beSelectedData.size() > 0)
        {
            beSelectedData.clear();
        }
    }


    void initLayoutInflater()
    {
        inflater = LayoutInflater.from(mContext);
    }


    public int getCount()
    {
        return mList.size();
    }


    public Object getItem(int position)
    {
        return mList.get(position);
    }


    public long getItemId(int position)
    {
        return 0;
    }


    public View getView(int position1, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        View view = null;
        final int position = position1;
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.list_adapter, null);
            holder = new ViewHolder();
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.item_cb_section);
            holder.tv_sequence = (TextView) convertView.findViewById(R.id.tv_zxing_section_sequence);
            holder.tv_sectionname = (TextView) convertView.findViewById(R.id.tv_zxing_sectionname);
            convertView.setTag(holder);
        } else
        {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        holder.checkBox.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                // 当前点击的checkBox
                boolean current_checkBox = !mIsSelectedMap.get(position);
                // 先将所有的置为false
                for (Integer allFalse : mIsSelectedMap.keySet())
                {
                    mIsSelectedMap.put(allFalse, false);
                }
                // 再将当前选择checkBox的实际状态
                mIsSelectedMap.put(position, current_checkBox);
                ListAdapter.this.notifyDataSetChanged();
                beSelectedData.clear();
                if (current_checkBox)
                    beSelectedData.add(mList.get(position));
                // 遍历HashMap
                Iterator iter = mIsSelectedMap.entrySet().iterator();
                while (iter.hasNext())
                {
                    Map.Entry entry = (Map.Entry) iter.next();
                    int position = (Integer) entry.getKey();
                    boolean val = (Boolean) entry.getValue();
                    if (val == true)
                    {
                        System.out.println("position==" + position);
                    }
                }
            }
        });
        holder.tv_sequence.setText(String.valueOf(position + 1));
        holder.tv_sectionname.setText(mList.get(position).toString());
        holder.checkBox.setChecked(mIsSelectedMap.get(position));
        return convertView;
    }
}


class ViewHolder
{


    CheckBox checkBox;


    TextView tv_sequence;


    TextView tv_sectionname;


}

3、

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal" >


    <TextView
        android:id="@+id/tv_zxing_section_sequence"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:textSize="16sp" />


    <TextView
        android:id="@+id/tv_zxing_sectionname"
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textSize="14sp" />


    <CheckBox
        android:id="@+id/item_cb_section"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false" />


</LinearLayout>

4、

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="hello" />
    
    <ListView android:id="@+id/lv_single_list"
android:descendantFocusability="blocksDescendants"
android:fastScrollEnabled="true" android:clickable="true"
android:dividerHeight="1.0dip" android:layout_width="fill_parent"
android:layout_height="400dp" android:scrollingCache="false"
android:fadingEdge="none" android:cacheColorHint="#00000000" />


</LinearLayout>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值