Android Wie erstelle ich eine dynamische Tabelle (TableLayout)?

Ljonja2107

Mitglied
Moin, ich bastle grade an einer Art Visitorlog, und dafür brauche ich eine Tabelle, welche bei Knopfdruck sich dynamisch um eine Zeile erweitert.
Dies klappt auch zu einem bestimmten Punkt aber irgendwie bekomme ichs nicht hin, dass die eingegeben Daten unter den Spaltenüberschriften ist. Also Aligned. Könnte mir da jemand helfen? Danke bereits im Vorraus.
Java:
 public void addTableRow() {
        try {
            String name = inputName.getText().toString();
            String firma = inputFirma.getText().toString();
            String ausweis = inputAusweis.getText().toString();
            String besuchter = inputBesuchter.getText().toString();
            String zielOrt = inputOrt.getText().toString();
            String kommen = inputKommen.getText().toString();

            if (inputName.getText().toString().trim().equals("") ||
                    inputKommen.getText().toString().trim().equals("") ||
                    inputAusweis.getText().toString().trim().equals("") ||
                    inputFirma.getText().toString().trim().equals("") ||
                    inputBesuchter.getText().toString().trim().equals("") ||
                    inputOrt.getText().toString().trim().equals("")) {
                Toast.makeText(this, "Bitte füllen Sie alle Felder aus.", Toast.LENGTH_SHORT).show();
                return;
            }

            TableLayout tableLayout = findViewById(R.id.table_layout);
            TableRow tableRow = new TableRow(this);

//            TableRow tableRow = findViewById(R.id.tableColorRow);

            counter++;
            bordCourtInt++;
            try {

                TextView counterView = new TextView(this);
                counterView.setText(String.valueOf(counter));

                TableRow.LayoutParams params1 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.2f);
                counterView.setLayoutParams(params1);
                tableRow.addView(counterView);

                TextView nameView = new TextView(this);
                nameView.setText(name);
                TableRow.LayoutParams params2 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, .2f);
                nameView.setLayoutParams(params2);
                tableRow.addView(nameView);

                TextView firmaView = new TextView(this);
                firmaView.setText(firma);
                TableRow.LayoutParams params3 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, .2f);
                firmaView.setLayoutParams(params3);
                tableRow.addView(firmaView);

                TextView ausweisView = new TextView(this);
                ausweisView.setText(ausweis);
                TableRow.LayoutParams params4 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, .2f);
                ausweisView.setLayoutParams(params4);
                tableRow.addView(ausweisView);

                TextView kommenView = new TextView(this);
                kommenView.setText(kommen);
                TableRow.LayoutParams params5 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, .2f);
                kommenView.setLayoutParams(params5);
                tableRow.addView(kommenView);

                TextView besuchterView = new TextView(this);
                besuchterView.setText(besuchter);
                TableRow.LayoutParams params6 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, .2f);
                besuchterView.setLayoutParams(params6);
                tableRow.addView(besuchterView);

                TextView zielOrtView = new TextView(this);
                zielOrtView.setText(zielOrt);
                TableRow.LayoutParams params7 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, .2f);
                zielOrtView.setLayoutParams(params7);
                tableRow.addView(zielOrtView);


                TextView gehenView = new TextView(this);
                gehenView.setText("n/a"); // "nicht vorhanden"
                TableRow.LayoutParams params8 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, .2f);
                gehenView.setLayoutParams(params8);
                tableRow.addView(gehenView);


                Button gehenBtn = new Button(this);
                gehenBtn.setText("Leave");
                TableRow.LayoutParams params9 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, .2f);
                clearForm();
Clearform ist einfach eine Methode, welche die Inhalte aus den Editfields wieder leert, nichts besonderes.
Außerdem sind OnCklickListener verwendet fürs "adden" der Zeilen, hoffe das ist gut genug erklärt.

