Android ListView中動態(tài)添加RaidoButton的實例詳解
Android ListView中動態(tài)添加RaidoButton的實例詳解
這里講解的內(nèi)容是:從數(shù)據(jù)庫中取得數(shù)據(jù),將這些數(shù)據(jù)的value值賦值給Radiobutton的text屬性,將這些數(shù)據(jù)的key值賦值給radiobutton的key值。同時實現(xiàn)點擊一整行,更換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 { //初始化字符數(shù)組:arrayValue用于存放數(shù)據(jù)庫中取得的key值,arrayText用于存放數(shù)據(jù)庫中取得的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); //查詢業(yè)務類型語句 String sql = "select * from t_Test"; final Cursor cur = dbHelper.select(sql); // 防止數(shù)據(jù)庫中無數(shù)據(jù)出錯 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,把數(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(); // 移動到下一條記錄 } } 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值傳入到實體類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中動態(tài)添加RaidoButton的實例,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- android開發(fā)教程之listview使用方法
- android開發(fā)之橫向滾動/豎向滾動的ListView(固定列頭)
- Android自定義Adapter的ListView的思路及代碼
- Android下拉刷新ListView——RTPullListView(demo)
- Android之帶group指示器的ExpandableListView(自寫)
- android中ListView多次刷新重復執(zhí)行getView的解決方法
- Android ListView的item背景色設置和item點擊無響應的解決方法
- android ListView內(nèi)數(shù)據(jù)的動態(tài)添加與刪除實例代碼
相關文章
Android 用HttpURLConnection訪問網(wǎng)絡的方法
下面小編就為大家分享一篇Android 用HttpURLConnection訪問網(wǎng)絡的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01簡單介紹Android開發(fā)中的Activity控件的基本概念
這篇文章主要介紹了Android開發(fā)中的Activity控件的基本概念,Activity控件的使用是安卓開發(fā)的基礎之一,需要的朋友可以參考下2015-12-12