The specified child already has a parent.must removeView first [duplicate] - android

I have a problem in Android creating a table adding rows dynamically. The error message is:
The specified child already has a parent. You must call removeView()
on the child's parent first
But why?
void setCalendario(List<ArrayList<String>> l){
TableLayout table = (TableLayout)findViewById(R.id.list_tableLayout1);
TableRow tr = new TableRow(this);
tr.removeAllViews();
tr.setPadding(0,10,0,10);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);
for (ArrayList<String> al : l){
int i = 0;
for(String s : al){
if (i == 0){
i++;
tv_item1.setText(s);
tv_item1.setGravity(Gravity.CENTER);
}
if (i == 1){
tv_item2.setText(s);
tv_item2.setGravity(Gravity.CENTER);
i++;
}
if (i == 2){
tv_item3.setText(s);
tv_item3.setGravity(Gravity.CENTER);
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
}
the xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.MSca.gorhinos.Calendario$PlaceholderFragment" >
<TableLayout
android:id="#+id/list_tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:stretchColumns="0,1,2,3" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"/>
</TableRow>
</TableLayout>
</RelativeLayout>

So, you once create tv_item1, tv_item2 and tv_item3. Then in cycle for all ArrayList you adding this views
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
At the second iteration you already add tv_item1 to tr. And you want to do it again. I guess you need just transfer this lines to cycle:
TableRow tr = new TableRow(this);
tr.removeAllViews();
tr.setPadding(0,10,0,10);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);

You're using a for-loop to add the same references to TextViews to your TableRow. So in the next iteration of the loop, the same objects are added to the TableRow (or TableLayout), again! They already have a parent by then.
Try to initialize the TableRow and TextView objects inside the (outer)for-loop.
EDIT: Modified your code.
void setCalendario(List<ArrayList<String>> l) {
// Here we initialize the objects we re-initialize every iteration of the loop
TableLayout table = (TableLayout)findViewById(R.id.list_tableLayout1);
for (ArrayList<String> al : l) {
TableRow tr = new TableRow(this);
// I can't believe a freshly initialized TableRow object has views attached...
tr.removeAllViews();
tr.setPadding(0,10,0,10);
// Not sure why these layout params are needed already, as they are specified
// when adding this TableRow to the TableLayout object as well.
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);
int i = 0;
for(String s : al) {
if (i == 0) {
i++;
tv_item1.setText(s);
tv_item1.setGravity(Gravity.CENTER);
}
if (i == 1) {
tv_item2.setText(s);
tv_item2.setGravity(Gravity.CENTER);
i++;
}
if (i == 2) {
tv_item3.setText(s);
tv_item3.setGravity(Gravity.CENTER);
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
}

because you are trying to put the same textView many times. you can use it only once, so you have to instantiate it again and again :
// you remove the definition of the text views here and you put it inside the loop
for (ArrayList<String> al : l){
int i = 0;
for(String s : al){
TextView tv_item2 = new TextView(this);
TextView tv_item1 = new TextView(this);
TextView tv_item3 = new TextView(this);
if (i == 0){
i++;
tv_item1.setText(s);
tv_item1.setGravity(Gravity.CENTER);
}
if (i == 1){
tv_item2.setText(s);
tv_item2.setGravity(Gravity.CENTER);
i++;
}
if (i == 2){
tv_item3.setText(s);
tv_item3.setGravity(Gravity.CENTER);
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
This is not the best way to do it, you should may be creat a methode that takes your "i" as parameter and returns a TextView (with gravity center ... and your staff).

Related

Create buttons programmatically goes new line

I created a layout which programmatically created buttons (see code). With this layout I have a column of buttons, but I would create something like the picture below. I tried something with linear layout but the buttons didn't go to new line automatically and didn't show, so I change it to table layout. How can I create something like the picture programmatically?
Thank you.
protected TableLayout layout;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout = (TableLayout) findViewById(R.id.workflows_activity_layout);
private void createButton() {
loading.setVisibility(View.VISIBLE);
layout.removeAllViews();
if (items.length == 0) {
TableRow row = new TableRow(this);
TextView no_item = new TextView(this);
no_questions.setText("there are no items");
no_questions.setTextSize(35);
row.addView(no_item);
layout.addView(row);
} else {
for (int i = 0; i < items.length; i++) {
TableRow row = new TableRow(this);
final Button btn = new Button(this);
TextView tw = new TextView(this);
tw.setText(items[i].getName());
tw.setTextSize(25);
btn.setBackgroundResource(R.drawable.button_image);
btn.setId(i);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int btn_selected = btn.getId();
Intent openItempage = new Intent(MainActivity.this, ItemsActivity.class);
startActivity(openItempage);
}
});
row.addView(btn);
row.addView(tw);
layout.addView(row, params);
items_buttons.add(btn);
items_nameText.add(tw);
}
}
loading.setVisibility(View.GONE);
}
There's an easy way to achieve what you want. Look at this code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<TableLayout
android:id="#+id/tab_lay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="*"
android:gravity="center_vertical" />
TabLayout tab_lay = (TableLayout)view.findViewById(R.id.tab_lay);
for(int i = 1; i <= 4; ){
TableRow a = new TableRow(getActivity());
TableRow.LayoutParams param = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
a.setLayoutParams(param);
a.setGravity(Gravity.CENTER_VERTICAL);
for(int y = 0; (y < 2) && (i <= 4); y++) {
Button x = new Button(getActivity());
x.setText("Text " + i);
x.setGravity(Gravity.CENTER);
TableRow.LayoutParams par = new TableRow.LayoutParams(y);
x.setLayoutParams(par);
int ids = i;
x.setId(ids);
x.setOnClickListener(this);
a.addView(x);
i++;
}
tab_lay.addView(a);
}
If you want more buttons, then just change 4 which is compare to i to your value. Hope I help.
You can implement this using labraries, for instance
FlowLayout
Usage:
<com.wefika.flowlayout.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start|top">
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum" />
</com.wefika.flowlayout.FlowLayout>
Another solution is:
AndroidFlowLayout
Usage:
<com.liangfeizc.flowlayout.FlowLayout
android:id="#+id/flow_layout"
flowlayout:vertical_spacing="10dp"
flowlayout:horizontal_spacing="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Clearing a Table Layout dynamically

I am currently working on an android app where I select a customer in an activity, it retrieves all records pertaining to that customer and displays them in a table layout in a new activity.
The problem arises when I go back to the previous activity and select a different customer, the new records are added behind the records of the previous customer (meaning the previous table layout entries are not deleted when I press the back button and go to select a new customer).
I've tried 2 solutions:
tl = (TableLayout) findViewById(R.id.maintable);
tl.removeAllViewsInLayout();
and
layout = (RelativeLayout)findViewById(R.id.layout1);
tl = (TableLayout) findViewById(R.id.maintable);
int count=tl.getChildCount();
for(int i=0;i<count;i++)
tl.removeView(layout.getChildAt(i));
Both these solutions do not work for me as the new data is still being added behind the existing data.
Could anyone suggest me a different solution that could work?
Also I am attaching the entire code.
OutstandingBills.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_outstanding_bills);
layout = (RelativeLayout)findViewById(R.id.layout1);
tl = (TableLayout) findViewById(R.id.maintable);
tl.removeAllViewsInLayout();
int count=tl.getChildCount();
for(int i=0;i<count;i++)
tl.removeView(layout.getChildAt(i));
addHeaders();
addData();
}
public void addHeaders(){
tr = new TableRow(OutstandingBills.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView bilDateTV = new TextView(OutstandingBills.this);
bilDateTV.setText("Date");
bilDateTV.setTextColor(Color.BLACK);
bilDateTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
bilDateTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
bilDateTV.setPadding(5, 5, 5, 0);
tr.addView(bilDateTV); // Adding textView to tablerow.
TextView billNoTV = new TextView(OutstandingBills.this);
billNoTV.setText("Bill No.");
billNoTV.setTextColor(Color.BLACK);
billNoTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
billNoTV.setPadding(5, 5, 5, 0);
billNoTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(billNoTV); // Adding textView to tablerow.
TextView dueAmtTV = new TextView(OutstandingBills.this);
dueAmtTV.setText("Amount Due");
dueAmtTV.setTextColor(Color.BLACK);
dueAmtTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
dueAmtTV.setPadding(5, 5, 5, 0);
dueAmtTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(dueAmtTV); // Adding textView to tablerow.
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr = new TableRow(OutstandingBills.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView divider = new TextView(OutstandingBills.this);
divider.setText("-----------------");
divider.setTextColor(Color.BLACK);
divider.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
divider.setPadding(5, 0, 0, 0);
divider.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider); // Adding textView to tablerow.
TextView divider2 = new TextView(OutstandingBills.this);
divider2.setText("-------------------------");
divider2.setTextColor(Color.BLACK);
divider2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
divider2.setPadding(5, 0, 0, 0);
divider2.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider2); // Adding textView to tablerow.
TextView divider3 = new TextView(OutstandingBills.this);
divider3.setText("-------------------------");
divider3.setTextColor(Color.BLACK);
divider3.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
divider3.setPadding(5, 0, 0, 0);
divider3.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(divider3); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
public void addData(){
for (int i = 0; i < arr.size(); i++)
{
tr = new TableRow(OutstandingBills.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
bilDate = new TextView(OutstandingBills.this);
bilDate.setText(arr.get(i).toString());
bilDate.setTextColor(Color.BLACK);
bilDate.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
bilDate.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
bilDate.setPadding(5, 5, 5, 5);
tr.addView(bilDate); // Adding textView to tablerow.
bilNo = new TextView(OutstandingBills.this);
bilNo.setText(arr1.get(i).toString());
bilNo.setTextColor(Color.BLACK);
bilNo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
bilNo.setPadding(5, 5, 5, 5);
bilNo.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(bilNo); // Adding textView to tablerow.
dueAmt = new TextView(OutstandingBills.this);
dueAmt.setText(arr2.get(i).toString());
dueAmt.setTextColor(Color.BLACK);
dueAmt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
dueAmt.setPadding(5, 5, 5, 5);
dueAmt.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(dueAmt); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
OutstandingBills.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:id="#+id/layout1"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.ashwin.projectx1.OutstandingBills">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0,1"
android:id="#+id/maintable" >
</TableLayout>
</ScrollView>
</RelativeLayout>
Implementing # Mr.Neo 's solution:
This is how I implemented your solution in my code:
addHeaders();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Add view for table layout here
addData();
}
}, 2000);
But there is still no effect on the TableLayout
Screenshots:
The first time I enter the activity with a Customer name
On re-entering the activity the second time, you can see the duplicate records
Just try using tl.removeAllViews() instead of tl.removeAllViewsInLayout().
Call this method before adding views in tablelayout.
Here is the solution I am using. Using removeAllViews() and add view after seconds to completely removing. If you add immediately, some rows could not be removed. I know this is not the best solution, but it's the best for me now.
tableMainContent.removeAllViews();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Add view for table layout here
}
}, 2000);

