亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android ListView中動(dòng)態(tài)添加RaidoButton的實(shí)例詳解

 更新時(shí)間:2017年08月16日 11:37:27   投稿:lqh  
這篇文章主要介紹了Android ListView中動(dòng)態(tài)添加RaidoButton的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

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ì)本站的支持!

相關(guān)文章

最新評(píng)論