main.xml配置文件
<?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="@string/hello" /> <ListView android:id="@+id/lv_single_list"
android:descendantFocusability="blocksDescendants"
android:fastScrollEnabled="true" android:clickable="true"
android:divider="@drawable/divider_horizontal_timeline"
android:dividerHeight="1.0dip" android:layout_width="fill_parent"
android:layout_height="400dp" android:scrollingCache="false"
android:fadingEdge="none" android:cacheColorHint="#00000000" /> </LinearLayout>
适配器adapter的配置文件list_adapter.xml
<?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:textSize="14sp" android:layout_marginLeft="10dp" /> <CheckBox android:id="@+id/item_cb_section" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:focusable="false"
android:focusableInTouchMode="false" android:clickable="false"
android:layout_alignParentTop="true" android:layout_marginLeft="5dp" /> </LinearLayout>
SingleListChoiceActivity.java

package com.amker.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView; public class SingleListChoiceActivity extends Activity { private ListView listView; private Map<Integer, Boolean> isSelected; private List beSelectedData = new ArrayList(); ListAdapter adapter; private List cs = null; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) this.findViewById(R.id.lv_single_list);
cs = new ArrayList();
cs.add("aaaaaa");
cs.add("bbbbbb");
cs.add("cccccc");
cs.add("dddddd");
cs.add("eeeeee");
cs.add("ffffff");
cs.add("gggggg");
cs.add("hhhhhh");
cs.add("jjjjjj"); initList();
} void initList(){ if (cs == null || cs.size() == )
return;
if (isSelected != null)
isSelected = null;
isSelected = new HashMap<Integer, Boolean>();
for (int i = ; i < cs.size(); i++) {
isSelected.put(i, false);
}
// 清除已经选择的项
if (beSelectedData.size() > ) {
beSelectedData.clear();
}
adapter = new ListAdapter(this, cs);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
adapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("map", cs.get(position).toString());
}
}); } class ListAdapter extends BaseAdapter { private Context context; private List cs; private LayoutInflater inflater; public ListAdapter(Context context, List data) {
this.context = context;
this.cs = data;
initLayoutInflater();
} void initLayoutInflater() {
inflater = LayoutInflater.from(context);
} public int getCount() {
return cs.size();
} public Object getItem(int position) {
return cs.get(position);
} public long getItemId(int position) {
return ;
} 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) {
// 当前点击的CB
boolean cu = !isSelected.get(position);
// 先将所有的置为FALSE
for(Integer p : isSelected.keySet()) {
isSelected.put(p, false);
}
// 再将当前选择CB的实际状态
isSelected.put(position, cu);
ListAdapter.this.notifyDataSetChanged();
beSelectedData.clear();
if(cu) beSelectedData.add(cs.get(position));
}
});
holder.tv_sequence.setText(String.valueOf(position + ));
holder.tv_sectionname.setText(cs.get(position).toString());
holder.checkBox.setChecked(isSelected.get(position));
return convertView;
}
} class ViewHolder { CheckBox checkBox; TextView tv_sequence; TextView tv_sectionname; }
}

