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 中的观察者模式. 不熟悉观察者 ...
随机推荐
- Borland license information was found,but it is not valid for delphi.
The start Delphi7 come amiss: Borland license information was found,but it is not valid for delphi. ...
- 关于iostream的效率问题
前言 经常有人说iostream的速度慢,IO流比stdio的慢多了.但是有人测试过的,iostream的速度是超过stdio的. 测试结果 /* C */#include <stdio. ...
- xcode忽略警告
1.对整个文件使用 你的工程 -> 你的target -> Build Phases -> Compile Sources -> 搜索要忽略警告的文件名,在 Compiler ...
- 【POJ2242】The Circumference of the Circle(初等几何)
已知三点坐标,算圆面积. 使用初等几何知识,根据海伦公式s = sqrt(p(p - a)(p - b)(p - c)) 和 外接圆直径 d = a * b * c / (2s) 来直接计算. #in ...
- 高性能以太网芯片W5500 数据手册 V1.0(一)
W5500 W5500 是一款全硬件 TCP/IP 嵌入式以太网控制器,为嵌入式系统提供了更加简易的互联网连接方案.W5500 集成了 TCP/IP 协议栈,10/100M 以太网数据链路层(MAC) ...
- css如何使背景图片水平居中
CSS中定位背景图片的属性是:background-position,用法background-position 属性设置背景图像的起始位置. 你要水平居中可以: div{background-pos ...
- 利用Excel批量高速发送电子邮件
利用Excel批量高速发送电子邮件,分两步: 1. 准备待发送的数据: a.) 打开Excel,新建Book1.xlsx b.) 填入以下的内容, 第一列:接收人,第二列:邮件标题,第三列:正文,第四 ...
- javaweb中去除某个get方式的参数,并且返回路径
String requestURL = request.getRequestURL() + ""; // String queryString = request.getQuery ...
- Mybatis 插入操作时获取主键 (Oracle 触发器与SEQ)
1.通过Oracle序列 -- Create sequence create sequence SEQ_DW_EWSYSTEM minvalue 1 maxvalue 9999999999999999 ...
- zookeeper应用——集中配置管理系统的实现
http://blog.csdn.net/huangfengxiao/article/details/8844239