使用 TableLayout 进行动态解析 Json

Using TableLayout for dynamically parsing Json

我是新手,我正在创建一个应用程序,我在其中获得一个包含大量数据的 json 对象。我必须解析它并在 TableLayout 上显示值,但我想动态创建 TableRows。我还读到我们应该使用 GridView 来显示动态数据,但我认为网格视图用于图像,但我有 5 列和许多行(可能是 100 行)取决于数据。我没有找到任何解决方案。我找不到任何教程。

我的密码是

            <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 但不能动态创建。此应用仅支持大型平板电脑。

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