Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8071479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:42:10+00:00 2026-06-05T13:42:10+00:00

Hi all I want to crate the simple app in which user inputs the

  • 0

Hi all I want to crate the simple app in which user inputs the number of records to display in the listview. Then I make a query to the database for that number of rows and show it on to the table layout as shown in fig..

enter image description here

and that dynamic rows are added to table layout as

   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());

            }
        });







    }
}

and after compiling i got following stack trace

    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)

So please tell me how to set the dynamic rows to the table view.Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:42:13+00:00Added an answer on June 5, 2026 at 1:42 pm

    The problem is because you are creating the TableRow only once (outside the while loop) and adding it multiple times.

    Solution – move the object creation inside the while loop:

    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));
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to create a simple state machine using C but all the events
I learning Perl and I want to create a simple application that gets all
All, I want to crate an rich looking, custom progress bar for blackberry touch
I want to create a logger which logs all my messages sent to System.out
I want to create JSF table which has select all button and check box
I want to create an Android app that can be used on 2.3.3 all
I want to create and then destroy buttons, all with code, but can't find
I have a simple iPhone app that Im learning and I want to have
I have a loading screen which closes after 5 secounds and then the app
I want to create the dictionary of all the ViewModels. public static Dictionary<string, WeakReference>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.