2013-09-24 3 views
4

xml로 디자인 된 헤더 행이있는 테이블이 있습니다. 이제 테이블에 데이터를 추가하고 싶습니다. (json 배열에서 가져옵니다.) 머리글 행에 영향을주지 않고 세로로 데이터 행을 스크롤해야하므로 머리글 행의 테이블 바로 아래에 다른 tablelayout을 만들었습니다. 그런 다음 각 새 행의 각 필드에 대해 Java 파일의 레이아웃 매개 변수를 설정 한 다음 두 번째 테이블 레이아웃에 행을 추가합니다. 그러나 로그에 데이터가 인쇄되는 동안 행이 보이지 않습니다. 실수가 어디인지 알 수 없습니다.테이블 레이아웃의 동적으로 추가 된 행이 표시되지 않습니다.

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> 

자바 코드 :

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

invalidate() 또는 postInvalid를 시도 했습니까? 후에 먹었습니까? – goodm

+0

아니요, 나는이 함수들 중 하나를 시도하지 않습니다. – HimanshuR

+0

모든 것이 requestLayout()을 사용하고 invalidate() 후에 올바른 것이 무엇인지 기억하지 못합니다. – goodm

답변

1

1 테이블 레이아웃 match_parent의 높이를 가지고 있으며, 따라서 두 번째 테이블 공간을 포기하지 않을했다 레이아웃, 그래서 내용을 줄 바꿈했습니다.

관련 문제