首页 > 解决方案 > 在 Android 中创建表格视图的问题

问题描述

我正在尝试编写一个变量表来显示来自 Android 数据库的数据。到目前为止,我可以显示 2 条不同的行,但程序不再创建任何行,我根本找不到错误。

主要活动:

package com.example.tabellentest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    String[] textArray ={" Null "," Eins ", " Zwei ", " Drei ", " Vier ", " Fünf "," SechsHundertZweiUndDreizig ", " Sechs "};
    String[] textArray1 ={" 12Null "," 12Eins ", " 12Zwei ", " 12Drei ", " 12Vier ", " 12Fünf "," 12SechsHundertZweiUndDreizig ", " Sechs "};

    private TableLayout firstTable;
    private TableLayout secondTable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ExpandTextView();
        this.firstTable = (TableLayout) findViewById(R.id.tableLayout1);
        this.secondTable = (TableLayout) findViewById(R.id.tableLayout2);

        ExpandTableRow();
    }
    public TextView CreateTextView(String text, int index){
        TextView textView1 = new TextView(getApplicationContext());
        textView1.setText(text);
        return textView1;
    }
    public void ExpandTextView (){
        LinearLayout linearLayout = findViewById(R.id.linearLayout1);
        for( int i = 0; i < textArray.length; i++ )
        {
            linearLayout.addView(CreateTextView(textArray[i], i));
            TextView textView = new TextView(this);
            //textView.setText(textArray[i]);
            //textView.setId(i);
            linearLayout.addView(textView);
        }
    }
    public void ExpandTableRow(){
        for (int ab = 2; ab < 8; ab++){
            LinearLayout linearLayout2 = findViewById(R.id.linearLayout2);
            for( int i = 0; i < textArray1.length; i++ ) {
                linearLayout2.addView(CreateTextView(textArray1[i], i));
            }
            Context context = getApplicationContext();
            TableRow row = new TableRow(context);
            secondTable.addView(row);
            for( int i = 0; i < textArray1.length; i++ ) {
            linearLayout2.addView(CreateTextView(textArray1[i], i));
            }
        }
  }
}

活动主.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="64dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <TableLayout
                android:id="@+id/tableLayout1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <LinearLayout
                        android:id="@+id/linearLayout1"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:orientation="horizontal"/>
                </TableRow>
            </TableLayout>
        </ScrollView>
    </HorizontalScrollView>

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TableLayout
            android:id="@+id/tableLayout2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:id="@+id/linearLayout2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal" />
            </TableRow>

        </TableLayout>
    </HorizontalScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

我希望任何人都可以给我一个提示以找到问题。

标签: javaandroid

解决方案


推荐阅读