2013-09-24 67 views
4

我有一個設計爲xml的標題行表。現在我想在表中添加數據(我從json數組中獲取數據)。 我需要垂直滾動數據行而不影響標題行,所以我在標題行表格下方創建了另一個表格佈局。然後,對於每個新行的每個字段,我在java文件中設置佈局參數,然後將該行添加到第二個表格佈局。 但是,當數據被打印到日誌中時,沒有行可見。我無法弄清楚我的錯誤在哪裏。在tablelayout中動態添加的行是不可見的

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代碼:

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

登錄正在打印的所有值準確

+0

你嘗試無效()或postInvalid吃完()後? – goodm

+0

不,我ddnot嘗試任何這些功能 – HimanshuR

+0

一切嘗試使用requestLayout()和invalidate()後,我不記得哪一個是正確的。 – goodm

回答

1

第一表佈局具有match_parent的高度,因此沒有給空間第二表佈局,所以我改變它來包裝內容

相關問題