Multiple rows for table in android application - android

I am a starter in android application development. Kindly help me how to create multiple rows in a table and key performance to create a row. I am able to create 3 rows now. but i want to autogenerate the rows while entering the datas in table.

try this:
//Declare mytable in layout XML file.
TableLayout tl = (TableLayout) findViewById(R.id.mytable);
//loop through number of times u want to create row.
for (int current = 0; current < numofrowsrequired; current++)
{
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//add required views with required parameters
TextView labelTV = new TextView(this);
labelTV.setId(200+current);
labelTV.setText(provinces[current]);
labelTV.setTextColor(Color.BLACK);
labelTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(labelTV);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}

Related

dynamically added table is not displayed in android

there is something going wrong , such that firstly i created separately xml file for table row ,it did not work, then i created table row and text view programatically, again it is not displayed when i run , i can not get if what is wrong with my code
table = (TableLayout) findViewById(R.id.goodsTable);
for (int i = 0; i < attrName.length; i++) {
TableRow tr = new TableRow(getApplicationContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView atName = new TextView(getApplicationContext());
atName.setText(attrName[i]);
tr.addView(atName);
table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
table.requestLayout();
Try This it may be help to you
for (int i = 0; i < attrName.length; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView atName = new TextView(this);
atName.setText(attrName[i]);
atName.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(atName);
table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
You must apply LayoutParam for TextView and for create new Object for TableRow or TextView you should use Activity Context not ApplicationContext

Android - Dynamic ColSpan not working

I am having two table rows in one of my activity. First row will always contains 2 textview and second might contains one or two textview (it depends on userinput). Whenever I am having one textview in 2nd row, I tried to span it to two columns. I am trying something this
TableRow.LayoutParams param = (LayoutParams)editText.getLayoutParams();
param.span = 2;
txtView.setLayoutParams(param);
But its not working. What happens is, second txtview in first row is pushed out of the screen. So can anyone here tell where i am making mistake?
I made this example, hope it helps you.
TableLayout table = (TableLayout)findViewById(R.id.table_id_in_xml);
TableRow row = new TableRow(this);
TableRow row2 = new TableRow(this);
TextView col1, col2, col3;
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
row2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
col1 = new TextView(this);
col2 = new TextView(this);
col3 = new TextView(this);
col1.setText("col1");
col2.setText("col2");
col3.setText("col3");
row.addView(col1);
row.addView(col2);
row.addView(col3);
table.addView(row, new TableLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//HERE IS WHAT YOU NEED
TableRow.LayoutParams params = (TableRow.LayoutParams) row2.getLayoutParams();
params.span = 3; //amount of columns you will span
col1 = new TextView(this);
col1.setText("span on col1 makes it bigger than the others");
col1.setLayoutParams(params);
row2.addView(col1);
table.addView(row2, new TableLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Explaining the answer marked as correct - looks like you need to set layout params after adding the view to it's parent. for instance, add the textview to a row and then set layout params

how to put a custom view in a table row in android?

I am creating table rows programmatically and i want to put a view that i created before in a row. Is there any way to do it?
for ( current = 0; current < rapor1.length; current++)
{
TableRow tr = new TableRow(this);
tr.setId(100+current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView f1 = new TextView(this);
f1.setId(200+current);
f1.setText(rapor1[current]);
f1.setTextColor(Color.BLACK);
f1.setWidth(100);
tr.addView(f1);
DrawView dv = new DrawView(this);
tr.addView(dv);
it should be working but something is wrong.
I think it may helps. Just change this string:
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
And of course you need to set LayoutParams to any view that you want to show on your screen

how to apply textclick in tableview

In android, through xml parsing i have xml file values in tableview now which are stored in an array.
Now my question is i want to apply textclick on that values.
For example: if values are
Lotus
Lily
Rose
Jasmine
and i want to click on Rose
then how can i take particular array value from the table and apply intent on it???
the for loop that i have applied in parsing from where im getting array values:
for (int i = 0; i < data.getName().size(); i++) {
TableRow tr = new TableRow(this);
//tr.setId(100+i);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to house the name of the province
name[i]= new TextView(this);
name[i].setId(200+i);
name[i].setText(data.getName().get(i));
name[i].setTextColor(Color.WHITE);
name[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(name[i]);
value[i] = new TextView(this);
value[i].setId(i);
value[i].setText(data.getValue().get(i));
value[i].setTextColor(Color.WHITE);
value[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(value[i]);
t1.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
Well, if it's TableView, than it has some rows in it. Depending on implementation, you may wrap your textView into, for instance, linear layout and add click listener to it. For more information please be more specific regarding the issue.

How can I add extra TableRows to my TableLayout depending on cursor?

I have a database with different results in my database in my Android project. I want to fetch these results and show them in a tablelayout. In the tablelayout I want to add a row for each result, and write the column "date" on one textview, and the "result" on the second textview. Both textviews are on the same row. I fetch the data and store it in a cursor.
My question is: How do I use the cursor and add a row depending on how much data there is in my database?
Right now I try to add a row for each row in the cursor like this:
TableLayout tl = (TableLayout)findViewById(R.id.table_layout2);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView mTvDate = new TextView(this);
TextView mTvResult = new TextView(this);
int x=0;
for(int i=0;i<c.getCount();i++){
mTvDate.setText(date[x]);
mTvResult.setText(""+nocorrect[x]+" av "+ noqs[x] +" ("+ proc[x]+ "%)");
mTvDate.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
mTvResult.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(mTvDate);
tr.addView(mTvResult);
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
But I think I need to create a NEW tablerow, cause I get this error message:
02-22 09:22:17.543: ERROR/AndroidRuntime(333): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
But I don't want to remove it, I want to add more. Any ideas? Thanks!
Hi try this
i think you adding same row every time. so just declare it every loop iteration.
TableLayout tl = (TableLayout)findViewById(R.id.table_layout2);
int x=0;
for(int i=0;i<c.getCount();i++){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView mTvDate = new TextView(this);
TextView mTvResult = new TextView(this);
mTvDate.setText(date[x]);
mTvResult.setText(""+nocorrect[x]+" av "+ noqs[x] +" ("+ proc[x]+ "%)");
mTvDate.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
mTvResult.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(mTvDate);
tr.addView(mTvResult);
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}

Categories

Resources