I am trying to create a simple table programmatically. But the code crashes when row2.addView is called. With this error:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Here is my code:
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.contnt_2);
TableLayout tableLayout = new TableLayout(this);
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
tableRowParams.setMargins(1, 1, 1, 1);
TableRow row1 = new TableRow(this);
TableRow row2 = new TableRow(this);
TableRow row3 = new TableRow(this);
TextView text1 = new TextView(this);
text1.setText("DDDDDDDDDDDDD");
text1.setBackgroundColor(Color.GRAY);
TextView text2 = new TextView(this);
text2.setText("DDDDDDDDDDDDD");
text2.setBackgroundColor(Color.GRAY);
TextView text3 = new TextView(this);
text3.setText("DDDDDDDDDDDDD");
text3.setBackgroundColor(Color.GRAY);
row1.addView(text1, tableRowParams);
row1.addView(text2, tableRowParams);
row1.addView(text3, tableRowParams);
row2.addView(text1, tableRowParams);
row2.addView(text2, tableRowParams);
row2.addView(text3, tableRowParams);
row3.addView(text1, tableRowParams);
row3.addView(text2, tableRowParams);
row3.addView(text3, tableRowParams);
tableLayout.addView(row1);
tableLayout.addView(row2);
tableLayout.addView(row3);
setContentView(tableLayout);
}
I can't find a reason why it happens. how correctly to add rows to table ? Can you help me out?
This is because you created three TextViews and added them to your first TableRow with this code:
row1.addView(text1, tableRowParams);
row1.addView(text2, tableRowParams);
row1.addView(text3, tableRowParams);
Then tried adding that same TextView that you already created to Row 2.
You can't add the same TextView to multiple TableRows, because as the error says, each TextView can only have one parent.
You need to create three different TextViews for EACH TableRow.
The main problem with your approach: A view can't be a child of more than one parent in Android.
Here's a tutorial on how to create a TableLayout programmatically, maybe you get some ideas for improvements to your logic:
http://www.prandroid.com/2014/05/creating-table-layout-dynamically-in.html
I'll paste it here as well just in case:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] row = { "ROW1", "ROW2", "Row3", "Row4", "Row 5", "Row 6",
"Row 7" };
String[] column = { "COLUMN1", "COLUMN2", "COLUMN3", "COLUMN4",
"COLUMN5", "COLUMN6" };
int rl=row.length; int cl=column.length;
Log.d("--", "R-Lenght--"+rl+" "+"C-Lenght--"+cl);
ScrollView sv = new ScrollView(this);
TableLayout tableLayout = createTableLayout(row, column,rl, cl);
HorizontalScrollView hsv = new HorizontalScrollView(this);
hsv.addView(tableLayout);
sv.addView(hsv);
setContentView(sv);
}
public void makeCellEmpty(TableLayout tableLayout, int rowIndex, int columnIndex) {
// get row from table with rowIndex
TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);
// get cell from row with columnIndex
TextView textView = (TextView)tableRow.getChildAt(columnIndex);
// make it black
textView.setBackgroundColor(Color.BLACK);
}
public void setHeaderTitle(TableLayout tableLayout, int rowIndex, int columnIndex){
// get row from table with rowIndex
TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);
// get cell from row with columnIndex
TextView textView = (TextView)tableRow.getChildAt(columnIndex);
textView.setText("Hello");
}
private TableLayout createTableLayout(String [] rv, String [] cv,int rowCount, int columnCount) {
// 1) Create a tableLayout and its params
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
TableLayout tableLayout = new TableLayout(this);
tableLayout.setBackgroundColor(Color.BLACK);
// 2) create tableRow params
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
tableRowParams.setMargins(1, 1, 1, 1);
tableRowParams.weight = 1;
for (int i = 0; i < rowCount; i++) {
// 3) create tableRow
TableRow tableRow = new TableRow(this);
tableRow.setBackgroundColor(Color.BLACK);
for (int j= 0; j < columnCount; j++) {
// 4) create textView
TextView textView = new TextView(this);
// textView.setText(String.valueOf(j));
textView.setBackgroundColor(Color.WHITE);
textView.setGravity(Gravity.CENTER);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
Log.d("TAG", "-___>"+id);
if (i ==0 && j==0){
textView.setText("0==0");
} else if(i==0){
Log.d("TAAG", "set Column Headers");
textView.setText(cv[j-1]);
}else if( j==0){
Log.d("TAAG", "Set Row Headers");
textView.setText(rv[i-1]);
}else {
textView.setText(""+id);
// check id=23
if(id==23){
textView.setText("ID=23");
}
}
// 5) add textView to tableRow
tableRow.addView(textView, tableRowParams);
}
// 6) add tableRow to tableLayout
tableLayout.addView(tableRow, tableLayoutParams);
}
return tableLayout;
}
}
Related
I want to add radiobuttons for every cell in given below table and get their position based on row and column, how to achieve it? Kindly share your views. Thanks in advance. I have commented the code where i was adding text to each cells, may be at the same place we can add radiobuttons
Below is working code :
public class MainActivity extends Activity {
RelativeLayout rl;
RadioGroup rg;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] row = { "AA", "BB", "CC", "DD", "EE", "FF", "GG" };
String[] column = { "Col 1", "Col 2", "Col 3", "Col 4", "Col 5", "Col 6" };
int rl = row.length;
int cl = column.length;
Log.d("--", "R-Lenght--" + rl + " " + "C-Lenght--" + cl);
ScrollView sv = new ScrollView(this);
TableLayout tableLayout = createTableLayout(row, column, rl, cl);
HorizontalScrollView hsv = new HorizontalScrollView(this);
hsv.addView(tableLayout);
sv.addView(hsv);
setContentView(sv);
}
public void makeCellEmpty(TableLayout tableLayout, int rowIndex, int columnIndex) {
// get row from table with rowIndex
TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);
// get cell from row with columnIndex
TextView textView = (TextView) tableRow.getChildAt(columnIndex);
// make it black
textView.setBackgroundColor(Color.BLACK);
}
public void setHeaderTitle(TableLayout tableLayout, int rowIndex,
int columnIndex) {
// get row from table with rowIndex
TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);
// get cell from row with columnIndex
TextView textView = (TextView) tableRow.getChildAt(columnIndex);
textView.setText("Hello");
}
private TableLayout createTableLayout(String[] rv, String[] cv,
int rowCount, int columnCount) {
// 1) Create a tableLayout and its params
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
TableLayout tableLayout = new TableLayout(this);
tableLayout.setBackgroundColor(Color.BLACK);
// 2) create tableRow params
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
tableRowParams.setMargins(1, 1, 1, 1);
tableRowParams.weight = 1;
for (int i = 0; i < rowCount; i++) {
// 3) create tableRow
TableRow tableRow = new TableRow(this);
tableRow.setBackgroundColor(Color.BLACK);
final RadioButton[] rb = new RadioButton[10];
rl=(RelativeLayout) findViewById(R.id.rl);
rg=new RadioGroup(this);
for (int j = 0; j < columnCount; j++) {
// 4) create textView
TextView textView = new TextView(this);
// textView.setText(String.valueOf(j));
textView.setBackgroundColor(Color.WHITE);
textView.setGravity(Gravity.CENTER);
String s1 = Integer.toString(i);
String s2 = Integer.toString(j);
String s3 = s1 + s2;
int id = Integer.parseInt(s3);
Log.d("TAG", "-___>" + id);
if (i == 0 && j == 0) {
textView.setText("0==0");
} else if (i == 0) {
Log.d("TAAG", "set Column Headers");
textView.setText(cv[j - 1]);
} else if (j == 0) {
Log.d("TAAG", "Set Row Headers");
textView.setText(rv[i - 1]);
} else {
/*textView.setText("" + id);
// check id=23
if (id == 23) {
textView.setText("ID=23");
}*/
//Add Radiobuttons here
}
// 5) add textView to tableRow
tableRow.addView(textView, tableRowParams);
}
// 6) add tableRow to tableLayout
tableLayout.addView(tableRow, tableLayoutParams);
}
return tableLayout;
}
}
please use listview instead of table layout
ListView listView = (ListView) view.findViewById(R.id.listview);
// values is a StringArray holding some string values.
CustomAdapter customAdapter = new CustomAdapter (getActivity(), values);
listView.setAdapter(customAdapter );
Use multi-dimensional array of Radio Button or group or whatever you want...
RadioButton[][] rb = new RadioButton[][];
Use above statement for declaring it and for accessing it use two loops which are nested. One loop is nested in another.
for(int i=0;i<=5;i++)
{
for (int j=0;j<=5;j++)
{
rb[i][j] = new RadioButton(this);
}
}
I have searched and cannot find an answer to my issue so i hope i am not completely barking up the wrong tree (so to speak).
I am new to android and have started to create an app. My app on one screen creates and adds entries to a SQLite database using public class DatabaseHandler extends SQLiteOpenHelper and this all appears to work.
I retrieve all the data and populate it into a grid, again this now works.
My issue is I am unable to retrieve one complete line from the grid.
I populate/display the grid with the following code.
I have cut a lot out as the grid is made in stages, header, blank lines etc but the grid does display as I want.
The id’s work as when I touch a line it displays its unique id.
The onClick is right at the end and when I use getText() instead of getID() all it returns is the data in the labelDate. How do I retrieve all the labels as listed below?
package com.pump.diary;
import java.util.List;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
public class PumpDiaryReview extends Activity implements android.view.View.OnClickListener{
DatabaseHandler db = new DatabaseHandler(this);
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pump_diary_review);
TableLayout tl = (TableLayout) findViewById(R.id.gridview);
boolean doFirstHeadings = true;
String dateCheck = "";
Integer count=0;
List<Readings> readings = db.getAllReadings();
for (Readings re : readings)
{
if (doFirstHeadings == true)
{
//First record so setup the headings.
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.GRAY);
tr_head.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView label_date = new TextView(this);
label_date.setText("Date:Time");
TextView label_CP = new TextView(this);
label_CP.setText("CP");;
TextView label_BG = new TextView(this);
label_BG.setText("BG");
TextView label_QA = new TextView(this);
label_QA.setText("QA");
TextView label_CN = new TextView(this);
label_CN.setText("CN");
TextView label_KT = new TextView(this);
label_KT.setText("KT");
TextView[] tvHeaderArray = {label_date, label_CP, label_BG, label_QA, label_CN, label_KT};
for (TextView tvHeader : tvHeaderArray)
{
tvHeader.setTextColor(Color.WHITE);
tr_head.addView(tvHeader);
}
tl.addView(tr_head, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
doFirstHeadings = false;
count = 0;
}
// Create the table row
TableRow tr = new TableRow(this);
tr.setClickable(true);
tr.setId(100+count);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView labelDATE = new TextView(this);
TextView labelCP = new TextView(this);
TextView labelBG = new TextView(this);
TextView labelQA = new TextView(this);
TextView labelCN = new TextView(this);
TextView labelKT = new TextView(this);
TextView[] tvArray = {labelDATE, labelCP, labelBG, labelQA, labelCN, labelKT};
if (!dateCheck.equals(re.getDate()) || (dateCheck == null) || dateCheck == "")
{
//Add a blank line in.
TableRow tr_blank = new TableRow(this);
tr_blank.setId(10);
tr_blank.setBackgroundColor(Color.GRAY);
tr_blank.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView label_date = new TextView(this);
label_date.setId(20);
label_date.setText(re.getDate());
label_date.setTextColor(Color.WHITE);
tr_blank.addView(label_date);
tl.addView(tr_blank, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
labelDATE.setText(re.getTime());
labelCP.setText(re.getCP());
labelBG.setText(re.getBG());
labelQA.setText(re.getQA());
labelCN.setText(re.getCN());
labelKT.setText(re.getKT());
for (TextView tv : tvArray)
{
tv.setTextColor(Color.WHITE);
tv.setId(200+count);
tr.setOnClickListener(this);
tr.addView(tv);
}
//add this to the table row
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
dateCheck = re.getDate().toString();
++count;
}
}
public void onClick(View v)
{
if (v instanceof TableRow)
{
TableRow row = (TableRow) v;
TextView child = (TextView) row.getChildAt(0);
Toast toast = Toast.makeText(this, String.valueOf(child.getId()), Toast.LENGTH_SHORT);
toast.show();
}
}
}
I can supply all the code for the grid creation if required.
Thanks for any help.
My issue is I am unable to retrieve one complete line from the grid.
The onClick is right at the end and when I use getText() instead of
getID() all it returns is the data in the labelDate. How do I retrieve
all the labels as listed below?
I'm not certain I understand the question, but this will give you toast with all the ID's in a TableRow
public void onClick(View v) {
if (v instanceof TableRow) {
TableRow row = (TableRow) v;
String msg = "";
for (int i=0; i < row.getChildCount(); i++) {
TextView child = (TextView) row.getChildAt(i);
msg += String.valueOf(child.getId()) + " ";
}
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toast.show();
}
}
your problem is here:
TextView child = (TextView) row.getChildAt(0); <------------------
Toast toast = Toast.makeText(this, String.valueOf(child.getId()), Toast.LENGTH_SHORT);
toast.show();
you are specifically requesting the first child of the row and getting the text from it which of course would only yield one value. to get all the rest, i'd suggest going to your List<Readings> and specifying the object that you want by index. then you can just print out everything from the object. As for how you'd get this index...with your current implementation, i think perhaps tagging your textViews with the index as they are being made and being populated by your List might be the most straight-forward.
I used the code below to create a TableRow with content dynamically. It works good but I wish to get the values in the TableRow. Here is the sample code (I got it from Google), it has two text values in the TableRow. When I click the TableRow at any position it gives the corresponding value in the TableRow (I wish something similar to a ListView).
All_CustomsActivity.java
public class All_CustomsActivity extends Activity {
String companies[] = { "Google", "Windows", "iPhone", "Nokia", "Samsung",
"Google", "Windows", "iPhone", "Nokia", "Samsung", "Google",
"Windows", "iPhone", "Nokia", "Samsung" };
String os[] = { "Android", "Mango", "iOS", "Symbian", "Bada", "Android",
"Mango", "iOS", "Symbian", "Bada", "Android", "Mango", "iOS",
"Symbian", "Bada" };
TableLayout tl;
TableRow tr;
TableRow mTable = null;
TextView companyTV, valueTV;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tl = (TableLayout) findViewById(R.id.maintable);
// addHeaders();
addData();
}
/** This function add the headers to the table **/
public void addHeaders() {
/** Create a TableRow dynamically **/
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/** Creating a TextView to add to the row **/
TextView companyTV = new TextView(this);
companyTV.setText("Companies");
companyTV.setTextColor(Color.GRAY);
companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
companyTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
companyTV.setPadding(5, 5, 5, 0);
tr.addView(companyTV); // Adding textView to tablerow.
/** Creating another textview **/
TextView valueTV = new TextView(this);
valueTV.setText("Operating Systems");
valueTV.setTextColor(Color.GRAY);
valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
valueTV.setPadding(5, 5, 5, 0);
valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(valueTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// we are adding two textviews for the divider because we have two
// columns
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/** Creating another textview **/
TextView divider = new TextView(this);
divider.setText("-----------------");
divider.setTextColor(Color.GREEN);
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(this);
divider2.setText("-------------------------");
divider2.setTextColor(Color.GREEN);
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.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
/** This function add the data to the table **/
public void addData() {
for (int i = 0; i < companies.length; i++) {
/** Create a TableRow dynamically **/
tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// ImageView im = new ImageView(this);
// im.setBackgroundResource(R.drawable.sample_image);
// im.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
// LayoutParams.WRAP_CONTENT));
// tr.addView(im);
/** Creating a TextView to add to the row **/
companyTV = new TextView(this);
companyTV.setText(companies[i]);
companyTV.setTextColor(Color.RED);
companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
companyTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, 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(os[i]);
valueTV.setTextColor(Color.GREEN);
valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
valueTV.setPadding(5, 5, 5, 5);
valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
tr.addView(valueTV); // Adding textView to tablerow.
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// tr.setOnClickListener(new View.OnClickListener() {
// public void onClick(View view) {
// view.setBackgroundColor(Color.DKGRAY);
// }
// });
//
// tr.setOnLongClickListener(new View.OnLongClickListener() {
// public boolean onLongClick(View v) {
// mTable = (TableRow) v; // assign selected TableRow gobally
// openContextMenu(v);
// return true;
// }
// });
}
}
// #Override
// public void onCreateContextMenu(ContextMenu menu, View v,
// ContextMenu.ContextMenuInfo menuInfo) {
// super.onCreateContextMenu(menu, v, menuInfo);
// menu.add(0, v.getId(), 0, "Do YourStuff");
//
// }
//
// #Override
// public boolean onContextItemSelected(MenuItem item) {
// int ccount = (mTable).getChildCount();
// String[] str = new String[ccount];
// for (int i = 0; i < ccount; i++) {
// TextView tv = (TextView) (((TableRow) mTable)).getChildAt(i);
// str[i] = tv.getText().toString(); // set selected text data into the
// // String array
// }
// Toast.makeText(All_CustomsActivity.this, Arrays.toString(str), 2)
// .show();
// return true;
// }
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
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:id="#+id/maintable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1" >
</TableLayout>
</ScrollView>
</LinearLayout>
In the code above some lines are commented, these lines are what I already tried to get the row values but it failed. Can anyone help me with this?
I guess you're talking about getting those values on a TableRow click. If this is the case you could add a listener to your TableRow and use getChildAt to get a hold of the two TextViews and get the data:
//...
tr.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
TableRow t = (TableRow) view;
TextView firstTextView = (TextView) t.getChildAt(0);
TextView secondTextView = (TextView) t.getChildAt(1);
String firstText = firstTextView.getText().toString();
String secondText = secondTextView.getText().toString();
}
});
//...
To get data from textview first u need to identify its parent from that u need to get your textView child position then u can get data from it... Here is a sample
String myText;
((TextView) findViewById(R.id.tv)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TableRow tablerow = (TableRow)v.getParent();
TextView items = (TextView) tablerow.getChildAt(2);
myText = items.getText().toString();
}
});
spinnerSelection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TableRow selectedRow = (TableRow)spinnerSelection.getParent();
int index = tableLayout.indexOfChild(selectedRow);
Here index is the selected row index value and you can call all the views under the parent row (selectedRow) like the below set of lines.
Button roomNo = (Button) selectedRow.getChildAt(0);
Spinner selectionA = (Spinner) selectedRow.getChildAt(1);
Spinner selectionB = (Spinner) selectedRow.getChildAt(2);
Happy Coding!!
final TableLayout table = (TableLayout) findViewById(R.id.tableLayout);
TableRow row = new TableRow(this);
TextView t2 = new TextView(this);
t2.setText("test");
row.addView(t2);
Button bu = new Button(this);
bu.setBackgroundResource(R.drawable.del);
bu.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//I need to delete the tablerow
//how to do?
}
});
row.addView(bu);
table.addView(row, new TableLayout.LayoutParams(WC, WC));
**
i want to delete tablerow in bu.setOnClickListener
how to do removeViewAt() ,i cant find indexId
**
use removeView for removing tablerow as:
table.removeView(row);
NOTE: If they don't have unique id then use:
table.removeView(rowIndex);
and by using removeViewAt
for(int i = 0, j < table.getChildCount(); i < j; i++){
// then, you can remove the the row you want...
// for instance...
TableRow row = getChildAt(i);
if( something you want to check ) {
removeViewAt(i);
// or...
removeView(row);
}
}
I am creating TableRows dynamically. And there are two types of content for these TableRows.
Some have 4 Views.
And some have 2 Views.
The problem is that TableRows with two, try to occupy the same space as the layout of which have four.
This is happening:
Img ThisTextViewHasThisSize
Img TV0 __________________TV1 TV2
...
TableLayout tl = (TableLayout) findViewById(R.id.myTableLayout);
for (int i = 0; i < array.length; i++) {
TableRow tr = new TableRow(this);
ImageButton button = new ImageButton(this);
tr.addView(button);
TextView tv0 = new TextView(this);
tv0.setText(array[i].something0());
if (another type of TableRow) {
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
tv1.setText(array[i].something1());
tv2.setText(array[i].something2());
tr.addView(tv1);
tr.addView(tv2);
}
tr.addView(tv0);
tl.addView(tr);
}
Someone can tell me how to let the TableRows layouts totally independent from each other?
You can set the span of a view within the table by using TableRow.LayoutParams. Eg:
TableRow tr = new TableRow(this);
//all rows have this button
ImageButton button = new ImageButton(this);
tr.addView(button);
TextView tv0 = new TextView(this);
tv0.setText("hey");
if (some condition == true) {
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
tv1.setText("heay 2");
tv2.setText("hey 3");
tr.addView(tv0);
tr.addView(tv1);
tr.addView(tv2);
}else {
TableRow.LayoutParams trlp = new TableRow.LayoutParams();
trlp.span = 3;
tv.setLayoutParams(trlp);
tr.addView(tv0);
}