
AndroID ListVIEw中动态添加RaIDobutton的实例详解
这里讲解的内容是:从数据库中取得数据,将这些数据的value值赋值给Radiobutton的text属性,将这些数据的key值赋值给radiobutton的key值。同时实现点击一整行,更换radiobutton选择。
XML代码:主要是添加一个ListVIEw控件
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > <ListVIEw androID:ID="@+ID/ListVIEw01" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"/> </relativeLayout>
下面是后台代码
说明:这里没有将需要引入的包贴出来,只是列举了其中重要的部分。
public class TestActivity extends Activity { //初始化字符数组:arrayValue用于存放数据库中取得的key值,arrayText用于存放数据库中取得的Value值 String[] arrayValue; String[] arrayText; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_test_item); //保证线程安全,防止多线程同时运行 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectdiskReads() .detectdiskWrites() .detectAll() .build()); //初始化DBHelper final DBHelper dbHelper = new DBHelper(this); //查询业务类型语句 String sql = "select * from t_Test"; final Cursor cur = dbHelper.select(sql); // 防止数据库中无数据出错 if (cur != null && cur.getCount() > 0) { arrayText = new String[cur.getCount()]; arrayValue = new String[cur.getCount()]; // 移动到第一条记录 cur.movetoFirst(); int i = 0; int index = 0; // 遍历Cursor,把数据添加到数组中 while (!cur.isAfterLast()) { index = cur.getColumnIndex("要查找的列名"); arrayText[i] = cur.getString(index); index = cur.getColumnIndex("ID"); arrayValue[i] = cur.getString(index); i++; cur.movetoNext(); // 移动到下一条记录 } } String[] contentString = arrayText; //创建ListvIEw适配器,存放取得的radiobutton ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( this,androID.R.layout.simple_List_item_single_choice,contentString); ListVIEw myList = (ListVIEw)findVIEwByID(R.ID.ListVIEw01); myList.setAdapter(arrayAdapter); //radiobutton监听事件 myList.setonItemClickListener(new OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> arg0,VIEw arg1,int arg2,long arg3) { //将选择的radiobutton的Value值传入到实体类ApplicationData中 appData.BusinessID =arrayValue[arg2]; } }); //设置选择模式:单选模式 myList.setChoiceMode(ListVIEw.CHOICE_MODE_SINGLE); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_business_item,menu); return true; } } 以上就是AndroID ListVIEw中动态添加RaIDobutton的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android ListView中动态添加RaidoButton的实例详解全部内容,希望文章能够帮你解决Android ListView中动态添加RaidoButton的实例详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)