adding table layout programmatically in android

I'm adding table in linearlayout programmatically but it is not displaying on screen.
Below is code -
public void displayTable(){
TableLayout.LayoutParams lp2 = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableLayout mTableLayout=new TableLayout(getActivity());
TableRow mTableRow;
LinearLayout mTableLinearLayout;
TextView mSrNotxt,mDatetxt,mTimetxt;
Date mDate=new Date();
String mCDate=mDate.getDate()+"-"+(mDate.getMonth()+1)+"-"+(mDate.getYear()+1900)+"";
mTableLayout.setLayoutParams(lp2);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams tlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
for(int count=0;count<mTimeList.size();count++)
{
mTableRow=new TableRow(getActivity());
mTableRow.setLayoutParams(lp);
mSrNotxt=new TextView(getActivity());
mDatetxt=new TextView(getActivity());
mTimetxt=new TextView(getActivity());
mSrNotxt.setTextColor(getResources().getColor(R.color.black));
mDatetxt.setTextColor(getResources().getColor(R.color.black));
mTimetxt.setTextColor(getResources().getColor(R.color.black));
mSrNotxt.setLayoutParams(tlp);
mDatetxt.setLayoutParams(tlp);
mTimetxt.setLayoutParams(tlp);
mSrNotxt.setTextSize(12);
mDatetxt.setTextSize(12);
mTimetxt.setTextSize(12);
if(count==0){
mSrNotxt.setText("No.");
mDatetxt.setText("Date");
mTimetxt.setText("Time in Min.");
}
else{
mSrNotxt.setText((count+1+""));
mDatetxt.setText(mCDate);
mTimetxt.setText(mTimeList.get(count));
}
mTableLinearLayout=new LinearLayout(getActivity());
mTableLinearLayout.setLayoutParams(lp);
mTableLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
mTableRow.addView(mTableLinearLayout);
mTableLayout.addView(mTableRow);
}
System.out.println("table created");
mChartLayout.addView(mTableLayout);
}
xml view for chartLayout -
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/chartView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
How can I display table?Is there any way?
Answer -
Need to add textview in view -
mTableLinearLayout.addView(mSrNotxt);
mTableLinearLayout.addView(mDatetxt);
mTableLinearLayout.addView(mTimetxt);
I am able to add Table using this code :
TableLayout mTableLayout = new TableLayout(this);
mTableLayout.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int count = 0; count < 3; count++) {
TableRow row = new TableRow(this);
row.setLayoutParams((new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT)));
TextView valueTV = new TextView(this);
valueTV.setText("text : "+count);
// valueTV.setId(5);
valueTV.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
row.addView(valueTV);
mTableLayout.addView(row);
}
mChartLayout.addView(mTableLayout);
Add other required properties
Hope it helps ツ