Der XML-Code sieht wie folgt aus (ist mein erstes mal XML also seid barmherzig:


XML:
    <TableRow
            android:id="@+id/headlines"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/black"
            android:padding="5dp"
            android:paddingStart="10dp">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:text="@string/nummer"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/nameHint"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/firmaHint"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/ausweisnrHint"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/kommenHint"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/besuchterHint"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/zielortHint"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/gehen"
                android:textColor="@color/white" />
                </TableRow>
 

Jw456

Top Contributor
Bei einem TabelLayout solltest du dir mal „stretchColumns“ ansehen.
Ich würde das nicht über das Gewicht lösen. „weight“

in etwa so
Java:
         <TableLayout
        android:id="@+id/....."
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stretchColumns="0,1,2,3,4,5,6,7" >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="3dp" >
          
          
            <TextView
                android:layout_width="0"
                android:layout_height="wrap_content"
                android:text="@string/nummer"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="0"
                android:layout_height="wrap_content"
                android:text="@string/nameHint"
                android:textColor="@color/white" />

              ....
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
W Zur Laufzeit erstelltes MenuItem an eine View binden Android & Cross-Platform Mobile Apps 1
J Android Zugriff auf eine Datei, diese von einer anderen App erstellt wurde? Android & Cross-Platform Mobile Apps 11
W Pixel Farbe in eine andere Farbe ändern im ImageView von ein Icon Android & Cross-Platform Mobile Apps 14
S Sinnvollste weg eine SQLite DB mit Android auslesen Android & Cross-Platform Mobile Apps 7
L Regelmäßig eine Methode im Hintergund ausführen Android & Cross-Platform Mobile Apps 1
J Android Wann ist eine App privat? Android & Cross-Platform Mobile Apps 10
M Android Schreiben von JsonDaten in eine SqliteDB Android & Cross-Platform Mobile Apps 7
M Suche eine Webseite mit dem aktuelle Datum in XML Android & Cross-Platform Mobile Apps 18
R Android Eine Website-frame in einer App Android & Cross-Platform Mobile Apps 4
Robat Android Wie mache ich eine App mit verschiedenen Leveln? Android & Cross-Platform Mobile Apps 8
M [Android] Wie erstellt man eine Verknüpfung eines Ordners? Android & Cross-Platform Mobile Apps 7
R [ANDROID] Dynamisch eine Liste erweitern Android & Cross-Platform Mobile Apps 3
K Verständisfrage bzgl. Buttons und ihnen eine Funktion geben. Android & Cross-Platform Mobile Apps 3
X Android Wo gibts eine SortableListView? Android & Cross-Platform Mobile Apps 6
G eine Methode einer anderen Activity aufrufen Android & Cross-Platform Mobile Apps 9
S Android In Service überprüfen ob eine Activity gebunden ist? Android & Cross-Platform Mobile Apps 6
B TableLayout verhält sich nicht wie eine Tabelle Android & Cross-Platform Mobile Apps 3
L Android Eine Kleinigkeit für Profis Android & Cross-Platform Mobile Apps 2
A Android Eine Datei in Android löschen, aber wie ? Android & Cross-Platform Mobile Apps 24
A Koordinaten eine Images feststellen Android & Cross-Platform Mobile Apps 9
U [Android] Eine Datei aus Jar-Archiv laden Android & Cross-Platform Mobile Apps 4
N Gibt es eine opensource Speech-to-Text engine? Android & Cross-Platform Mobile Apps 3
B Android Eine Warnung in der Console (Eclipse) auswerten Android & Cross-Platform Mobile Apps 3
T Android Ergebnis eines XML+XSLT "transform" in eine HTML - Datei schreiben (Android) Android & Cross-Platform Mobile Apps 2
M Eclipse: cdc.jar in eine Midlet Suite integrieren Android & Cross-Platform Mobile Apps 18
L SecurityException beim lesen/schreiben eine Datei Android & Cross-Platform Mobile Apps 7
H Gibt es eine Alternative zur Location API? Android & Cross-Platform Mobile Apps 4
F Wie komplex wäre eine Wecker-Applikation? Android & Cross-Platform Mobile Apps 5
M Android Dynamische SchriftGröße einer TextView Android & Cross-Platform Mobile Apps 3
? dynamische Inhalte Android & Cross-Platform Mobile Apps 3
D Android dynamische Buttons positionieren Android & Cross-Platform Mobile Apps 1
S Dynamische EditText View eingaben in Datenbank speichern Android & Cross-Platform Mobile Apps 0
K Dynamische (Image)Buttons, Größe bestimmen Android & Cross-Platform Mobile Apps 4
G Dynamische View Inhalt -> Lagesensor Android & Cross-Platform Mobile Apps 3

Ähnliche Java Themen

Neue Themen


Oben