The specified child already has a parent - TableLayout - android

I'm getting data from JSON to my object BalanceSheet and want to display the data in a Table layout but Im getting the following error,
The specified child already has a parent. You must call removeView() on the child's parent first.
// on this line
tableLayout.addView(data_row);
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/balance_table_SV"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.BalanceTable">
</ScrollView>
and Activity :
public class BalanceTable extends AppCompatActivity {
ScrollView balance_table_SV;
ArrayList<BalanceSheet> bsList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_balance_table);
balance_table_SV = findViewById(R.id.balance_table_SV);
bsList = (ArrayList<BalanceSheet>) getIntent().getSerializableExtra("list");
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams( new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tableLayout.setStretchAllColumns(true);
tableLayout.removeAllViews();
int rows = bsList.size();
TableRow headers_row = new TableRow(this);
TableRow data_row = new TableRow(this);
for(int i = -1; i < rows; i ++) {
if (i == -1) {
// need check
headers_row.setLayoutParams( new TableLayout.LayoutParams());
TextView headers = new TextView(this);
headers.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
headers.setGravity(Gravity.LEFT);
headers.setPadding(5, 15, 0, 15);
headers.setText("Inv.#");
headers.setBackgroundColor(Color.parseColor("#f0f0f0"));
headers_row.addView(headers);
tableLayout.addView(headers_row);
}
else {
data_row.setLayoutParams( new TableRow.LayoutParams());
TextView tv1 = new TextView(this);
tv1.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv1.setText(String.valueOf(bsList.get(i).getName()));
TextView tv2 = new TextView(this);
tv2.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv2.setText(String.valueOf(bsList.get(i).getCredit()));
TextView tv3 = new TextView(this);
tv3.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv3.setText(String.valueOf(bsList.get(i).getDebt()));
TextView tv4 = new TextView(this);
tv4.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv4.setText(String.valueOf(bsList.get(i).getCredit()));
data_row.addView(tv1);
data_row.addView(tv2);
data_row.addView(tv3);
data_row.addView(tv4);
}
tableLayout.addView(data_row);
}
balance_table_SV.addView(tableLayout);
}
}

This happen cause you are trying to add the same view multiple times in TableLayout. as you are looping through all of your element you have to recreate the data_row again and again for adding it into the table layout.
else{
TableRow data_row = new TableRow(this);
data_row.setLayoutParams( new TableRow.LayoutParams());
TextView tv1 = new TextView(this);
tv1.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv1.setText(String.valueOf(bsList.get(i).getName()));
TextView tv2 = new TextView(this);
tv2.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv2.setText(String.valueOf(bsList.get(i).getCredit()));
TextView tv3= new TextView(this);
tv3.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv3.setText(String.valueOf(bsList.get(i).getDebt()));
TextView tv4 = new TextView(this);
tv4.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv4.setText(String.valueOf(bsList.get(i).getCredit()));
data_row.addView(tv1);
data_row.addView(tv2);
data_row.addView(tv3);
data_row.addView(tv4);
tableLayout.addView(data_row);
}
the above block will solve the problem as it will create the new object of TableRow every time and add it to TableLayout Object
Explanation:
while adding the table row second time it will generate the error cause we are trying to add the same table row which being attached to a parent in this case we can consider it as table layout which shows the row.
So Why we require to Create the new TableRow object:
A simple answer will be the same view item can not be at the multiple places.

Related

How to create table structure in Android programmatically not in xml file

I am creating an Android app for online shopping. I just want to generate invoice after payment in table format programmatically. Please guide me to create a table view in java program of Android.
This is how you create a Table Layout Programmatically for both Linear Layout and Relative Layout :
-------------------------------------------------------------------------
1) Linear Layout
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(tableParams);
TextView textView = new TextView(context);
textView.setLayoutParams(rowParams);
tableLayout.addView(tableRow);
tableRow.addView(textView);
-------------------------------------------------------------------------
2) Relative Layout
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(new Relative.LayoutParams(Relative.LayoutParams.WRAP_CONTENT, Relative.LayoutParams.WRAP_CONTENT));
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(tableParams);
TextView textView = new TextView(context);
textView.setLayoutParams(rowParams);
tableLayout.addView(tableRow);
tableRow.addView(textView);
-------------------------------------------------------------------------

getting data from dynamically created table row containing many textviews

