2015-07-21 87 views
1

我是一個新手,我創建了一個應用程序,我得到一個Json對象與大量的數據。我必須解析它並顯示TableLayout上的值,但我想動態創建TableRows。我也讀過,我們應該使用GridView來顯示動態數據,但我認爲網格視圖是用於圖像的,但是我有5列和許多行(可能是100s)取決於數據。我沒有找到任何解決方案。我找不到任何教程。使用TableLayout動態分析Json

我的代碼是

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

我知道如何創建XML TableLayout但不能動態地進行。這款應用只支持大型平板電腦。

回答

0

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> 

活動

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 the row and col Numbers", 
     Toast.LENGTH_SHORT).show(); 
    } 

    } 
    }); 

} 

private void createTable(int rows, int cols) { 

    // outer for loop 
    for (int i = 1; i <= rows; i++) { 

    TableRow row = new TableRow(this); 
    row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
    LayoutParams.WRAP_CONTENT)); 

    // inner for loop 
    for (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 " + i + ", C" + j); 

    row.addView(tv); 

    } 

    table_layout.addView(row); 

    } 
} 

} 

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

+0

我一直在尋找這個。謝謝 – devloperuser