HimanshuR
HimanshuR

Reputation: 1428

dynamically added rows in tablelayout are not visible

i have a table with header row designed in xml. Now i want to add data in the table ( which i get from json array). I need to scroll data rows vertically without affecting header row, so i created another tablelayout just below the table of header row. Then for each field of each new row, i set layout params in java file and then add the row to second table layout. But no rows are visible while data is getting printed in the log.I can't figure out where is my mistake.

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");
   }

Log is printing all the values accurately

Upvotes: 4

Views: 1534

Answers (1)

HimanshuR
HimanshuR

Reputation: 1428

1st Table Layout has height of match_parent and thus was not giving space to second table layout, so i changed it to wrap content

Upvotes: 1

Related Questions