i created a table row dynamically, That contains three textviews.now i want to get the value of three textview's value on click on tablerow(tr).means i want to get the companyTV,valueTV & YearTV's value.
Thanks.
for (int i = 0; i < match.length; i++) {
/** Create a TableRow dynamically **/
tr.setBackground(getResources().getDrawable(R.drawable.tv_bg));
tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.FILL_PARENT));
/** Creating a TextView to add to the row **/
companyTV = new TextView(this);
companyTV.setText(match[i]);
companyTV.isClickable();
companyTV.setTextColor(Color.RED);
companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
companyTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
companyTV.setPadding(5, 5, 5, 5);
tr.addView(companyTV); // Adding textView to tablerow.
/** Creating another textview **/
valueTV = new TextView(this);
valueTV.setText(kanr[i]);
valueTV.isClickable();
valueTV.setTextColor(Color.RED);
valueTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
valueTV.setPadding(5, 5, 5, 5);
valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(valueTV); // Adding textView to tablerow.
YearTV = new TextView(this);
YearTV.setText(ort[i]);
YearTV.setTextColor(Color.RED);
YearTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
YearTV.setPadding(5, 5, 5, 5);
YearTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(YearTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
First you have one error in your code when you are setting background of table row before you have created it. You should first create new table row, and then set its background:
for (int i = 0; i < match.length; i++) {
/** Create a TableRow dynamically **/
tr = new TableRow(this);
tr.setBackground(getResources().getDrawable(R.drawable.tv_bg));
You can access table row children views by index in your OnClickListener
tr.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
TableRow tr = (TableRow)v;
TextView companyTV, valueTV, yearTV;
String company, value, year;
companyTV = (TextView) tr.getChildAt(0);
valueTV = (TextView) tr.getChildAt(1);
yearTV = (TextView) tr.getChildAt(2);
company = companyTV.getText().toString();
value = valueTV.getText().toString();
year = yearTV.getText().toString();
}
});
If you want to track which table row is currently selected, you can create global variable for your activity
TableRow selectedTR;
and then set that inside OnClickListener
selectedTR = (TableRow)v;
When you use that selectedTR variable make sure that you check it for null value in case there is no selection made yet.

Android Adding more than one TextView to a TableRow Programmatically

I have a LinearLayout inside a FrameLayout for tab purposes. And I'm trying to add TableRow's to the LinearLayout in the code.
LinearLayout testLayout = (LinearLayout)findViewById(R.id.testLayout);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
This gets my LinearLayout and creates a TableRow to the specifications I want. I know this part is working.
TextView textOne = new TextView(this);
textOne.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textOne.setText("One");
TextView textTwo = new TextView(this);
textTwo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textTwo.setText("Two");
Here I make my two TextViews with no problem.
tableRow.addView(textOne);
tableRow.addView(textTwo);
testLayout.addView(tableRow, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Here is where I assume everything goes wrong. What happens is it only shows the textTwo. I don't know why it's not showing both of them in order like a normal TableRow would in XML. And I repeat, this must be done in code.
Please help, thank you.
Have you imported this import android.widget.TableRow.LayoutParams
Below code works for me
TableLayout tl = (TableLayout) findViewById(R.id.spreadsheet);
TableRow tr = new TableRow(this);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
TextView tvLeft = new TextView(this);
tvLeft.setLayoutParams(lp);
tvLeft.setBackgroundColor(Color.WHITE);
tvLeft.setText("OMG");
TextView tvCenter = new TextView(this);
tvCenter.setLayoutParams(lp);
tvCenter.setBackgroundColor(Color.WHITE);
tvCenter.setText("It");
TextView tvRight = new TextView(this);
tvRight.setLayoutParams(lp);
tvRight.setBackgroundColor(Color.WHITE);
tvRight.setText("WORKED!!!");
tr.addView(tvLeft);
tr.addView(tvCenter);
tr.addView(tvRight);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
I think you want to create your TextView dynamically and should're getting error "removeView () parent." Here is a good solution for that:
TableView tlSkills = (TableView) findViewById(R.id.myTableView);
if(listSkills.size() > 0) {
TableRow tableRow = new TableRow(getContext());
int i = 0;
for (Skills s : listSkills) {
TextView textView = new TextView(getContext());
textView.setText("" + s.getName());
tableRow.addView(textView);
if(i > 0) {
tlSkills.removeView(tableRow);//this is to avoid the error I mentioned above.
}
tlSkills.addView(tableRow);
i++;
}
}

Android - Dynamically Adding TableRow to TableLayout using an existing TableRow layout

I'm trying to adding TableRows to a TableLayout, which is working fantastically. However, I'm running into some problems with layout parameters. The spacing between the TextViews within a TableRow isn't working out like I thought it would.
My current code looks as follows.
paymentTable = (TableLayout) findViewById(R.id.paymentTable);
for(i = 0; i < cursor.getCount(); i++) {
TableRow row = new TableRow(this);
TextView payAmount = new TextView(this);
payAmount.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_AMOUNT)));
payAmount.setPadding(payAmount.getPaddingLeft(),
payAmount.getPaddingTop(),
textView.getPaddingRight(),
payAmount.getPaddingBottom());
TextView payDate = new TextView(this);
payDate.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_DATE)));
row.addView(payDate);
row.addView(payAmount);
paymentTable.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
cursor.moveToNext();
}
Afterwards, the TableLayout looks something along the lines of:
January$500
February$500
March$405
But I want it to look like:
January $500
February $500
March $405
To clarify things, I want each new TableRow and the TextViews it contains to inherit layout properties of an existing TableRow and TextView(s).
yawus this is a good tutorial will help you set the table layout
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/
this is the xml layout
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/
this is the activity code
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/8/
TableRow tableRow= new TableRow(this);
ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(row.get(0).toString());
tableRow.addView(idText);
TextView textOne = new TextView(this);
textOne.setText(row.get(1).toString());
tableRow.addView(textOne);
TextView textTwo = new TextView(this);
textTwo.setText(row.get(2).toString());
tableRow.addView(textTwo);
dataTable.addView(tableRow);
You need to add a RelativeLayout to each Table
TableRow row = new TableRow(this);
row.setId(tag);
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// as the parent of that RelativeLayout is a TableRow you must use TableRow.LayoutParams
TableRow.LayoutParams rlp = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT);
rlp.setMargins(0, 0, 0, 0);
row.addView(relativeLayout, rlp);
Then you add your TextView to the RelativeLayout. Like this
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payAmount = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_LEFT);
payAmount.setLayoutParams(lp);
relativeLayout.addView(payAmount);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payDate = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
payDate.setLayoutParams(lp);
relativeLayout.addView(payDate);
This is the way I handle positioning interface components within a table row.

