2014-09-01 62 views
-1

我想從數據庫中動態地創建單選按鈕我已經瀏覽這個問題,但不工作。當我調用函數從數據庫獲取值並傳遞應用程序上下文的值時,函數被調用,並且在調用函數或返回值時沒有錯誤,但是當視圖返回線性佈局時,它將顯示初始創建的空白xml。我需要幫助來展示這些價值觀。提前致謝。 這裏是代碼:動態單選按鈕無線電組和視圖不工作

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_result); 
    StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 

    dbcon=DBConnection.instance(this); 
    dbcon.connect("ip:1433", "pas", "login", "db"); 
    Context cntxt=getApplicationContext(); 

    try { 

     Thread.sleep(10); 

     String city=getIntent().getExtras().getString("city"); 
     String choice=getIntent().getExtras().getString("choice"); 

     LinearLayout ll=new LinearLayout(cntxt); 
     ll.setOrientation(LinearLayout.VERTICAL); 
     ImageView iv=new ImageView(cntxt); 
     RadioGroup.LayoutParams prams=new RadioGroup.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); 
     prams.setMargins(0, 2, 0, 2); 


     RadioGroup rg=(RadioGroup)findViewById(R.id.RRG); 

     ll=dbcon.GetResultList(rg,choice,city,iv,cntxt,ll); 

     ((ViewGroup)findViewById(R.id.RRG)).addView(ll); 



    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

這裏是我從活動調用的函數:

@SuppressLint("NewApi") 
public LinearLayout GetResultList(RadioGroup rg,String choice,String city,ImageView iv,Context context, LinearLayout ll) { 
    // TODO Auto-generated method stub 

    if(conn==null){ 

    } 
    try{ 
     RadioButton rb; 
     rg.setBackgroundColor(Color.LTGRAY); 

      Statement st=conn.createStatement(); 
      ResultSet rs=st.executeQuery("select * from AM_NATIONAL where ca_city="+city+ "order by cvg_count"); 

      while (rs.next()){ 
       rb=new RadioButton(context); 
       rb.setId(rs.getInt(1)); 
       rb.setText(rs.getString(2)+"\n"); 
       rb.setText(rs.getString(3)+"\n"); 
       rb.setText(rs.getInt(8)); 

       byte[] photo=rs.getBytes(4); 
       Bitmap bitmap; 
       bitmap=BitmapFactory.decodeByteArray(photo, 0, photo.length); 
       iv.setImageBitmap(bitmap); 

       rb.setEnabled(false); 
       rg.addView(iv); 
       rg.addView(rb); 
       ll.addView(rg); 

      } 
      rs.close(); 
      st.close(); 
     } 
     catch(SQLException e){ 

     } 
    return ll; 

} 

,這裏是我使用這個XML文件:

<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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".ResultActivity" > 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/imageView1" 
     android:layout_alignParentLeft="true" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 

      <RadioGroup 
       android:id="@id/RRG" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

      </RadioGroup> 

     </LinearLayout> 
    </ScrollView> 


</RelativeLayout> 
+0

爲什麼你要在Radio組中添加imageview? – 2014-09-01 05:51:21

+0

,因爲我想顯示數據庫中的圖像與單選按鈕 – Awais 2014-09-01 06:30:50

+0

但問題是不存在的問題是,然後線性佈局返回它是空的 – Awais 2014-09-01 06:31:51

回答

1

斷點在線
ResultSet rs=st.executeQuery 檢查您的rs在調試模式下是否爲空如果rs將爲空d默認視圖佈局,所以它不會返回null佈局

+0

感謝您的幫助 – Awais 2014-09-01 13:46:49