2014-09-01 24 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