Android ListView中動(dòng)態(tài)添加RaidoButton的實(shí)例詳解
Android ListView中動(dòng)態(tài)添加RaidoButton的實(shí)例詳解
這里講解的內(nèi)容是:從數(shù)據(jù)庫中取得數(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)代碼
說明:這里沒有將需要引入的包貼出來,只是列舉了其中重要的部分。
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);
//保證線程安全,防止多線程同時(shí)運(yùn)行
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectAll()
.build());
//初始化DBHelper
final DBHelper dbHelper = new DBHelper(this);
//查詢業(yè)務(wù)類型語句
String sql = "select * from t_Test";
final Cursor cur = dbHelper.select(sql);
// 防止數(shù)據(jù)庫中無數(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í)例,如有疑問請(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)擊無響應(yīng)的解決方法
- android ListView內(nèi)數(shù)據(jù)的動(dòng)態(tài)添加與刪除實(shí)例代碼
相關(guān)文章
Android使用自定義PageTransformer實(shí)現(xiàn)個(gè)性的ViewPager動(dòng)畫切換效果
這篇文章主要介紹了Android使用自定義PageTransformer實(shí)現(xiàn)個(gè)性的ViewPager切換動(dòng)畫,具有很好的參考價(jià)值,一起跟隨小編過來看看吧2018-05-05
Android實(shí)現(xiàn)拍照截取和相冊(cè)圖片截取
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)拍照截取和相冊(cè)獲取圖片截取,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android 8.0版本更新無法自動(dòng)安裝問題的解決方法
這篇文章主要為大家詳細(xì)介紹了Android 8.0版本更新無法自動(dòng)安裝問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android 中View.onDraw(Canvas canvas)的使用方法
這篇文章主要介紹了Android 中View.onDraw(Canvas canvas)的使用方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
Android實(shí)現(xiàn)短信驗(yàn)證碼輸入框
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)短信驗(yàn)證碼輸入框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
玩轉(zhuǎn)AppBarLayout實(shí)現(xiàn)更酷炫的頂部欄
玩轉(zhuǎn)AppBarLayout,實(shí)現(xiàn)更酷炫的頂部欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

