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 中的观察者模式. 不熟悉观察者 ...
随机推荐
- JFrame编程
最基础的窗口 public test1() { setTitle("test1"); setSize(300,300); //设置窗口大小 setLocation(300,300) ...
- eucMenu
- linux考试基础知识测验
Linux系统管理基础测试(100分钟) 姓名: 座位号: 一.单项选择题:(每小题0.5分,共计30分) 1. cron 后台常驻程序 (daemon) 用于:D A. 负责文件在网络中的共 ...
- java 面试基础典型题及答案
1.switch能否作用在byte.int.long.String? 答案:switch能作用在byte.int.enum常量, 补充:jdk7可以作用在String上 2.short s = 1; ...
- [Qt] fontawesome图标
fontawesome图标 fontawesome是一个图标的集合,里面有好多的图标,使用起来也还是非常方便的. 图标信息可以到官网去查:http://fontawesome.io/cheatshee ...
- html打开个人QQ聊天页面
打开qq聊天页面(有权限需要添加好友) <a href="tencent://message/?uin=1578929883&Site=&Menu=yes" ...
- 1042. Shuffling Machine (20) - sstream实现数字转字符串
题目例如以下: Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffli ...
- unbantu相关笔记
很多项目使用的系统是centos或者redhat,最近有一个项目使用的系统竟然是阿里云unbantu,不知道他们负责人怎么想的,明明有centos,非要用unbantu.抱怨到此,unbantu的学习 ...
- C/C++中的空类及抽象类大小
代码: #include <iostream> using namespace std; struct A{ }; struct B{ int i; }; class C:B{ ; }; ...
- uva 10061 How many zero's and how many digits ?
How many zeros and how many digits? Input: standard input Output: standard output Given a decimal in ...