tablelayout中动态添加的行不可见

时间:2013-09-24 11:53:19

标签: android android-layout android-tablelayout

我有一个带有标题行的表,用xml设计。现在我想在表中添加数据(我从json数组中获取)。 我需要垂直滚动数据行而不影响标题行,所以我在标题行表的下方创建了另一个tablelayout。然后,对于每个新行的每个字段,我在java文件中设置布局参数,然后将该行添加到第二个表格布局。 但是在日志中打印数据时没有可见的行。我无法弄清楚我的错误在哪里。

XML:

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

    <TableLayout
        android:id="@+id/rv_table_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableRow 
            android:id="@+id/rv_head_row"
            >
                        <TextView
                            android:id="@+id/rv_head_desc"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"

                            android:background="#999"
                            android:gravity="center_horizontal|center_vertical"

                            android:maxLines="1"
                            android:padding="15dp"
                            android:text="@string/Desc"
                            android:textSize="18sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/rv_head_unit"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"

                            android:background="#999"
                            android:gravity="center_horizontal|center_vertical"

                            android:maxLines="1"
                            android:padding="15dp"
                            android:text="@string/Unit"
                            android:textSize="18sp"
                            android:textStyle="bold" />

                          <TextView
                            android:id="@+id/rv_head_type"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"

                            android:background="#999"
                            android:gravity="center_horizontal|center_vertical"

                            android:maxLines="1"
                            android:padding="15dp"
                            android:text="@string/Type"
                            android:textSize="18sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/rv_head_range"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"

                            android:background="#999"
                            android:gravity="center_horizontal|center_vertical"

                            android:maxLines="1"
                            android:padding="15dp"
                            android:text="@string/Range"
                            android:textSize="18sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/rv_head_rv"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"

                            android:background="#999"
                            android:gravity="center_horizontal|center_vertical"
                            android:maxLines="1"
                            android:padding="15dp"
                            android:text="@string/RV"
                            android:textSize="18sp"
                            android:textStyle="bold" />
                        <TextView
                            android:id="@+id/rv_head_remarks"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:background="#999"
                            android:gravity="center_horizontal|center_vertical"
                            android:maxLines="1"
                            android:padding="15dp"
                            android:text="@string/Remarks"
                            android:textSize="18sp"
                            android:textStyle="bold" />
        </TableRow>
    </TableLayout>

    <ScrollView android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
    <TableLayout
        android:id="@+id/rv_data_table_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        </TableLayout>
    </ScrollView>

</LinearLayout>

JAVA CODE:

protected void onCreate(Bundle savedInstanceState) {
        Log.i("result value","in result value");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.results_value);

        /*
         * connecting with xml
         */

        rv_table = (TableLayout) findViewById(R.id.rv_table_layout);
        rv_data_table = (TableLayout) findViewById(R.id.rv_data_table_layout);
        rv_head_row = (TableRow) findViewById(R.id.rv_head_row);

        desc = (TextView)findViewById(R.id.rv_head_desc);
        unit= (TextView)findViewById(R.id.rv_head_unit);
        type= (TextView)findViewById(R.id.rv_head_type);
        range = (TextView)findViewById(R.id.rv_head_range);
        rv= (TextView)findViewById(R.id.rv_head_rv);
        remarks= (TextView)findViewById(R.id.rv_head_remarks);;


        /*
         * getting data from server
         */
        final Bundle extras = getIntent().getExtras();
        try {
            js = new JSONObject(){
                {

                        put("CaseId", extras.getString("CaseId"));

                }
            };

        } catch (JSONException e1) {
            e1.printStackTrace();
        }

        Log.v("~~~~~===data is", js.toString());
        service = new Services(ResultValueActivity.this);
        String rslt=service.getResultValue(js);
        try {
            JSONObject rs=new JSONObject(rslt);
            String p = rs.get("d").toString(); // this is done to
            // remove
            // starting d: i.e seprating
            //      first key value pairs
            JSONObject temp = new JSONObject(p);
            values = temp.getJSONArray("Table"); // values is a array of JSON

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


        /*
         * data received, now starting filling values from json array to table
         */
        JSONObject test;
        TableRow row;
        TextView desc1,unit1,type1,range1;
        EditText rv1;
        Button remarks1;
        for(int i=0;i<values.length();i++)
        {
            try{
                test = values.getJSONObject(i);
                row = new TableRow(ResultValueActivity.this);
                desc1 = new TextView(ResultValueActivity.this);
                unit1 = new TextView(ResultValueActivity.this);
                type1 = new TextView(ResultValueActivity.this);
                range1 = new TextView(ResultValueActivity.this);
                rv1 = new EditText(ResultValueActivity.this);
                remarks1 = new Button(ResultValueActivity.this);

                /*
                 *  setting text in fields
                 */


                desc1.setText(test.getString("Description"));
                unit1.setText(test.getString("Unit"));
                type1.setText(test.getString("ResultType"));
                range1.setText(test.getString("RangeValue"));
                rv1.setText(test.getString("ResultValue"));
                remarks1.setText("Remarks");

                desc1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
                unit1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
                type1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
                range1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
                rv1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
                remarks1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
                row.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
Log.i(i+" result value test====>", test.toString());


                row.addView(desc1);
                row.addView(unit1);
                row.addView(type1);
                row.addView(range1);
                row.addView(rv1);
                row.addView(remarks1);
                rv_data_table.addView(row,new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
                rv_data_table.setVisibility(View.VISIBLE);

                Log.i(i+" desc value test====>", desc1.getText().toString());
                Log.i(i+" unit value test====>", unit1.getText().toString());
            }
            catch(JSONException jse)
            {
                jse.printStackTrace();
            }}
        Log.i("result Value"," it is done :D :D");
   }

日志准确打印所有值

1 个答案:

答案 0 :(得分:1)

第一个表格布局具有match_parent的高度,因此没有为第二个表格布局提供空间,所以我将其更改为包装内容