to add rows and textviews to each day of the table dynamically - android

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

Related

DataTable in a fragment, pulling from SQLite...not able to show the table after all rows are loaded

I am working on a DataTable in a Fragment that pulls data from a SQLite database and will display it in a table that is browsable up and down and side to side. I am having a difficult time getting the final table to show after the FOR loop iterates. I think that the problem lies in the last line. What is the appropriate way to show the table after the last row is inserted? TIA...
public class DataTable extends Fragment {
SQLiteDatabase db;
String rowid, species, area, sampler;
TableRow tableRow;
TextView textView, textView1, textView2, textView3, textView4, textView5;
.
.
.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//View view = inflater.inflate(R.layout.datatable, container, false);
View view = inflater.inflate(R.layout.datatable, container, false);
//GridView gridView = (GridView) view.findViewById(R.id.grid); //this is fine
TableLayout tablelayout = (TableLayout)this.getActivity().findViewById(R.id.table);
DBAdapter msdb= new DBAdapter(getActivity().getApplicationContext(),"adfg", null);
db=msdb.getWritableDatabase();
showdata(view);
return view;
}
public void showdata(View view){
Cursor c = db.rawQuery("SELECT * FROM surveyDB",null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getActivity().getApplicationContext
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView,textView1,textView2,textView3,textView4,textView5;
tableRow = new TableRow(getActivity().getApplicationContext());
textView=new TextView(getActivity().getApplicationContext());
textView.setText("Rowid");
textView.setTextColor(Color.RED);
textView.setTypeface(null, Typeface.BOLD);
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
.
.
.
tableLayout.addView(tableRow)
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getActivity().getApplicationContext());
textView1 = new TextView(getActivity().getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("_id")));
//Toast.makeText(getActivity().getApplicationContext(), "SAVE Successful."+textView1+".", Toast.LENGTH_LONG).show();
textView2 = new TextView(getActivity().getApplicationContext());
textView2.setText(c.getString(c.getColumnIndex("species")));
textView3 = new TextView(getActivity().getApplicationContext());
textView3.setText(c.getString(c.getColumnIndex("area")));
//still need sampler...
textView1.setPadding(20, 20, 20, 20);
textView2.setPadding(20, 20, 20, 20);
textView3.setPadding(20, 20, 20, 20);
tableRow.addView(textView1);
tableRow.addView(textView2);
tableRow.addView(textView3);
tableLayout.addView(tableRow);
c.moveToNext() ;
}
c.close();
db.close();
// setContentView(tableLayout);
return ;
}
}
This works,
public class DataTable extends Fragment {
SQLiteDatabase database, db;
TableLayout tableLayout;
TableRow row;
TextView firstCol;
TextView secondCol;
TextView thirdCol;
TextView fourthCol;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.datatable, container, false);
tableLayout = (TableLayout) view.findViewById(R.id.table);
DBAdapter msdb= new DBAdapter(getActivity().getApplicationContext(),"adfg", null);
db=msdb.getWritableDatabase();
displayDB();
return view;
}
private void displayDB() {
Cursor c=db.rawQuery("SELECT * FROM surveyDB", null);
Integer index0=c.getColumnIndex("_id");
Integer index1 = c.getColumnIndex("species");
Integer index2 = c.getColumnIndex("area");
Integer index3 = c.getColumnIndex("sampler");
if(c.getCount()>0){
c.moveToFirst();
do{
row=new TableRow(getActivity());
row.setId(100);
row.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
//Setting up the first column parameters
firstCol=new TextView(getActivity());
firstCol.setText(c.getString(index0));
firstCol.setTextSize(12);
firstCol.setTextColor(Color.YELLOW);
firstCol.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
row.addView(firstCol);
//Setting up the second column parameters
secondCol=new TextView(getActivity());
secondCol.setText(c.getString(index1));
secondCol.setTextColor(Color.YELLOW);
secondCol.setTextSize(12);
secondCol.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
row.addView(secondCol); //adding coloumn to row
//Setting up the third column parameters
thirdCol=new TextView(getActivity());
thirdCol.setText(c.getString(index2));
thirdCol.setTextColor(Color.YELLOW);
thirdCol.setTextSize(12);
thirdCol.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
row.addView(thirdCol); //adding column to row
//Setting up fourth column parameters
fourthCol=new TextView(getActivity());
fourthCol.setText(c.getString(index3));
fourthCol.setTextColor(Color.YELLOW);
fourthCol.setTextSize(12);
fourthCol.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
row.addView(fourthCol);
tableLayout.addView(row,new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}while(c.moveToNext());
db.close();
}else{
Toast.makeText(getActivity().getApplicationContext(), "Event occurred.", Toast.LENGTH_LONG).show();
}
}
}

Center TextView Programmatically on TableRow

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);
}

I got a force close when adding table row programatically?

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"
/>

How to get list of product in memory in Android

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

TableRow dynamically created with different contents

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);
}

Categories

Resources