-1

I'm trying to add dynamically row in TableLayout.. but i don't know where i am getting wrong..Please help.. Thanks

JSONObject res = new JSONObject(response);
                            JSONArray thread = res.getJSONArray("physician_info");
                            for (int i = 0; i < thread.length(); i++) {
                                JSONObject obj = thread.getJSONObject(i);
                                TableRow row= new TableRow(getActivity());
                                row.setLayoutParams(new TableLayout.LayoutParams(
                                        TableLayout.LayoutParams.MATCH_PARENT,
                                        TableLayout.LayoutParams.WRAP_CONTENT));
                              //  row.setLayoutParams(lp);

                                physician_name = new TextView(getActivity());
                                number = new TextView(getActivity());
                                type =new TextView(getActivity());
                                physician_name.setText(obj.getString("name"));
                                number.setText(obj.getString("number"));
                                type.setText(obj.getString("type"));

                                row.addView(physician_name);
                                row.addView(number);
                                row.addView(type);

                                tbl.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));

                            }

i have Tablelayout like

<TableLayout android:layout_width="match_parent" android:id="@+id/physicians" android:layout_height="150dp" android:shrinkColumns="*" android:stretchColumns="*" android:orientation="vertical">
 <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal">
                <TextView android:layout_width="match_parent" android:textStyle="" android:layout_height="wrap_content" android:text="Phyisician" android:textColor="#000"  android:layout_margin="1dp" android:gravity="center" />
                <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Number" android:textColor="#000"  android:gravity="center" android:layout_margin="1dp"/>
                <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Type" android:textColor="#000"  android:gravity="center" android:layout_margin="1dp" /> </TableRow></TableLayout>

'

i want to add rows in bottom of this above row dynamically.

Vishal Thakkar
  • 652
  • 4
  • 18

1 Answers1

0

Use TableRow.LayoutParams instead of TableLayout.LayoutParams

        TableRow row= new TableRow(getActivity());
        TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
        row.setLayoutParams(lp);

Update better way of handling.

void OnSomeCLickOrResponse() {

        JSONObject res = new JSONObject(response);
        JSONArray thread = res.getJSONArray("physician_info");
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        for (int i = 0; i < thread.length(); i++) {

            JSONObject obj = thread.getJSONObject(i);
            View view = inflater.inflate(R.layout.row_layout, null);
            TableRow row = (TableRow) view.findViewById(R.id.myrow);

            TextView physician_name = (TextView) view.findViewById(R.id.physician_name);
            TextView number = (TextView) view.findViewById(R.id.number);
            TextView type = (TextView) view.findViewById(R.id.type);

            physician_name.setText(obj.getString("name"));
            number.setText(obj.getString("number"));
            type.setText(obj.getString("type"));

            tbl.addView(row);

        }
    }

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myrow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <TextView
        android:id="@+id/physician_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="New Text" />

    <TextView
        android:id="@+id/number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="New Text" />

    <TextView
        android:id="@+id/type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="New Text" />


</TableRow>
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41