创建这篇文章,因为我是新手,我需要一些帮助。我正在对一个应用程序进行一些练习,你可以通过一些值从类中创建一个tablelayout。但是在我按下按钮后我得到错误“指定的孩子已经有了父母。你必须首先在孩子的父母上调用removeView()”
public class TabelFlat extends Activity{
//deklarasi variabel
String jumlah,jasa,jangkawaktu;
int jum = 0, jsa = 0, jkwaktu = 0;
int a = 0, b = 0, c = 0, d = 0, i = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
//mengambil value yg diimport dari class lain
if (extras != null) {
jumlah = extras.getString("jumlah");
jasa = extras.getString("jasa");
jangkawaktu = extras.getString("jangkawaktu");
}
//konversi string menjadi int
jum = Integer.parseInt(jumlah);
jsa = Integer.parseInt(jasa);
jkwaktu = Integer.parseInt(jangkawaktu);
//membuat tabel
TableLayout MainLayout = new TableLayout(this);
MainLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
MainLayout.setStretchAllColumns(true);
//Baris Judul
TableRow row1 = new TableRow(this);
TextView text1 = new TextView(this);
text1.setText("Bulan");
TextView text2 = new TextView(this);
text2.setText("Total Angsuran");
TextView text3 = new TextView(this);
text2.setText("Angsuran");
TextView text4 = new TextView(this);
text2.setText("Bunga");
TextView text5 = new TextView(this);
text2.setText("Sisa Saldo");
text2.setGravity(android.view.Gravity.RIGHT);
//memasukkan nilai text ke row
row1.addView(text1);
row1.addView(text2);
row1.addView(text3);
row1.addView(text4);
row1.addView(text5);
//menambah row ke tabel
MainLayout.addView(row1);
//kalkulasi
TextView tangsuran = new TextView(this);
a = (jum * 120/100);
tangsuran.setText(""+String.valueOf(a));
TextView angsuran = new TextView(this);
b = jum / jkwaktu;
angsuran.setText(""+String.valueOf(b));
TextView bunga = new TextView(this);
c = jum * 20 / 100 / jkwaktu;
bunga.setText(""+String.valueOf(c));
//loop
d = jum;
for (i = 1;i<=jkwaktu;i++){
TableRow barisloop = new TableRow(this);
//membuat string kosong untuk setiap kolom
TextView bulan = new TextView(this);
bulan.setText(""+i);
TextView sisa = new TextView(this);
d = d - b;
sisa.setText(""+String.valueOf(d));
barisloop.addView(bulan);
barisloop.addView(tangsuran);
barisloop.addView(angsuran);
barisloop.addView(bunga);
barisloop.addView(sisa);
MainLayout.addView(barisloop);
}
setContentView(MainLayout);
}
}
答案 0 :(得分:0)
在这里,您将tangsuran
,angsuran
和bunga
文本视图添加到新行,但它们已添加到另一个。您必须为循环中的行创建新对象
for (i = 1;i<=jkwaktu;i++){
TableRow barisloop = new TableRow(this);
//membuat string kosong untuk setiap kolom
TextView bulan = new TextView(this);
bulan.setText(""+i);
TextView sisa = new TextView(this);
d = d - b;
sisa.setText(""+String.valueOf(d));
barisloop.addView(bulan);
//barisloop.addView(tangsuran);
//barisloop.addView(angsuran); CREATE NEW TEXTVIEWS
//barisloop.addView(bunga);
barisloop.addView(sisa);
MainLayout.addView(barisloop);
}