ListView与CheckBox组合实现单选的更多相关文章

  1. ListView与RadioButton组合——自定义单选列表

      标签: radiobuttonlistviewandroidlayout 2013-09-10 11:13 19396人阅读 评论(8) 收藏 举报  分类: Android(19)  版权声明: ...

  2. 【转】ListView与RadioButton组合——自定义单选列表

    原文网址:http://blog.csdn.net/checkin001/article/details/11519131 Android自带的RadioButton单选框只支持添加文字,我们自己写A ...

  3. 基于CSS3自定义美化复选框Checkbox组合

    今天我们要来分享一组非常漂亮的CSS3自定义复选框checkbox,每一个checkbox都有其各自的特点.有几款checkbox在选中的情况下还会出现动画效果,非常不错的CSS3自定义美化check ...

  4. android UI进阶之实现listview中checkbox的多选与记录

    今天继续和大家分享涉及到listview的内容.在很多时候,我们会用到listview和checkbox配合来提供给用户一些选择操作.比如在一个 清单页面,我们需要记录用户勾选了哪些条目.这个的实现并 ...

  5. 【转】android UI进阶之实现listview中checkbox的多选与记录--不错

    原文网址:http://www.cnblogs.com/notice520/archive/2012/02/17/2355415.html 今天继续和大家分享涉及到listview的内容.在很多时候, ...

  6. Android高级控件(一)——ListView绑定CheckBox实现全选,增加和删除等功能

    Android高级控件(一)--ListView绑定CheckBox实现全选,增加和删除等功能 这个控件还是挺复杂的,也是项目中应该算是比较常用的了,所以写了一个小Demo来讲讲,主要是自定义adap ...

  7. winform利用ImageList控件和ListView控件组合制作图片文件浏览器

    winform利用ImageList控件和ListView控件组合制作图片文件浏览器,见图,比较简单,实现LISTVIEW显示文件夹图片功能. 1.选择文件夹功能代码: folderBrowserDi ...

  8. ListView 自己定义BaseAdapter实现单选打勾(无漏洞)

    (假设须要完整demo,请评论留下邮箱) (眼下源代码已经不发送.假设须要源代码,加qq316701116.不喜勿扰) 近期由于一个项目的原因须要自己定义一个BaseAdapter实现ListVIew ...

  9. Android高级控件(一)——ListView绑定CheckBox实现全选,添加和删除等功能

    Android高级控件(一)--ListView绑定CheckBox实现全选,添加和删除等功能 这个控件还是挺复杂的.也是项目中应该算是比較经常使用的了,所以写了一个小Demo来讲讲,主要是自己定义a ...

随机推荐

  1. hadoop 1.2.1 安装步骤 伪分布式

    最近在系统的学习hadoop 课程第一步是安装hadoop1.x,具体安装步骤如下: 一.系统安装 本文使用centos6.5安装,具体安装步骤省略 二.jdk安装 下载jdk1.7.0_51解压,在 ...

  2. 方案:解决 wordpress 中 gravatar 头像被墙问题

    Gravatar头像具有很好的通用性,但是却遭到了无辜的拦截,对于无法加载头像URL,我们在WordPress系统中通过修改默认的URL链接可以达到恢复头像的功能. 修改文件路径为 /wp-inclu ...

  3. bst 二叉搜索树简单实现

    //数组实现二叉树: // 1.下标为零的元素为根节点,没有父节点 // 2.节点i的左儿子是2*i+1:右儿子2*i+2:父节点(i-1)/2: // 3.下标i为奇数则该节点有有兄弟,否则又左兄弟 ...

  4. 【HDU1879】继续畅通工程(MST基础题)

    真心大水题...不多说. #include <iostream> #include <cstring> #include <cstdlib> #include &l ...

  5. 分析linux下的编译环境

    不论是windows下的程序,还是linux下的程序,开发环境都离不开三个目录:include.lib.bin,分别是头文件目录.库文件目录.运行文件目录.或许目录不叫这个名字,但却必不可少,除非你的 ...

  6. python3-day3(深浅copy)

    1.对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. import copy n1 = 123 print(id(n1)) n2 = n1 print(id(n ...

  7. runtime的基本应用

    1.什么是runtime? runtime是一套底层的C语言API,包含很多强大实用的C语言数据类型和C语言函数,平时我们编写的OC代码,底层都是基于runtime实现的. 2.runtime有什么作 ...

  8. (HYSBZ)BZOJ 1588 营业额统计

    营业额统计 Time Limit: 5000MS   Memory Limit: 165888KB   64bit IO Format: %lld & %llu Description 营业额 ...

  9. pat 1049 Counting Ones

    要统计1到N之间‘1’的个数,如数11包含2个1.所以当N=12时,答案为5. 思想: 找规律,假设ans[N]表示1到N的‘1’的个数,则有a[100]=(a[10]-1)*9+10+a[10]-1 ...

  10. IOS中对图片进行重绘处理的方法总结

    一.CGImageRef是什么 CGImageRef是定义在QuartzCore框架中的一个结构体指针,用C语言编写.在CGImage.h文件中,我们可以看到下面的定义: ? 1 typedef st ...