Android ListView中動(dòng)態(tài)添加RaidoButton的實(shí)例詳解
Android ListView中動(dòng)態(tài)添加RaidoButton的實(shí)例詳解
這里講解的內(nèi)容是:從數(shù)據(jù)庫(kù)中取得數(shù)據(jù),將這些數(shù)據(jù)的value值賦值給Radiobutton的text屬性,將這些數(shù)據(jù)的key值賦值給radiobutton的key值。同時(shí)實(shí)現(xiàn)點(diǎn)擊一整行,更換radiobutton選擇。
XML代碼:主要是添加一個(gè)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>
下面是后臺(tái)代碼
說(shuō)明:這里沒有將需要引入的包貼出來(lái),只是列舉了其中重要的部分。
public class TestActivity extends Activity { //初始化字符數(shù)組:arrayValue用于存放數(shù)據(jù)庫(kù)中取得的key值,arrayText用于存放數(shù)據(jù)庫(kù)中取得的Value值 String[] arrayValue; String[] arrayText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_item); //保證線程安全,防止多線程同時(shí)運(yùn)行 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectAll() .build()); //初始化DBHelper final DBHelper dbHelper = new DBHelper(this); //查詢業(yè)務(wù)類型語(yǔ)句 String sql = "select * from t_Test"; final Cursor cur = dbHelper.select(sql); // 防止數(shù)據(jù)庫(kù)中無(wú)數(shù)據(jù)出錯(cuò) if (cur != null && cur.getCount() > 0) { arrayText = new String[cur.getCount()]; arrayValue = new String[cur.getCount()]; // 移動(dòng)到第一條記錄 cur.moveToFirst(); int i = 0; int index = 0; // 遍歷Cursor,把數(shù)據(jù)添加到數(shù)組中 while (!cur.isAfterLast()) { index = cur.getColumnIndex("要查找的列名"); arrayText[i] = cur.getString(index); index = cur.getColumnIndex("id"); arrayValue[i] = cur.getString(index); i++; cur.moveToNext(); // 移動(dòng)到下一條記錄 } } String[] contentString = arrayText; //創(chuàng)建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監(jiān)聽事件 mylist.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { //將選擇的radiobutton的Value值傳入到實(shí)體類ApplicationData中 appData.BusinessID =arrayValue[arg2]; } }); //設(shè)置選擇模式:?jiǎn)芜x模式 mylist.setChoiceMode(ListView.CHOICE_MODE_SINGLE); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_business_item, menu); return true; } }
以上就是Android ListView中動(dòng)態(tài)添加RaidoButton的實(shí)例,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- android開發(fā)教程之listview使用方法
- android開發(fā)之橫向滾動(dòng)/豎向滾動(dòng)的ListView(固定列頭)
- Android自定義Adapter的ListView的思路及代碼
- Android下拉刷新ListView——RTPullListView(demo)
- Android之帶group指示器的ExpandableListView(自寫)
- android中ListView多次刷新重復(fù)執(zhí)行g(shù)etView的解決方法
- Android ListView的item背景色設(shè)置和item點(diǎn)擊無(wú)響應(yīng)的解決方法
- android ListView內(nèi)數(shù)據(jù)的動(dòng)態(tài)添加與刪除實(shí)例代碼
相關(guān)文章
Android實(shí)現(xiàn)關(guān)機(jī)與重啟的幾種方式(推薦)
這篇文章主要介紹了Android實(shí)現(xiàn)關(guān)機(jī)與重啟的幾種方式(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Android實(shí)現(xiàn)登錄注冊(cè)頁(yè)面(下)
這篇文章主要介紹了Android實(shí)現(xiàn)登錄注冊(cè)頁(yè)面的第二篇,實(shí)現(xiàn)驗(yàn)證登錄和記住密碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android 用HttpURLConnection訪問(wèn)網(wǎng)絡(luò)的方法
下面小編就為大家分享一篇Android 用HttpURLConnection訪問(wèn)網(wǎng)絡(luò)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android常見XML轉(zhuǎn)義字符(總結(jié))
下面小編就為大家?guī)?lái)一篇Android常見XML轉(zhuǎn)義字符(總結(jié))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04簡(jiǎn)單介紹Android開發(fā)中的Activity控件的基本概念
這篇文章主要介紹了Android開發(fā)中的Activity控件的基本概念,Activity控件的使用是安卓開發(fā)的基礎(chǔ)之一,需要的朋友可以參考下2015-12-12Android開發(fā)中滑動(dòng)分頁(yè)功能實(shí)例詳解
這篇文章主要介紹了Android開發(fā)中滑動(dòng)分頁(yè)功能,結(jié)合實(shí)例形式詳細(xì)分析了Android滑動(dòng)分頁(yè)功能的具體步驟與相關(guān)實(shí)現(xiàn)技巧,代碼中備有詳盡的注釋便于理解,需要的朋友可以參考下2017-10-10