1

我正在尝试使用布局充气器将表格行添加到表格中。

这是我的 XML 文件,名为 customlistviewitem.xml

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:id="@+id/LinearLayout01"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <ScrollView
   android:id="@+id/ScrollView1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_marginTop="75dp">
  <TableLayout 
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:id="@+id/myTableLayout1">

<TableRow android:layout_marginTop="1dp" android:layout_marginLeft="10dp">

    </TableRow>
    </TableLayout>

    </ScrollView>
    </LinearLayout>

这是我用来向表中添加行的代码

    LinearLayout l = (LinearLayout) findViewById(R.id.mylayout1);
            LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            JSONArray jArray;
            try {
                jArray = new JSONArray(result);

            JSONObject json_data=null;
            for(int i=0;i<jArray.length();i++){
                //Log.e("Carlist","Result" +result);
                json_data = jArray.getJSONObject(i);
                   int ct_id = json_data.getInt("id");
                   String make = json_data.getString("make");
                   String model = json_data.getString("model");
                   String price = json_data.getString("price");

                   TableRow tr = new TableRow(this);
                         tr.setId(ct_id);
                         tr.setClickable(true);

                         tr.setOnClickListener(new  OnClickListener() {

                                @Override
                                public void onClick(final View v) {

                                    String sdet_id;
                                    int det_id;
                        //          v.setBackgroundColor(Color.GRAY);
                                    det_id = v.getId();
                                    sdet_id = String.valueOf(det_id);
                                    final Intent i = new Intent();
                                    i.setClassName("demo.example.com", "demo.example.com.cardetails");
                                    i.putExtra("Det_id", sdet_id);
                                    startActivity(i);

                          //        v.setBackgroundColor(Color.TRANSPARENT);
                                    // TODO Auto-generated method stub
                                }
                            });
                         TableLayout.LayoutParams tableRowParams=
                              new TableLayout.LayoutParams
                              (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);

                            int leftMargin=20;
                            int topMargin=10;
                            int rightMargin=15;
                            int bottomMargin=20;

                            tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);

                            tr.setLayoutParams(tableRowParams);

                              /* Create a Button to be the row-content. */
                         ImageView myimage = new ImageView(this);

                         BitmapFactory.Options bmOptions;
                        bmOptions = new BitmapFactory.Options();
                        bmOptions.inSampleSize = 1;
                        Bitmap bm = LoadImage(image_URL, bmOptions);
                        myimage.setImageBitmap(bm);
                         tr.addView(myimage);
                         TextView tmake=new TextView(this);
                        // tmake.setText(make);
                         tmake.setText(Html.fromHtml("<H1>" + make));
                         tr.addView(tmake); 

                         TextView tmodel=new TextView(this);
                         tmodel.setText(Html.fromHtml("<b><H1>" + "&nbsp;&nbsp;" + "€" + price + "</b></H1></br></br>" ));
                         tr.addView(tmodel);



                    /* Add row to TableLayout. */
                     //  tr.setPadding(50, 0, 0, 0);
                         tl.addView(tr);
                         View v = new View(this);
                         v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                         v.setBackgroundColor(Color.rgb(51, 51, 51));
                         tl.addView(v);

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

这不是添加表格行。我需要做什么来添加行和文本项?

4

1 回答 1

1

问题似乎是 LayoutParams 设置为 TableRow,并且视图被添加到 TableRow,尝试评论设置布局参数代码,然后检查结果。

LinearLayout l = (LinearLayout) findViewById(R.id.mylayout1);
            LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            JSONArray jArray;
            try {
                jArray = new JSONArray(result);

            JSONObject json_data=null;
            for(int i=0;i<jArray.length();i++){
                //Log.e("Carlist","Result" +result);
                json_data = jArray.getJSONObject(i);
                   int ct_id = json_data.getInt("id");
                   String make = json_data.getString("make");
                   String model = json_data.getString("model");
                   String price = json_data.getString("price");

                   TableRow tr = new TableRow(this);
                         tr.setId(ct_id);
                         tr.setClickable(true);

                         tr.setOnClickListener(new  OnClickListener() {

                                @Override
                                public void onClick(final View v) {

                                    String sdet_id;
                                    int det_id;
                        //          v.setBackgroundColor(Color.GRAY);
                                    det_id = v.getId();
                                    sdet_id = String.valueOf(det_id);
                                    final Intent i = new Intent();
                                    i.setClassName("demo.example.com", "demo.example.com.cardetails");
                                    i.putExtra("Det_id", sdet_id);
                                    startActivity(i);

                          //        v.setBackgroundColor(Color.TRANSPARENT);
                                    // TODO Auto-generated method stub
                                }
                            });


                              /* Create a Button to be the row-content. */
                         ImageView myimage = new ImageView(this);

                         BitmapFactory.Options bmOptions;
                        bmOptions = new BitmapFactory.Options();
                        bmOptions.inSampleSize = 1;
                        Bitmap bm = LoadImage(image_URL, bmOptions);
                        myimage.setImageBitmap(bm);
                         tr.addView(myimage);
                         TextView tmake=new TextView(this);
                        // tmake.setText(make);
                         tmake.setText(Html.fromHtml("<H1>" + make));
                         tr.addView(tmake); 

                         TextView tmodel=new TextView(this);
                         tmodel.setText(Html.fromHtml("<b><H1>" + "&nbsp;&nbsp;" + "€" + price + "</b></H1></br></br>" ));
                         tr.addView(tmodel);



                    /* Add row to TableLayout. */
                     //  tr.setPadding(50, 0, 0, 0);
                         tl.addView(tr);
                         View v = new View(this);

                         v.setBackgroundColor(Color.rgb(51, 51, 51));
                         tl.addView(v);

            }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
于 2012-07-31T10:31:42.050 回答