Reputation: 4008
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..
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
Upvotes: 0
Views: 1505
Reputation: 13805
See in my code linearContainer is Table.
linearContainer = new TableLayout(getApplicationContext());
TableLayout.LayoutParams tab_lp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
linearContainer.setLayoutParams(tab_lp);
So after setting table parameters.
for (int j = 0; j < productList.length; j++)
{
createLayout2(productList[j], beerBrand);
}
And then create method
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());
}
So I think you have to add rows like below
row.addView(tvProduct, new TableRow.LayoutParams(1));
linearContainer.addView(row,new TableLayout.LayoutParams());
Upvotes: 1
Reputation: 15774
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));
}
Upvotes: 2
Reputation: 1544
Use TableLayout.LayoutParams
insted of LayoutParams
in
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
Upvotes: 1