I have a dynamic table layout created in OnCreate(). Everytime the activity resumes, the data in the table rows can change, so I would like to destroy the table rows and create new ones.
How can I do that? Thanks!!!
Here is what the OnCreate table creation portion looks like:
tableLayout tl = new TableLayout(this);
for (i = 0; i < numberOfVariables; i++) {
TableRow tr = new TableRow(this);
CheckBox cb = new CheckBox(this);
tr.addView(cb);
TextView dv = new TextView(this);
tr.addView(dv);
EditText et = new EditText(this);
tr.addView(et);
tl.addView(tr);
}
ll.addView(tl);
Just use tl.removeAllViews();, and then recreate the rows.
Related
I want to dynamically add a row in a table whenever this function is called from a different class. Right now it crashes the program. I looked through many topics but couldn't find anything.
public void newRow(String time, String cloc, String loc, String contact) {
TableLayout tl = (TableLayout) findViewById(R.id.table);
TableRow tr = new TableRow(this);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
TextView t3 = new TextView(this);
TextView t4 = new TextView(this);
t1.setText(time);
t1.setText(cloc);
t1.setText(loc);
t1.setText(contact);
tr.addView(t1);
tr.addView(t2);
tr.addView(t3);
tr.addView(t4);
tl.addView(tr, TableLayout.LayoutParams.WRAP_CONTENT);
}
Don't create new instance of TableLayout on each call
Create a global instance of TableLayout so that it will be accessible
in newRow() method. Remember that you would want to creat just one
TableLayout and repeatedly add new rows to it.
You are calling setText() method repeatedly on t1 instance. I don't
think you intend to do that.
Post the crash logs so that I could help you further.
I'm designing an app which will accept user input into a TableLayout . The table has 4 headings . I'm unsure of how to get the data into the table . I have a successfully added a test piece of code but I can only add it once. I've attached the code below.
What I wish to know is how to get this to add more than one line to the table?
public void addNew(){
TableLayout tL = (TableLayout)findViewById(R.id.table);
TableRow tR = new TableRow(getApplicationContext());
TextView tV = new TextView(getApplicationContext());
tV.setTextColor(0xff000000);
tV.setText("TEST");
TextView tV2 = new TextView(getApplicationContext());
tV2.setTextColor(0xff000000);
tV2.setText("TEST");
TextView tV3 = new TextView(getApplicationContext());
tV3.setTextColor(0xff000000);
tV3.setText("TEST");
TextView tV4 = new TextView(getApplicationContext());
tV4.setTextColor(0xff000000);
tV4.setText("TEST");
tR.addView(tV);
tR.addView(tV2);
tR.addView(tV3);
tR.addView(tV4);
tL.addView(tR);
}
for (/* x rows */)
{
tL.addView(tR);
}
This should work but if you run into an issue with text view id's clashing you can create an array of text views and an array of table rows. Would make managing and creating them easier as well.
If you make them in an array by doing something like this.
TextView[] textViewArray = new TextView[textViewCount];
for(int i = 0; i < textViewCount; i++)
{
textViewArray[i] = new TextView(this);
}
then you would be able to handle individual values by doing something like this.
textViewArray[i].setText(/* This stuff */);
Edit: as Blumer pointed out, I was not adding the items to the table, so that this question appeared just because I was careless and I didn't see my mistake.
I am trying to create a dynamic TableLayout, as I have to receive results from the server and add rows based on the results, but the table is not updating. (Also, the TableLayout already has 1 initial row, the header row).
This is my code:
Room[] rooms = State.rooms;
TableLayout tblBookDetails = (TableLayout) findViewById(R.id.tblbookdetails);
for(int i = 0; i < rooms.length; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LayoutParams layout_wrapwrap = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout_wrapwrap.rightMargin = 10; //TODO: Convert into DP
Resources res = getResources();
TextView txt1 = new TextView(this);
txt1.setLayoutParams(layout_wrapwrap);
txt1.setTextColor(res.getColor(android.R.color.black));
txt1.setText(rooms[i].name);
TextView txt2 = new TextView(this);
txt2.setLayoutParams(layout_wrapwrap);
txt2.setTextColor(getResources().getColor(android.R.color.black));
txt2.setText(rooms[i].price + " " + rooms[i].currency);
EditText edit1 = new EditText(this);
edit1.setLayoutParams(layout_wrapwrap);
//Must use deprecated method, since support library does not provide for this.
edit1.setBackgroundDrawable(res.getDrawable(android.R.drawable.edit_text));
edit1.setEms(3);
edit1.setInputType(InputType.TYPE_CLASS_NUMBER);
EditText edit2 = new EditText(this);
edit2.setLayoutParams(layout_wrapwrap);
//Must use deprecated method, since support library does not provide for this.
edit2.setBackgroundDrawable(res.getDrawable(android.R.drawable.edit_text));
edit2.setEms(3);
edit2.setInputType(InputType.TYPE_CLASS_NUMBER);
Spinner spinner = new Spinner(this);
layout_wrapwrap.rightMargin = 0;
spinner.setLayoutParams(layout_wrapwrap);
Integer[] numbers = new Integer[rooms[i].count];
for(int j = 0; j < numbers.length; j++) {
numbers[j] = i + 1;
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(
BookActivity.this, R.layout.spinner_textview, numbers);
spinner.setAdapter(adapter);
tblBookDetails.addView(tr);
}
//Another exquisite beauty of Java.
Log.d("USR", Integer.valueOf(tblBookDetails.getChildCount()).toString());
tblBookDetails.invalidate();
tblBookDetails.refreshDrawableState();
To prevent any confusion, the Room[] array is just a simple property-holder class.
This code looks enormously convoluted, and the table is not updating. I've searched quite a bit on the Internet, and I could not find any solution for this problem.
Thank you in advance.
I see where you add tr to tblBookDetails, but I don't see anywhere where you put txt1, txt2, edit1, etc. in tr. Try adding those views to the row, and I think that should get you there, because right now you appear to be adding the TableRow, but there's nothing in it.
I have a little problem. I have a tool that will likly have a large an changing number of channels which should be displayed in an Layout. So I have to create the TextView for the displaying of the values in code. Which worked like a charm.
My problem:
How do I access the created "valueTV" Field in my Code? Specifically I want to write a Value of the accelerometer into it, which is stored in String SAccX.
Is there a method to see the created field with the R.id.XXX in the DDMS or Debug window of Eclipse?
If I try to access it with the assigning like an normal XML Object Eclipse gives me an error (something like " sAccX = (TextView) findViewById(R.id.valueTV201); ). I understand why this error is coming up but I don't know how to get around it ;-)
//Get Tablelayout
TableLayout ChannelTable = (TableLayout) findViewById(R.id.TableChannels);
//Create a new Row for every Channel
for (int current = 0; current < numberOfChannels; current++)
{
// Create a Table with new ID
TableRow tr = new TableRow(this);
tr.setId(100 + current);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Create a TextView to show the Name of the Channel
TextView TVChannels = new TextView(this);
TVChannels.setId(200 + current);
TVChannels.setText(channels[current]);
TVChannels.setTextColor(Color.DKGRAY);
TVChannels.setTextSize(20);
TVChannels.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(TVChannels);
// Create a TextView to house the values
TextView valueTV = new TextView(this);
valueTV.setId(200 + current);
valueTV.setTextColor(Color.DKGRAY);
TVChannels.setTextSize(20);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(valueTV);
// Add the TableRow to the TableLayout
ChannelTable.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
} /* End of OnCreate*/
Use tags instead of ID's
View.setTag()
and then to find the view with a certain tag use
findViewWithTag()
I want to populate the android tableview's rows with an array at runtime...
How to go about it....please advise
Thanks,
You can populate table view row's by reading tables child as follows:
TableLayout x = (TableLayout) findViewById(R.id.table_id);
for(int i=0;i<x.getChildCount();i++){
TableRow tr = (TableRow) x.getChildAt(index);
//Do here with table row, what u want to do.
}
TableLayout layout = (TableLayout) findViewById(R.id.table);
TableRow tr = new TableRow(context);
TextView view = new TextView(context);
view.setText("Test");
tr.addView(view);
layout.addView(tr);