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实现单项选择的更多相关文章

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

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

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

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

  3. 如何在Android的ListView中构建CheckBox和RadioButton列表(支持单选和多选的投票项目示例)

    引言 我们在android的APP开发中有时候会碰到提供一个选项列表供用户选择的需求,如在投票类型的项目中,我们提供一些主题给用户选择,每个主题有若干选项,用户对这些主题的选项进行选择,然后提交. 本 ...

  4. 单项选择RadioButton和多项选择CheckBox的使用

     在Android中,可以通过RadioButton和RadioGroup的组合来实现单项选择的效果.而多项选择则是通过CheckBox来实现的. 1.单项选择RadioButton 我们知道,一 ...

  5. 阅读《Android 从入门到精通》(10)——单项选择

    单项选择(RadioGroup) RadioGroup 是 LinearLayout 的子类,继承关系例如以下: android.view.ViewGroup android.widget.Linea ...

  6. Android 如何在 ListView 中更新 ProgressBar 进度

    =======================ListView原理============================== Android 的 ListView 的原理打个简单的比喻就是: 演员演 ...

  7. C#-WinForm-ListView-表格式展示数据、如何将数据库中的数据展示到ListView中、如何对选中的项进行修改

    在展示数据库中不知道数量的数据时怎么展示最好呢?--表格 ListView - 表格形式展示数据 ListView 常用属性 HeaderStyle - "详细信息"视图中列标头的 ...

  8. C#中将ListView中数据导出到Excel

    首先 你需要添加引用Microsoft Excel 11.0 Object Library 添加方法:选择项目->引用->右击“添加引用”->选择COM 找到上面组件—>点击“ ...

  9. 最熟悉的陌生人:ListView 中的观察者模式

    RecyclerView 得宠之前,ListView 可以说是我们用的最多的组件.之前一直没有好好看看它的源码,知其然不知其所以然. 今天我们来窥一窥 ListView 中的观察者模式. 不熟悉观察者 ...

随机推荐

  1. 磁珠(FB)的选用

    1. 磁珠(FB)的单位是欧姆,而不是亨特,这一点要特别注意.因为磁珠的单位是按照它在某一频率 产生的阻抗来标称的,阻抗的单位也是欧姆.磁珠的 DATASHEET上一般会提供频率和阻抗的特性曲线图,一 ...

  2. Altium Designer (protel) 各版本“故障”随谈

    Altium 的版本很多,每个版本都或多或少有些可容忍或可不容忍的问题,此贴只是希望各位能将遇到的问题写出来,只是希望 给还在使用 altium 的网友一些参考,也希望有些能被 altium 所接受@ ...

  3. bzoj1681[Usaco2005 Mar]Checking an Alibi 不在场的证明

    Description A crime has been comitted: a load of grain has been taken from the barn by one of FJ's c ...

  4. JS监听组合按键

    有些时候,我们需要在网页上,增加一些快捷按键,方便用户使用一些常用的操作,比如:保存,撤销,复制.粘贴等等. 下面简单梳理一下思路: 我们所熟悉的按键有这么集中类型: 单独的按键操作,如:delete ...

  5. hibernate 对 sql server 2005 分页改进

    Hibernate 可以实现分页查询 如下 Query q = session.createQuery("from Cat as c"); q.setFirstResult(100 ...

  6. Linux远程访问windows时,出现"连接被对端重置"错误

    1.sudo apt-get install rdesktop 需要下载 152 kB 的软件包.       解压缩后会消耗掉 512 kB 的额外空间. 2.运行时出现错误 root@oskey- ...

  7. poj 2046 Gap(bfs+hash)

    Description Let's play a card game called Gap. You have cards labeled with two-digit numbers. The fi ...

  8. html--鼠标控制DIV移动

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  9. BootStrap学习之先导篇——响应式网页

    Bootstrap学习之前,要知道响应式网页的原理. 1.什么是响应式网页? 一个页面,可以根据浏览设备的不同,以及特性的不同,而自动改变布局.大小等.使得在不同的设备上上都可以呈现优秀的界面. 优点 ...

  10. Go程序GC优化经验分享

    http://1234n.com/?post/yzsrwa 最近一段时间对<仙侠道>的服务端进行了一系列针对GC的调优,这里跟各位分享一下调优的经验. 游戏第一次上线的时候,大部分精力都投 ...