0

大家好,我想创建一个简单的应用程序,用户可以在其中输入要在列表视图中显示的记录数。然后我向数据库查询该行数并将其显示在表格布局中,如图所示。

在此处输入图像描述

并将动态行添加到表格布局中

   package com.heavy.test;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class TestHeavyDBActivity extends Activity {

    TableLayout t1;
    Button btnSubmit;
    EditText edtRecords;

    ArrayList<Record> recordsList = new ArrayList<Record>();
    ArrayAdapter<String> adapter; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



        t1 = (TableLayout) findViewById(R.id.tableLayout);
        edtRecords = (EditText) findViewById(R.id.editText);
        btnSubmit = (Button) findViewById(R.id.btnRecords);

        btnSubmit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                System.out.println(new Date().getTime());
                int recordsNum = Integer.parseInt(edtRecords.getText().toString().trim());

                SQLiteDatabase db;
                ConferenceDBHelper helper;

                helper = new ConferenceDBHelper(TestHeavyDBActivity.this);
                db = helper.getWritableDatabase();

                Cursor cursor = db.rawQuery("select VP_BRAND_DESC, VP_BOOK, VP_DATE, VP_SIZE_DESC from SOL_SALES  LIMIT "+recordsNum, null);
                while(cursor.moveToNext()){

                    Record record = new Record();
                    record.setBrand(cursor.getString(0));
                    record.setBookName(cursor.getString(1));
                    record.setDate(cursor.getString(2));
                    record.setSize(cursor.getString(3));

                    recordsList.add(record);

                }

                helper.close();
                db.close();

                TableRow tr = new TableRow(TestHeavyDBActivity.this);
                LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                lp.setMargins(5, 5 , 5, 5);
                tr.setLayoutParams(lp);

                Iterator<Record> it = recordsList.iterator();
                while (it.hasNext()) {

                    Record r = it.next();

                    TextView tv1 = new TextView(TestHeavyDBActivity.this);
                    tv1.setLayoutParams(lp);
                    tv1.setText("OMG");

                    TextView tv2 = new TextView(TestHeavyDBActivity.this);
                    tv2.setLayoutParams(lp);
                    tv2.setText("It");

                    TextView tv3 = new TextView(TestHeavyDBActivity.this);
                    tv3.setLayoutParams(lp);
                    tv3.setText("WORKED!!!");

                    TextView tv4 = new TextView(TestHeavyDBActivity.this);
                    tv4.setLayoutParams(lp);
                    tv4.setText("Tes!!!");

                    tr.addView(tv1);
                    tr.addView(tv2);
                    tr.addView(tv3);
                    tr.addView(tv4);

                    t1.addView(tr, new TableLayout.LayoutParams(
                            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                }
                System.out.println(new Date().getTime());

            }
        });







    }
}

编译后我得到了以下堆栈跟踪

    06-14 10:12:36.948: E/AndroidRuntime(203): Uncaught handler: thread main exiting due to uncaught exception
06-14 10:12:36.978: E/AndroidRuntime(203): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1861)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.view.ViewGroup.addView(ViewGroup.java:1756)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.widget.TableLayout.addView(TableLayout.java:418)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.view.ViewGroup.addView(ViewGroup.java:1736)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.widget.TableLayout.addView(TableLayout.java:409)
06-14 10:12:36.978: E/AndroidRuntime(203):  at com.heavy.test.TestHeavyDBActivity$1.onClick(TestHeavyDBActivity.java:103)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.view.View.performClick(View.java:2364)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.view.View.onTouchEvent(View.java:4179)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.widget.TextView.onTouchEvent(TextView.java:6540)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.view.View.dispatchTouchEvent(View.java:3709)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-14 10:12:36.978: E/AndroidRuntime(203):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-14 10:12:36.978: E/AndroidRuntime(203):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
06-14 10:12:36.978: E/AndroidRuntime(203):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.os.Looper.loop(Looper.java:123)
06-14 10:12:36.978: E/AndroidRuntime(203):  at android.app.ActivityThread.main(ActivityThread.java:4363)
06-14 10:12:36.978: E/AndroidRuntime(203):  at java.lang.reflect.Method.invokeNative(Native Method)
06-14 10:12:36.978: E/AndroidRuntime(203):  at java.lang.reflect.Method.invoke(Method.java:521)
06-14 10:12:36.978: E/AndroidRuntime(203):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-14 10:12:36.978: E/AndroidRuntime(203):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-14 10:12:36.978: E/AndroidRuntime(203):  at dalvik.system.NativeStart.main(Native Method)

所以请告诉我如何将动态行设置为表格视图。提前致谢

4

3 回答 3

2

问题是因为您只创建TableRow一次(在while循环外)并多次添加它。

while解决方案 - 在循环内移动对象创建:

Iterator<Record> it = recordsList.iterator();
while (it.hasNext()) {

    TableRow tr = new TableRow(TestHeavyDBActivity.this);
    LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    lp.setMargins(5, 5 , 5, 5);
    tr.setLayoutParams(lp);

    //populate the TableRow
    // ...

    t1.addView(tr, new TableLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));


}
于 2012-06-14T05:16:14.897 回答
1

使用TableLayout.LayoutParamsinsted LayoutParams_

LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
于 2012-06-14T05:05:03.157 回答
1

在我的代码中看到的 linearContainer 是 Table。

linearContainer = new TableLayout(getApplicationContext());
TableLayout.LayoutParams tab_lp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
linearContainer.setLayoutParams(tab_lp);

所以设置表参数后。

    for (int j = 0; j < productList.length; j++)
    {
        createLayout2(productList[j], beerBrand);   

    }

然后创建方法

public void createLayout2(String product, final String beerBrand) { 

    TableRow row = new TableRow(this);

    final TextView tvProduct = new TextView(getApplicationContext());

    LinearLayout.LayoutParams lp_l3 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, (LayoutParams.WRAP_CONTENT));
    tvProduct.setLayoutParams(lp_l3);
    tvProduct.setText(product);

    row.addView(tvProduct,  new TableRow.LayoutParams(1));
    linearContainer.addView(row,new TableLayout.LayoutParams());        


}

所以我认为你必须添加如下行

 row.addView(tvProduct,  new TableRow.LayoutParams(1));
 linearContainer.addView(row,new TableLayout.LayoutParams());
于 2012-06-14T05:10:57.660 回答