Я пытаюсь добавить строки динамически с помощью for для l oop, выполняю поиск, но пока безуспешно, получаю следующее сообщение об ошибке: java .lang.RuntimeException: Невозможно запустить действие ComponentInfo {com .sanogh.mybilling / com.sanogh.mybilling.FakturaActivity}: java .lang.IllegalStateException: указанный дочерний элемент уже имеет родителя. Сначала вы должны вызвать removeView () для родительского объекта ребенка.
Код класса
@SuppressLint("ResourceType")
private void loadArtikelTable(){
tableLayout = (TableLayout) findViewById(R.id.aritkelTableLayout);
TextView textView = new TextView(this);
artikelTV = (TextView) new TextView(this);
dbHelper = new ArtiklarDatabaseHelper(this);
arrayList = dbHelper.getAllArtiklar(ArtiklarConstants.C_ARTIKEL);
for (int i = 1; i < 3 ; i++) {
tr = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
artikelTV.setText("test");
artikelTV.setPadding(20,0,20,0);
textView.setText("test23");
tr.addView(artikelTV);
tr.addView(textView);
tableLayout.addView(tr);
}
}
Код размещения должен быть добавлен ниже следующей строки
<TableLayout
android:id="@+id/aritkelTableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_weight="2"
android:text="Artikel"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:text="Moms"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_weight="1"
android:text="á pris"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="4"
android:layout_weight="1"
android:text="Antal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="5"
android:layout_weight="1"
android:text="Pris"/>
</TableRow>
</TableLayout>