簡體   English   中英

如何通過重用一個布局動態創建一個android表?

[英]How to create an android table dynamically by reusing one layout?

我正在制作一個應用程序來將小型數據庫的內容顯示為動態創建的表。 我使用 init() 函數來創建表 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);
    }
}

日志貓:

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.access$900(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.access$900(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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM