ListView中RadioButton实现单项选择
1:FragmentHack5.java
public class FragmentHack5 extends Fragment {
View view;
ListView lvCountries;
Button btnShow;
CountryListAdapter adapter;
List<String> list;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
list = new ArrayList<String>();
list.add("中国");
list.add("俄罗斯");
list.add("美国");
list.add("德国");
list.add("英国");
list.add("西班牙");
list.add("法国");
list.add("巴西");
list.add("印度");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_hack5,container,false);
btnShow = (Button)view.findViewById(R.id.btnShow);
lvCountries = (ListView)view.findViewById(R.id.lvCountries);
adapter = new CountryListAdapter(getActivity(),R.layout.list_country_item,list);
lvCountries.setAdapter(adapter);
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(),adapter.getChoiceCountry(),Toast.LENGTH_SHORT).show();;
}
});
return view;
}
}
2:CountryListAdapter.java
public class CountryListAdapter extends ArrayAdapter<String>{
int resourceId;
int choiceId = -1;
public CountryListAdapter(Context context, int resourceId, List<String> objects){
super(context,resourceId,objects);
this.resourceId = resourceId;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
String country = getItem(position);
final ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(resourceId,null);
holder.rbCountry = (RadioButton)convertView.findViewById(R.id.rbCountry);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.rbCountry.setText(country);
if(choiceId==position){
holder.rbCountry.setChecked(true);
}else{
holder.rbCountry.setChecked(false);
}
holder.rbCountry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(holder.rbCountry.isChecked()){
choiceId = position;//记住当前选中的下标
CountryListAdapter.this.notifyDataSetChanged();
}
}
});
return convertView;
}
static class ViewHolder{
public RadioButton rbCountry;
}
public String getChoiceCountry(){
return getItem(choiceId);
}
}
3:运行结果


ListView中RadioButton实现单项选择的更多相关文章
- 【转】ListView与RadioButton组合——自定义单选列表
原文网址:http://blog.csdn.net/checkin001/article/details/11519131 Android自带的RadioButton单选框只支持添加文字,我们自己写A ...
- ListView与RadioButton组合——自定义单选列表
标签: radiobuttonlistviewandroidlayout 2013-09-10 11:13 19396人阅读 评论(8) 收藏 举报 分类: Android(19) 版权声明: ...
- 如何在Android的ListView中构建CheckBox和RadioButton列表(支持单选和多选的投票项目示例)
引言 我们在android的APP开发中有时候会碰到提供一个选项列表供用户选择的需求,如在投票类型的项目中,我们提供一些主题给用户选择,每个主题有若干选项,用户对这些主题的选项进行选择,然后提交. 本 ...
- 单项选择RadioButton和多项选择CheckBox的使用
在Android中,可以通过RadioButton和RadioGroup的组合来实现单项选择的效果.而多项选择则是通过CheckBox来实现的. 1.单项选择RadioButton 我们知道,一 ...
- 阅读《Android 从入门到精通》(10)——单项选择
单项选择(RadioGroup) RadioGroup 是 LinearLayout 的子类,继承关系例如以下: android.view.ViewGroup android.widget.Linea ...
- Android 如何在 ListView 中更新 ProgressBar 进度
=======================ListView原理============================== Android 的 ListView 的原理打个简单的比喻就是: 演员演 ...
- C#-WinForm-ListView-表格式展示数据、如何将数据库中的数据展示到ListView中、如何对选中的项进行修改
在展示数据库中不知道数量的数据时怎么展示最好呢?--表格 ListView - 表格形式展示数据 ListView 常用属性 HeaderStyle - "详细信息"视图中列标头的 ...
- C#中将ListView中数据导出到Excel
首先 你需要添加引用Microsoft Excel 11.0 Object Library 添加方法:选择项目->引用->右击“添加引用”->选择COM 找到上面组件—>点击“ ...
- 最熟悉的陌生人:ListView 中的观察者模式
RecyclerView 得宠之前,ListView 可以说是我们用的最多的组件.之前一直没有好好看看它的源码,知其然不知其所以然. 今天我们来窥一窥 ListView 中的观察者模式. 不熟悉观察者 ...
随机推荐
- Ubuntu12.04 下修改Apache端口号
1:$sudo vim /etc/apache2/ports.conf NameVirtualHost *:80Listen 8090 #将此行的80修改成8090 2:sudo vim /etc/a ...
- Configuring HTTP and HTTPS
Configuring HTTP and HTTPS .NET Framework (current version) Other Versions WCF services and clie ...
- 查询Sqlserver 表结构信息 SQL
SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号 = a.colorder, 字段名 = ...
- 2015第24周三Spring事务3
在一个典型的事务处理场景中,有以下几个参与者: Resource Manager(RM) ResourceManager简称RM,它负责存储并管理系统数据资源的状态,比如数据库服务器,JMS消息服务器 ...
- ubuntu scp
一.将本机文件复制到远程服务器上 scp -r /Users/Dreamover/Desktop/jsnone dreamover@localserver:/var/www/project/ /Use ...
- Jquery回车键切换焦点方法(兼容各大浏览器)
做项目时,客户要求能够用enter回车直接切换输入(焦点),当最后一个时候,直接提交信息. 第一想法就是,网上去copy一段代码直接用.但了百度.谷歌找了个遍,找到的代码80%以上都是一样的.有的代码 ...
- [转] Trie树详解及其应用
一.知识简介 最近在看字符串算法了,其中字典树.AC自动机和后缀树的应用是最广泛的了,下面将会重点介绍下这几个算法的应用. 字典树(Trie)可以保存一些字符串->值 ...
- HBase面试问题
一.HBase的特点是什么 1.HBase一个分布式的基于列式存储的数据库,基于hadoop的hdfs存储,zookeeper进行管理. 2.HBase适合存储半结构化或非结构化数据,对于数据结构字段 ...
- CSS介绍
从HTML被发明开始,样式就以各种形式存在.不同的浏览器结合它们各自的样式语言为用户提供页面效果的控制.最初的HTML只包含很少的显示属性. 随着HTML的成长,为了满足页面设计者的要求,HTML添加 ...
- .net网站开发(一):1.input表单元素
其实,在半年前我对网站开发还是完全不感冒的,不是没认识,而是只认识到表面.我以为网站模型就那几样,新闻.论坛.博客啥的,仿个站出来有什么意思?但现在我是知道了,大多应用开发还是采用B/S架构的,包括服 ...