动态单选按钮无线电组和视图不起作用

时间:2014-09-01 05:31:50

标签: android android-layout jdbc android-view

我想从数据库中动态创建单选按钮我已经浏览了这个问题但是没有工作。当我调用函数从数据库中获取值并传递应用程序上下文的值时,函数被调用并且在调用函数或返回值时没有错误,但是当视图返回线性布局时,它显示最初创建的空白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>

1 个答案:

答案 0 :(得分:1)

线路上的断点
    ResultSet rs=st.executeQuery 检查rs在调试模式下是否为空,如果rs为null则向Layout添加默认视图,这样它就不会返回null布局