android - Using TableLayout for dynamically parsing Json -


i newbie , creating application getting json object lot of data. have parse , show values on tablelayout want create tablerows dynamically. have read should use gridview displaying dynamic data thnk grid view images have 5 cols , many rows (may 100s) depends on data. didn't find solution. not find tutorial.

my code is

            <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:background="#e57373"             tools:context="com.example.myapp.afterlogin"             android:screenorientation="userlandscape">              <linearlayout                 android:layout_margintop="100dp"                  android:layout_width="250dp"                  android:layout_height="match_parent"                  android:orientation="vertical"                 android:id="@+id/linearlayout"                 android:background="#9e9e9e">                   </linearlayout>                  <tablelayout             android:layout_width="match_parent"             android:layout_height="match_parent"             android:layout_aligntop="@+id/linearlayout"             android:layout_toendof="@+id/linearlayout"                 android:stretchcolumns="*">                <tablerow             android:layout_height="40dp"             android:layout_width="match_parent"             >              <textview             android:layout_width="match_parent"             android:layout_height="25dp"             android:textsize="18sp"             android:textcolor="@android:color/black"             android:text="@string/tasinsp"             android:id="@+id/textview3"             android:gravity="center"             android:layout_span="5"             android:background="#9e9e9e"/>             </tablerow>               <tablerow             android:layout_height="40dp"             android:layout_width="match_parent"             >              <textview              android:layout_width="wrap_content"             android:layout_height="42dp"             android:textsize="18sp"             android:textcolor="@android:color/black"             android:text="@string/sno"             android:id="@+id/textview4"             android:gravity="center"             android:background="#00e5ff"               />             <textview              android:layout_width="wrap_content"             android:layout_height="42dp"             android:textsize="18sp"             android:textcolor="@android:color/black"             android:text="@string/reno"             android:id="@+id/textview5"             android:gravity="center"             android:background="#cddc39"             />             <textview              android:layout_width="wrap_content"             android:layout_height="42dp"             android:textsize="18sp"             android:textcolor="@android:color/black"             android:text="@string/impoame"             android:id="@+id/textview6"             android:gravity="center"             android:background="#00e5ff"                 />             <textview              android:layout_width="wrap_content"             android:layout_height="42dp"             android:textsize="18sp"             android:textcolor="@android:color/black"             android:text="@string/custno"             android:id="@+id/textview7"             android:gravity="center"             android:background="#cddc39"                 />             <textview              android:layout_width="wrap_content"             android:layout_height="42dp"             android:textsize="18sp"             android:textcolor="@android:color/black"             android:text="@string/custdate"             android:id="@+id/textview8"             android:gravity="center"             android:background="#00e5ff"             />             </tablerow>             </tablelayout>               </relativelayout> 

i know how create tablelayout in xml cannot dynamically. app supports large tablets.

define table layout in activity_main.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" >      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:gravity="center_horizontal"         android:orientation="horizontal"         android:padding="5dp" >          <edittext             android:id="@+id/rowno_id"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:hint="rowno"             android:inputtype="number" />          <edittext             android:id="@+id/colno_id"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:hint="colno"             android:inputtype="number" />          <button             android:id="@+id/build_btn_id"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="build" />     </linearlayout>      <tablelayout         android:id="@+id/tablelayout1"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:padding="10dp"         android:shrinkcolumns="*"         android:stretchcolumns="*" >     </tablelayout>  </linearlayout> 

activity

public class mainactivity extends activity {   tablelayout table_layout;  edittext rowno_et, colno_et;  button build_btn;   @override  protected void oncreate(bundle savedinstancestate) {   super.oncreate(savedinstancestate);   setcontentview(r.layout.activity_main);    rowno_et = (edittext) findviewbyid(r.id.rowno_id);   colno_et = (edittext) findviewbyid(r.id.colno_id);   build_btn = (button) findviewbyid(r.id.build_btn_id);   table_layout = (tablelayout) findviewbyid(r.id.tablelayout1);    build_btn.setonclicklistener(new onclicklistener() {     @override    public void onclick(view v) {     // todo auto-generated method stub      string rowstring = rowno_et.gettext().tostring();     string colstring = colno_et.gettext().tostring();      if (!rowstring.equals("") && !colstring.equals("")) {      int rows = integer.parseint(rowstring);      int cols = integer.parseint(colstring);      table_layout.removeallviews();      createtable(rows, cols);     }      else {      toast.maketext(mainactivity.this,        "please enter row , col numbers",        toast.length_short).show();     }     }   });   }   private void createtable(int rows, int cols) {    // outer loop   (int = 1; <= rows; i++) {     tablerow row = new tablerow(this);    row.setlayoutparams(new layoutparams(layoutparams.match_parent,      layoutparams.wrap_content));     // inner loop    (int j = 1; j <= cols; j++) {      textview tv = new textview(this);     tv.setlayoutparams(new layoutparams(layoutparams.wrap_content,       layoutparams.wrap_content));     tv.setbackgroundresource(r.drawable.cell_shape);     tv.setpadding(5, 5, 5, 5);     tv.settext("r " + + ", c" + j);      row.addview(tv);     }     table_layout.addview(row);    }  }  }  

courtesy: http://www.tutorialsbuzz.com/2014/02/android-building-tablelayout-at-runtime.html


Comments