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);
}
Related
public class TimeTableActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout activity_time_table = new TableLayout(this);
activity_time_table.setStretchAllColumns(true);
activity_time_table.setShrinkAllColumns(true);
TableRow rowTitle = new TableRow(this);
rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);
TableRow rowDayLabels = new TableRow(this);
TableRow monday = new TableRow(this);
TableRow tuesday = new TableRow(this);
TableRow wednesday=new TableRow(this);
TableRow thursday=new TableRow(this);
TableRow friday=new TableRow(this);
TableRow saturday=new TableRow(this);
TextView empty = new TextView(this);
// title column/row
TextView title = new TextView(this);
title.setText("Time Table");
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
title.setGravity(Gravity.CENTER);
title.setTypeface(Typeface.SERIF, Typeface.BOLD);
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 6;
rowTitle.addView(title, params);
// labels column
TextView monLabel = new TextView(this);
monLabel.setText("Monday");
monLabel.setTypeface(Typeface.DEFAULT_BOLD);
TextView tueLabel = new TextView(this);
tueLabel.setText("Tuesday");
tueLabel.setTypeface(Typeface.DEFAULT_BOLD);
TextView wedLabel=new TextView(this);
wedLabel.setText("Wednesday");
wedLabel.setTypeface(Typeface.DEFAULT_BOLD);
TextView thurLabel=new TextView(this);
thurLabel.setText("Thursday");
thurLabel.setTypeface(Typeface.DEFAULT_BOLD);
TextView friLabel=new TextView(this);
friLabel.setText("Friday");
friLabel.setTypeface(Typeface.DEFAULT_BOLD);
TextView satLabel=new TextView(this);
satLabel.setText("Saturday");
satLabel.setTypeface(Typeface.DEFAULT_BOLD);
rowDayLabels.addView(empty);
monday.addView(monLabel);
tuesday.addView(tueLabel);
wednesday.addView(wedLabel);
thursday.addView(thurLabel);
friday.addView(friLabel);
saturday.addView(satLabel);
// day 1 column
//add days for thursday friday satruday rahul midnyt
TextView day1Label = new TextView(this);
day1Label.setText("09:00-09:55");
day1Label.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView mon1 = new TextView(this);
mon1.setText("6");
mon1.setGravity(Gravity.CENTER_HORIZONTAL);
TextView tue1 = new TextView(this);
tue1.setText("7/8");
tue1.setGravity(Gravity.CENTER_HORIZONTAL);
TextView wed1=new TextView(this);
wed1.setText("4");
wed1.setGravity(Gravity.CENTER_HORIZONTAL);
TextView thur1=new TextView(this);
thur1.setText("3");
thur1.setGravity(Gravity.CENTER_HORIZONTAL);
TextView fri1=new TextView(this);
fri1.setText("1");
fri1.setGravity(Gravity.CENTER_HORIZONTAL);
TextView sat1=new TextView(this);
sat1.setText("3");
sat1.setGravity(Gravity.CENTER_HORIZONTAL);
rowDayLabels.addView(day1Label);
monday.addView(mon1);
tuesday.addView(tue1);
wednesday.addView(wed1);
thursday.addView(thur1);
friday.addView(fri1);
saturday.addView(sat1);
// day2 column
TextView day2Label = new TextView(this);
day2Label.setText("09:55-10:50");
day2Label.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView mon2 = new TextView(this);
mon2.setText("4");
mon2.setGravity(Gravity.CENTER_HORIZONTAL);
TextView tue2 = new TextView(this);
tue2.setText("7/8");
tue2.setGravity(Gravity.CENTER_HORIZONTAL);
TextView wed2=new TextView(this);
wed2.setText("2");
wed2.setGravity(Gravity.CENTER_HORIZONTAL);
TextView thur2=new TextView(this);
thur2.setText("5");
thur2.setGravity(Gravity.CENTER_HORIZONTAL);
TextView fri2=new TextView(this);
fri2.setText("3");
fri2.setGravity(Gravity.CENTER_HORIZONTAL);
TextView sat2=new TextView(this);
sat2.setText("1");
sat2.setGravity(Gravity.CENTER_HORIZONTAL);
rowDayLabels.addView(day2Label);
monday.addView(mon2);
tuesday.addView(tue2);
wednesday.addView(wed2);
thursday.addView(thur2);
friday.addView(fri2);
saturday.addView(sat2);
// day3 column
TextView day3Label = new TextView(this);
day3Label.setText("11:10-12:05");
day3Label.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView mon3 = new TextView(this);
mon3.setText("1");
mon3.setGravity(Gravity.CENTER_HORIZONTAL);
TextView tue3 = new TextView(this);
tue3.setText("7/8");
tue3.setGravity(Gravity.CENTER_HORIZONTAL);
TextView wed3=new TextView(this);
wed3.setText("5");
wed3.setGravity(Gravity.CENTER_HORIZONTAL);
TextView thur3=new TextView(this);
thur3.setText("6");
thur3.setGravity(Gravity.CENTER_HORIZONTAL);
TextView fri3=new TextView(this);
fri3.setText("2");
fri3.setGravity(Gravity.CENTER_HORIZONTAL);
TextView sat3=new TextView(this);
sat3.setText("-");
sat3.setGravity(Gravity.CENTER_HORIZONTAL);
rowDayLabels.addView(day3Label);
monday.addView(mon3);
tuesday.addView(tue3);
wednesday.addView(wed3);
thursday.addView(thur3);
friday.addView(fri3);
saturday.addView(sat3);
// day4 column
TextView day4Label = new TextView(this);
day4Label.setText("12:05-01:00");
day4Label.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView mon4 = new TextView(this);
mon4.setText("3");
mon4.setGravity(Gravity.CENTER_HORIZONTAL);
TextView tue4 = new TextView(this);
tue4.setText("6");
tue4.setGravity(Gravity.CENTER_HORIZONTAL);
TextView wed4=new TextView(this);
wed4.setText("3");
wed4.setGravity(Gravity.CENTER_HORIZONTAL);
TextView thur4=new TextView(this);
thur4.setText("4");
thur4.setGravity(Gravity.CENTER_HORIZONTAL);
TextView fri4=new TextView(this);
fri4.setText("5");
fri4.setGravity(Gravity.CENTER_HORIZONTAL);
TextView sat4=new TextView(this);
sat4.setText("-");
sat4.setGravity(Gravity.CENTER_HORIZONTAL);
rowDayLabels.addView(day4Label);
monday.addView(mon4);
tuesday.addView(tue4);
wednesday.addView(wed4);
thursday.addView(thur4);
friday.addView(fri4);
saturday.addView(sat4);
// day5 column
TextView day5Label = new TextView(this);
day5Label.setText("01:55-02:50");
day5Label.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView mon5 = new TextView(this);
mon5.setText("2");
mon5.setGravity(Gravity.CENTER_HORIZONTAL);
TextView tue5 = new TextView(this);
tue5.setText("4");
tue5.setGravity(Gravity.CENTER_HORIZONTAL);
TextView wed5=new TextView(this);
wed5.setText("6");
wed5.setGravity(Gravity.CENTER_HORIZONTAL);
TextView thur5=new TextView(this);
thur5.setText("1");
thur5.setGravity(Gravity.CENTER_HORIZONTAL);
TextView fri5=new TextView(this);
fri5.setText("7/8");
fri5.setGravity(Gravity.CENTER_HORIZONTAL);
rowDayLabels.addView(day5Label);
monday.addView(mon5);
tuesday.addView(tue5);
wednesday.addView(wed5);
thursday.addView(thur5);
friday.addView(fri5);
// day6 column
TextView day6Label = new TextView(this);
day6Label.setText("02:50-03:45");
day6Label.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView mon6 = new TextView(this);
mon6.setText("R");
mon6.setGravity(Gravity.CENTER_HORIZONTAL);
TextView tue6 = new TextView(this);
tue6.setText("2");
tue6.setGravity(Gravity.CENTER_HORIZONTAL);
TextView wed6=new TextView(this);
wed6.setText("T");
wed6.setGravity(Gravity.CENTER_HORIZONTAL);
TextView thur6=new TextView(this);
thur6.setText("R");
thur6.setGravity(Gravity.CENTER_HORIZONTAL);
TextView fri6=new TextView(this);
fri6.setText("7/8");
fri6.setGravity(Gravity.CENTER_HORIZONTAL);
rowDayLabels.addView(day6Label);
monday.addView(mon6);
tuesday.addView(tue6);
wednesday.addView(wed6);
thursday.addView(thur6);
friday.addView(fri6);
// day7 column
TextView day7Label = new TextView(this);
day7Label.setText("03:45-04:40");
day7Label.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView mon7 = new TextView(this);
mon7.setText("-");
mon7.setGravity(Gravity.CENTER_HORIZONTAL);
TextView tue7 = new TextView(this);
tue7.setText("-");
tue7.setGravity(Gravity.CENTER_HORIZONTAL);
TextView wed7=new TextView(this);
wed7.setText("-");
wed7.setGravity(Gravity.CENTER_HORIZONTAL);
TextView thur7=new TextView(this);
thur7.setText("-");
thur7.setGravity(Gravity.CENTER_HORIZONTAL);
TextView fri7=new TextView(this);
fri7.setText("7/8");
fri7.setGravity(Gravity.CENTER_HORIZONTAL);
rowDayLabels.addView(day7Label);
monday.addView(mon7);
tuesday.addView(tue7);
wednesday.addView(wed7);
thursday.addView(thur7);
friday.addView(fri7);
activity_time_table.addView(rowTitle);
activity_time_table.addView(rowDayLabels);
activity_time_table.addView(monday);
activity_time_table.addView(tuesday);
activity_time_table.addView(wednesday);
activity_time_table.addView(thursday);
activity_time_table.addView(friday);
activity_time_table.addView(saturday);
setContentView(activity_time_table);
}
}
how can i add rows to this table dynamically and insert textview for each row instead of writing the code for each hour ..how can it be done using for loops..and column having 7/8 should take span of 3 .
thnks in advance
instead of using table layout use list view and custom adapter
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;
}
}
I am trying to center a textview inside a tablerow, that is itself inside a tablelayout, but I guess I am missing something, because the TextView doesn't get centered :s
Here is my method:
private void insertClock() {
TableLayout table = new TableLayout(this);
table.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
table.setStretchAllColumns(true);
TableRow tbRow = new TableRow(this);
TableLayout.LayoutParams layoutRow = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
clock = new TextView(this);
TableRow.LayoutParams layoutHistory = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
clock.setLayoutParams(layoutHistory);
clock.setText(calculateClockText());
tbRow.setLayoutParams(layoutRow);
table.addView(tbRow);
clock.setGravity(Gravity.CENTER_HORIZONTAL);
tbRow.addView(clock);
}
Thanks in advance ;)
Finnaly made it working !
Here is the solution:
private void insertClock(TableLayout table, String text) {
TableRow tbRow = new TableRow(this);
TableLayout.LayoutParams layoutRow = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
tbRow.setLayoutParams(layoutRow);
clock = new TextView(this);
TableRow.LayoutParams layoutHistory = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
clock.setLayoutParams(layoutHistory);
clock.setText(text);
clock.setGravity(Gravity.CENTER);
tbRow.setGravity(Gravity.CENTER);
tbRow.addView(clock);
table.addView(tbRow);
}
HI am doing table layout programatically for that when i add table row to the layout application shows force close can any body help me and following is my code
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
TableRow tr = new TableRow(this);
TextView labelTV = new TextView(this);
TextView valueTV = new TextView(this);
// Go through each item in the array
for (int current = 0; current < numProvinces; current++)
{
// Create a TableRow and give it an ID
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to house the name of the province
labelTV.setId(200+current);
labelTV.setText(provinces[current]);
labelTV.setTextColor(Color.WHITE);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// Create a TextView to house the value of the after-tax income
valueTV.setId(current);
valueTV.setText("$0");
valueTV.setTextColor(Color.WHITE);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(valueTV);
// Add the TableRow to the TableLayout
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
setContentView(tl);
}
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
TableRow[] tr;
TextView[] labelTV;
TextView[] valueTV;
tr = new TableRow[numProvinces];
labelTV = new TextView[numProvinces];
valueTV= new TextView[numProvinces];
tl.removeAllViews();
tl.refreshdrawablestate();
for (int current = 0; current < numProvinces; current++)
{
tr[current] = new TableRow(this);
labelTV[current] = new TextView(this);
labelTV[current] = new TextView(this);
// Create a TableRow and give it an ID
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to house the name of the province
labelTV.setId(200+current);
labelTV.setText(provinces[current]);
labelTV.setTextColor(Color.WHITE);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// Create a TextView to house the value of the after-tax income
valueTV.setId(current);
valueTV.setText("$0");
valueTV.setTextColor(Color.WHITE);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(valueTV);
// Add the TableRow to the TableLayout
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
setContentView(tl);
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
for (int current = 0; current < numProvinces; current++)
{
TableRow tr = new TableRow(this);
TextView labelTV = new TextView(this);
TextView valueTV = new TextView(this);
/// Do your stuff
tr.addView(labelTV );
tr.addView(valueTV );
tbl.addView(tr);
}
Try this answer
The view already exist may be the Exception you are getting so,
TableRow tr = new TableRow(this);
add the above line in side for loop..
This is happening because you are trying to add same TableRow again and again..
Updated:
try this
tl.addView(tr);
instead of
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
I tested your code with a change in xml file and it work. I post my code so that you can compare with your own one.
I copy your code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//I add some data for test here:
int numProvinces = 5;
String[] provinces =new String[]{"1","2","3","4","5"};
//below is the same with your code:
TableLayout tl = (TableLayout) findViewById(R.id.maintable);
// Go through each item in the array
for (int current = 0; current < numProvinces; current++)
{
TableRow tr = new TableRow(this);
TextView labelTV = new TextView(this);
TextView valueTV = new TextView(this);
// Create a TableRow and give it an ID
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to house the name of the province
labelTV.setId(200+current);
labelTV.setText(provinces[current]);
labelTV.setTextColor(Color.WHITE);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// Create a TextView to house the value of the after-tax income
valueTV.setId(current);
valueTV.setText("$0");
valueTV.setTextColor(Color.WHITE);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(valueTV);
// Add the TableRow to the TableLayout
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
setContentView(tl);
}
}
and this is my main layout (main.xml)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/maintable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
I have list of product.That may be contain 5000 records.I put pagination.First It load 50 records.Then if we press next option(>) then load next 50 option likewise.I designed table-layout.
ArrayList<Product> productList = new ArrayList<Product>();
productList = getProducts();
TableLayout tl;
tl = (TableLayout) findViewById(R.id.tableLayout1);
if(productList.size() >0){
for (int i = 0; i < productList.size(); i++) {
TableRow tr = new TableRow(this);
tr.setTag(i);
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=10;
int topMargin=2;
int rightMargin=10;
int bottomMargin=2;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
TextView txtCode = new TextView(this);
TextView txtDes = new TextView(this);
EditText txtQty = new EditText(this);
TextView txtVal = new TextView(this);
TextView txtDisVal = new TextView(this);
TextView txtDisQty = new TextView(this);
txtQty.setId(i);
txtQty.setFocusable(true);
txtQty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txtQty.setHeight(5);
txtQty.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
createView(tr, txtCode, productList.get(i).getProductCode());
createView(tr, txtDes, productList.get(i).getDescription());
txtQty.setText(Double.toString(productList.get(i).getPrice()));
tr.addView(txtQty);
createView(tr, txtVal,"Value");
createView(tr, txtDisVal,"0.00");
createView(tr, txtDisQty,"0");
tr.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
System.out.println("Row Clicked with tag " + view.getTag());
}
});
tl.addView(tr);
}
}
}
public void createView(TableRow tr, TextView t, String viewdata) {
t.setText(viewdata);
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t.setTextColor(Color.DKGRAY);
t.setPadding(1, 0, 0, 0);
tr.setPadding(0, 0, 0, 0);
tr.addView(t);
}
This is my implementation.I didn't implement pagination part yet.
How can I get it into the memory and load it?
Please help me
I suggest you create a function to get products with a param pageindex。
Then you can set a trigger which activated by pagination event。
And update you UI by a handler