Problem mit dem Includieren eines Layouts während der Laufzeit

S

Son Goku

Ambitioniertes Mitglied
5
Hallo

Ich habe ein Layout was wie folgt aufgebaut ist:

(tabelle.xml)
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TableLayout
    android:id="@+id/tableLayout1"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">
    <TableRow
      android:paddingLeft="15px"
      android:id="@+id/tableRow4"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content">
      <TextView
        android:text="@string/textSpalte1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/textView1">
      </TextView>
      <TextView
        android:text="@string/textSpalte2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/textView2">
      </TextView>
    </TableRow>
    <include android:layout_height="wrap_content" layout="@layout/neue_zeile" android:id="@+id/include1" android:layout_width="wrap_content"></include>
  </TableLayout>
</LinearLayout>
Folgedes Layout wird includiert:

(neue_zeile.xml)
Code:
<?xml version="1.0" encoding="utf-8"?>
<TableRow
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/neueZeile1"
  style="@style/style_neue_zeile">
  <EditText
    android:id="@+id/editText1"
    style="@style/style_spalte1"
    android:inputType="number">
  </EditText>
  <EditText
    android:id="@+id/editText2"
    style="@style/style_spalte2">
  </EditText>
</TableRow>
Nun möchte ich zur Laufzeit neue Zeilen hinzufügen zu der einen bisher includierten.
Dazu habe ich folgenden Java-Code:

Code:
protected void addNeueZeile() {
  TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
  TableRow tr = (TableRow) findViewById(R.id.neueZeile1);
  table.addView(tr);
}
In der Zeile, wo das addView steht, bekomme ich aber immer die Meldung "Source not found".
Was mache ich falsch? Ist das überhaupt während der Laufzeit möglich?
Wie kann ich evtl. anders vor gehen?

Danke für eure Hilfe.

Viele Grüße
Son Goku
 
Halllo Son Goku,

mit "findViewById(int)" kannst du nur View finden, die bereits in deinem Content geladen sind.

Wenn du neue Views aus anderen xml-Dateien laden adden willst, brauchst du den LayoutInflater.

regards
 
  • Danke
Reaktionen: Son Goku
Danke!
Hat funktioniert!
 

Ähnliche Themen

M
Antworten
3
Aufrufe
161
moin
M
S
Antworten
4
Aufrufe
994
Sempervivum
S
W
  • WuDiDong
Antworten
3
Aufrufe
767
jogimuc
J
Zurück
Oben Unten