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) 1 回だけ作成し、複数回追加しているためです。

解決策 - オブジェクトの作成を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 に答える