如何通过重用一个布局动态创建 android table?
How to create an android table dynamically by reusing one layout?
我正在制作一个应用程序来将小型数据库的内容显示为动态创建的 table。我使用 init() 函数创建 table onCreate 并希望它随后填充数据。由于第一行或标题行是静态的,我决定对该行进行硬编码,然后通过数据库循环创建其余部分。问题是当我尝试 运行 时它确实会抛出 IndexOutOfBoundsException。我假设它与我用来回收我的布局的 removeView() 调用有关,但真的不确定。任何人都可以向我解释我做错了什么吗?
初始化函数:
//Initiates the table
public void init(){
//This part defines the layout to be used for creating new rows
TableLayout ll = (TableLayout) findViewById(R.id.tableLayout);
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
TextView tdate = new TextView(this);
TextView tweight = new TextView(this);
//This generates the caption row
tdate.setText("Weight");
tdate.setPadding(3, 3, 3, 3);
tweight.setText("Date");
tweight.setPadding(3, 3, 3, 3);
row.addView(tdate);
row.addView(tweight);
ll.addView(row,0);
//This loop adds the database content to the table (using hardcoded values here for demonstration)
for (int i = 1; i < 3; i++) {
((ViewGroup)tdate.getParent()).removeView(tdate);
((ViewGroup)tweight.getParent()).removeView(tweight);
((ViewGroup)row.getParent()).removeView(row);
tdate.setText("20.20.2020");
tdate.setPadding(3, 3, 3, 3);
tweight.setText("89");
tweight.setPadding(3, 3, 3, 3);
row.addView(tdate);
row.addView(tweight);
ll.addView(row,i);
}
}
Logcat:
07-03 11:00:34.903 30276-30276/net.sanctum.adhominem.sanctum E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: net.sanctum.adhominem.sanctum, PID: 30276
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.sanctum.adhominem.sanctum/net.sanctum.adhominem.sanctum.Log}: java.lang.IndexOutOfBoundsException: index=1 count=0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access0(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: java.lang.IndexOutOfBoundsException: index=1 count=0
at android.view.ViewGroup.addInArray(ViewGroup.java:3971)
at android.view.ViewGroup.addViewInner(ViewGroup.java:3902)
at android.view.ViewGroup.addView(ViewGroup.java:3733)
at android.widget.TableLayout.addView(TableLayout.java:429)
at android.view.ViewGroup.addView(ViewGroup.java:3678)
at android.widget.TableLayout.addView(TableLayout.java:411)
at net.sanctum.adhominem.sanctum.Log.init(Log.java:77)
at net.sanctum.adhominem.sanctum.Log.onCreate(Log.java:20)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access0(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
使用这个,
//Initiates the table
public void init(){
//This part defines the layout to be used for creating new rows
TableLayout ll = (TableLayout) findViewById(R.id.table_layout);
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
TextView tdate = new TextView(this);
TextView tweight = new TextView(this);
//This generates the caption row
tdate.setText("Weight");
tdate.setPadding(3, 3, 3, 3);
tweight.setText("Date");
tweight.setPadding(3, 3, 3, 3);
row.addView(tdate);
row.addView(tweight);
ll.addView(row,0);
//This loop adds the database content to the table (using hardcoded values here for demonstration)
for (int i = 1; i < 3; i++) {
row= new TableRow(this);
lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
tdate = new TextView(this);
tweight = new TextView(this);
tdate.setText("20.20.2020");
tdate.setPadding(3, 3, 3, 3);
tweight.setText("89");
tweight.setPadding(3, 3, 3, 3);
row.addView(tdate);
row.addView(tweight);
ll.addView(row,i);
}
}
我正在制作一个应用程序来将小型数据库的内容显示为动态创建的 table。我使用 init() 函数创建 table onCreate 并希望它随后填充数据。由于第一行或标题行是静态的,我决定对该行进行硬编码,然后通过数据库循环创建其余部分。问题是当我尝试 运行 时它确实会抛出 IndexOutOfBoundsException。我假设它与我用来回收我的布局的 removeView() 调用有关,但真的不确定。任何人都可以向我解释我做错了什么吗?
初始化函数:
//Initiates the table
public void init(){
//This part defines the layout to be used for creating new rows
TableLayout ll = (TableLayout) findViewById(R.id.tableLayout);
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
TextView tdate = new TextView(this);
TextView tweight = new TextView(this);
//This generates the caption row
tdate.setText("Weight");
tdate.setPadding(3, 3, 3, 3);
tweight.setText("Date");
tweight.setPadding(3, 3, 3, 3);
row.addView(tdate);
row.addView(tweight);
ll.addView(row,0);
//This loop adds the database content to the table (using hardcoded values here for demonstration)
for (int i = 1; i < 3; i++) {
((ViewGroup)tdate.getParent()).removeView(tdate);
((ViewGroup)tweight.getParent()).removeView(tweight);
((ViewGroup)row.getParent()).removeView(row);
tdate.setText("20.20.2020");
tdate.setPadding(3, 3, 3, 3);
tweight.setText("89");
tweight.setPadding(3, 3, 3, 3);
row.addView(tdate);
row.addView(tweight);
ll.addView(row,i);
}
}
Logcat:
07-03 11:00:34.903 30276-30276/net.sanctum.adhominem.sanctum E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: net.sanctum.adhominem.sanctum, PID: 30276
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.sanctum.adhominem.sanctum/net.sanctum.adhominem.sanctum.Log}: java.lang.IndexOutOfBoundsException: index=1 count=0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access0(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: java.lang.IndexOutOfBoundsException: index=1 count=0
at android.view.ViewGroup.addInArray(ViewGroup.java:3971)
at android.view.ViewGroup.addViewInner(ViewGroup.java:3902)
at android.view.ViewGroup.addView(ViewGroup.java:3733)
at android.widget.TableLayout.addView(TableLayout.java:429)
at android.view.ViewGroup.addView(ViewGroup.java:3678)
at android.widget.TableLayout.addView(TableLayout.java:411)
at net.sanctum.adhominem.sanctum.Log.init(Log.java:77)
at net.sanctum.adhominem.sanctum.Log.onCreate(Log.java:20)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access0(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
使用这个,
//Initiates the table
public void init(){
//This part defines the layout to be used for creating new rows
TableLayout ll = (TableLayout) findViewById(R.id.table_layout);
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
TextView tdate = new TextView(this);
TextView tweight = new TextView(this);
//This generates the caption row
tdate.setText("Weight");
tdate.setPadding(3, 3, 3, 3);
tweight.setText("Date");
tweight.setPadding(3, 3, 3, 3);
row.addView(tdate);
row.addView(tweight);
ll.addView(row,0);
//This loop adds the database content to the table (using hardcoded values here for demonstration)
for (int i = 1; i < 3; i++) {
row= new TableRow(this);
lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
tdate = new TextView(this);
tweight = new TextView(this);
tdate.setText("20.20.2020");
tdate.setPadding(3, 3, 3, 3);
tweight.setText("89");
tweight.setPadding(3, 3, 3, 3);
row.addView(tdate);
row.addView(tweight);
ll.addView(row,i);
}
}