Android: How to create such layout dynamicaly (not by xml)?

In My application i want to create the layout as like below image:
So How to make it possible ?
I have done Something like below code:
public void showResult()
{
List<TextView> textListWord = new ArrayList<TextView>(tempEmployerList.size());
List<TextView> textListAnswer = new ArrayList<TextView>(tempEmployerList.size());
List<TextView> imageListAnswer = new ArrayList<TextView>(tempEmployerList.size());
for(int i = 0; i<=tempEmployerList.size()-1; i++)
{
LinearLayout innerLayout = new LinearLayout(this);
innerLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
innerLayout.setBackgroundColor(0x00000000);
// set the Multiple TextView
TextView mHeading = new TextView(getApplicationContext());
TextView middleValue = new TextView(getApplicationContext());
TextView aImageView = new TextView(getApplicationContext());
mHeading.setText("\n"+"1");
//mHeading.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mHeading.setTextColor(0xFFFF0000);
mHeading.setPadding(3, 0, 0, 0);
middleValue.setText("\n"+"2");
//middleValue.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
middleValue.setTextColor(0xFFFF0000);
middleValue.setGravity(Gravity.RIGHT);
middleValue.setPadding(2, 0, 9, 0);
aImageView.setText("\n"+"3");
//aImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
aImageView.setGravity(Gravity.RIGHT);
aImageView.setTextColor(0xFF000000);
aImageView.setPadding(0, 0, 9, 0);
View line = new View(getApplicationContext());
//line.setOrientation(1);
line.setLayoutParams(new LayoutParams(2, android.view.ViewGroup.LayoutParams.FILL_PARENT));
line.setBackgroundColor(0xFF000000);
/**** Any other text view setup code ****/
innerLayout.addView(mHeading);
innerLayout.addView(middleValue);
innerLayout.addView(aImageView);
innerLayout.addView(line);
myLinearLayout.addView(innerLayout);
textListWord.add(mHeading);
textListAnswer.add(middleValue);
imageListAnswer.add(aImageView);
}
}
So please guide me that what more i have to do to create such view ?
Thanks.
try TableLayout and TableRow instead of LinearLayout.
You can create TableLayout in xml as well, as it, I think would be static and add TableRows. To Create a TableLayout in Java use,
TableLayout tblLayout=new TableLayout(this);
Set LayoutParams and other properties of table layout in java, like I am setting LayoutParams:
LayoutParams params=new LayoutParams(LayoutParams.Fill_Parent, LayoutParams.Wrap_Content);
tblLayout.setLayoutParams(params);
Create a loop for TableRows creation and insertion:
for(int i=0;i<arrList1.size();i++)
{
TableRow row=new TableRow(this);
row.setLayoutParams(params);
TextView lbl1=new TextView(this);
lbl1.setText(arrList1.get(i));
row.addView(lbl1);
TextView lbl2=new TextView(this);
lbl2.setText(arrList2.get(i));
row.addView(lbl2);
TextView lbl3=new TextView(this);
lbl3.setText(arrList3.get(i));
row.addView(lbl3);
tblLayout.addView(row);
}

Categories

Resources