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. java decompiler如何去掉行号

    今天想反编译jar包保存源代码,默认前面加了行号,不知道这个小工具如何设置去掉行号? 反编译后: 找到它的安装路径,我的是:C:\Program Files\decomp.然后将该路径加入到环境变量p ...

  2. eclipse,tomcat部署web项目,以及本地文件访问

    1.直接把项目复制到Tomcat安装目录的webapps目录中,这是最简单的一种Tomcat项目部署的方法,也是初学者最常用的方法. 2.在tomcat安装目录中有一个conf文件夹,打开此文件夹,其 ...

  3. js面向对象的五种写法

    第一种: //第1种写法 function Circle(r) { this.r = r; } Circle.PI = 3.14159; Circle.prototype.area = functio ...

  4. Android初学:联系创建Activity

    public class Activity2 extends Activity{ @Override protected void onCreate(Bundle savedInstanceState ...

  5. Building bridges_hdu_4584(排序).java

    Building bridges Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...

  6. TI C66x DSP 系统events及其应用 - 5.1(QM accumulator的配置)

    以下解说在详细应用中,event与中断ISR的设置.以对QM的queue监控产生中断(不是EXCEP)为例,主要包含配置QM accumulator(用于监控QM queue)与配置ISR(ISR与e ...

  7. LR翻页脚本并在每页实现业务操作

    性能需求:在列表中删除后有记录,或对列表中的每条记录进行操作(如点击每条记录的“单号”进入订单详情页面,或在列表中对每条记录进行“启用”.“停止”操作) 举例:Vuser脚本模拟用户在订单列表中点击每 ...

  8. Unity发送短信

    闲来无事,觉得用uinity来发送短信挺有意思的,所以自己差了点资料,看看能否实现,结果还真的可以!废话不多说,直接码! 1,新建一空工程,我们就简单的使用UGUI搭建一个丑陋的界面吧! 2,界面极其 ...

  9. C/C++笔试准备(2)

    问题:编辑距离,是指将一个字符串变为另一个字符串,仅可以3种操作:修改一个字符,删除一个字符,插入一个字符.the变成that:删除e,插入a,插入t.20’ 实现编辑距离算法. 解算:利用动态规划的 ...

  10. Linux查看网络即时网速

    sar -n DEV 1 100 1代表一秒统计并显示一次 100代表统计一百次 使用ntop等工具,就更方便简单了,不过这个更灵活 P.S. sar在sysstat包 来源:http://www.c ...