博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ListView中RadioButton实现单项选择
阅读量:6906 次
发布时间:2019-06-27

本文共 2824 字,大约阅读时间需要 9 分钟。

1:FragmentHack5.java

public class FragmentHack5 extends Fragment {    View view;    ListView lvCountries;    Button btnShow;    CountryListAdapter adapter;    List
list; @Override public void onAttach(Activity activity) { super.onAttach(activity); list = new ArrayList
(); 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
{ int resourceId; int choiceId = -1; public CountryListAdapter(Context context, int resourceId, List
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:运行结果

转载地址:http://huldl.baihongyu.com/

你可能感兴趣的文章
利用bentley view将Revit模型输出为3D PDF文档
查看>>
Log4j配置详解
查看>>
nodejs 笔记
查看>>
Fastjson是一个Java语言编写的高性能功能完善的JSON库。
查看>>
【机器学习算法-python实现】决策树-Decision tree(1) 信息熵划分数据集
查看>>
最新的goldengate monitor 12.1.3已经发布
查看>>
ASP.NET防止用户多次登录的方法
查看>>
2D多边形碰撞器优化器
查看>>
webBrowser 模拟登录
查看>>
C# 采用线程重绘图形要点记录
查看>>
About Technology Research
查看>>
java + jni + mingw实例开发(基于命令行窗口模式)
查看>>
【LeetCode】7. Reverse Integer
查看>>
Struts2总结
查看>>
CentOS6.5菜鸟之旅:VIM插件NERDtree初探
查看>>
【记录】ASP.NET MVC RegisterBundles
查看>>
odex反编译dex异常 Cannot locate boot class path file /system/framework/core.odex
查看>>
【记录】AutoMapper Project To not support ResolveUsing
查看>>
IOS开发基础知识--碎片3
查看>>
触发隐藏链接进行文件下载,click无响应
查看>>