Android: You must call removeView() on the child's parent first 2

I have a problem in Android creating a table adding rows dynamically. The error message is:
The specified child already has a parent. You must call removeView()
on the child's parent first
But why?
void setCalendario(List<ArrayList<String>> l){
TableLayout table = (TableLayout)findViewById(R.id.list_tableLayout1);
TableRow tr = new TableRow(this);
tr.removeAllViews();
tr.setPadding(0,10,0,10);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);
for (ArrayList<String> al : l){
int i = 0;
for(String s : al){
if (i == 0){
i++;
tv_item1.setText(s);
tv_item1.setGravity(Gravity.CENTER);
}
if (i == 1){
tv_item2.setText(s);
tv_item2.setGravity(Gravity.CENTER);
i++;
}
if (i == 2){
tv_item3.setText(s);
tv_item3.setGravity(Gravity.CENTER);
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
}
the xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.MSca.gorhinos.Calendario$PlaceholderFragment" >
<TableLayout
android:id="#+id/list_tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:stretchColumns="0,1,2,3" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"/>
</TableRow>
</TableLayout>
</RelativeLayout>
So, you once create tv_item1, tv_item2 and tv_item3. Then in cycle for all ArrayList you adding this views
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
At the second iteration you already add tv_item1 to tr. And you want to do it again. I guess you need just transfer this lines to cycle:
TableRow tr = new TableRow(this);
tr.removeAllViews();
tr.setPadding(0,10,0,10);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);
You're using a for-loop to add the same references to TextViews to your TableRow. So in the next iteration of the loop, the same objects are added to the TableRow (or TableLayout), again! They already have a parent by then.
Try to initialize the TableRow and TextView objects inside the (outer)for-loop.
EDIT: Modified your code.
void setCalendario(List<ArrayList<String>> l) {
// Here we initialize the objects we re-initialize every iteration of the loop
TableLayout table = (TableLayout)findViewById(R.id.list_tableLayout1);
for (ArrayList<String> al : l) {
TableRow tr = new TableRow(this);
// I can't believe a freshly initialized TableRow object has views attached...
tr.removeAllViews();
tr.setPadding(0,10,0,10);
// Not sure why these layout params are needed already, as they are specified
// when adding this TableRow to the TableLayout object as well.
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView tv_item1 = new TextView(this);
TextView tv_item2 = new TextView(this);
TextView tv_item3 = new TextView(this);
int i = 0;
for(String s : al) {
if (i == 0) {
i++;
tv_item1.setText(s);
tv_item1.setGravity(Gravity.CENTER);
}
if (i == 1) {
tv_item2.setText(s);
tv_item2.setGravity(Gravity.CENTER);
i++;
}
if (i == 2) {
tv_item3.setText(s);
tv_item3.setGravity(Gravity.CENTER);
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
}
because you are trying to put the same textView many times. you can use it only once, so you have to instantiate it again and again :
// you remove the definition of the text views here and you put it inside the loop
for (ArrayList<String> al : l){
int i = 0;
for(String s : al){
TextView tv_item2 = new TextView(this);
TextView tv_item1 = new TextView(this);
TextView tv_item3 = new TextView(this);
if (i == 0){
i++;
tv_item1.setText(s);
tv_item1.setGravity(Gravity.CENTER);
}
if (i == 1){
tv_item2.setText(s);
tv_item2.setGravity(Gravity.CENTER);
i++;
}
if (i == 2){
tv_item3.setText(s);
tv_item3.setGravity(Gravity.CENTER);
tr.addView(tv_item1);
tr.addView(tv_item2);
tr.addView(tv_item3);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
This is not the best way to do it, you should may be creat a methode that takes your "i" as parameter and returns a TextView (with gravity center ... and your staff).

android - add TableRow (containing LinearLayout) to Table

I have a complicated table, so I would like to make one row a little more flexible for adding a button or text view, so I have the following code for adding TableRow to Table. I am trying to set LinearLayout to TableRow programmatically, but the row does not show up when I run it. I'd appreciated if somebody can point what the problem is.
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
final CheckBox checkbox = new CheckBox(this);
checkbox.setPadding(10, 5, 0, 0);
checkbox.setTextSize(TypedValue.COMPLEX_UNIT_PX, 15);
checkbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String item_clicked_on = (String) ((TextView) view).getText();
Toast.makeText(getApplicationContext(), item_clicked_on, Toast.LENGTH_SHORT).show();
}
});
TextView tv = new TextView(this);
tv.setText(" " + count + ". " + baby.getName());
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 14);
tv.setPadding(10, 10, 0, 0);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String item_clicked_on = (String) ((TextView) view).getText();
Toast.makeText(getApplicationContext(), item_clicked_on, Toast.LENGTH_SHORT).show();
}
});
checkbox.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tv.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout = new LinearLayout(tr.getContext());
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(checkbox, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(tv, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tableRow.addView(linearLayout);
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
I am trying to make it look like this xml.
<TableRow android:id="#+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:id="#+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<CheckBox android:id="#+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
<TextView android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Choice 1" android:textColor="#FFFFFF"></TextView>
</LinearLayout>
</TableRow>
You are adding linear layout to tableRow .
tableRow.addView(linearLayout);
But you are adding table row tr to Table table
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
I think you have to add tableRow to table. Once check it.

Categories

Resources