2016-08-05 24 views
-1

我試圖動態地添加在TableLayout行..但我不知道在哪裏我得到wrong..Please幫助..謝謝無法動態地添加在TableLayout排的Android

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

          } 

我有Tablelayout像

<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>

'

我想動態地在上面這行的底部添加行。

+0

請參閱本http://stackoverflow.com/questions/18207470/adding-table-rows-dynamically-in-android –

+0

的應用程序崩潰?如果是,那麼添加logcat如果沒有,那麼你面臨什麼問題? –

+0

@SohailZahid沒有應用程序不崩潰..我從json數組中獲得完整的數據,但沒有顯示在表格行中。 –

回答

0

使用TableRow.LayoutParams而不是TableLayout.LayoutParams

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

更新更好的處理辦法。

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> 
+0

如果此處仍有問題評論。 –

+0

仍然沒有顯示.. –

+0

@VishalThakkar你在片段中初始化你的tableLayout佈局編程。 –

相關問題