How to create a table dynamically? - java

I know that it's possible to have an XML file with a TableLayout, and another XML file with a row. And then it's somehow possible to add rows, from the second file, with custom content in the java code to the table in the first file. But I can't find an example that shows how this is done. So, does anyone here know where I can find an example that shows this? I know that I have seen it before.

Dynamically adding rows to TableLayout
Dynamically add rows to TableLayout
First results using Google. Whatever... the idea is really simple:
Get a reference of the TableLayout (either one declared in a XML file, or created by hand using new TableLayou(context))
For each row you want to add, create a new TableRow object. Again, you can do so by using an already defined TableRow in an XML and inflate it; or you can just created by using the new operator.
Add the items you want to put inside each row by using the addView method. Sometimes you will want to specify some TableRow.LayoutParams.
Add the TableRow to the TableLayout.

Once you have your TableLayout, you can inflate and add rows using:
TableRow row = (TableRow) LayoutInflater.from(this).inflate(
R.layout.table_row, tableLayout, false);
tableLayout.addView(row);

Related

Make excel like interface for android app

The question is how to make excel like interface for table with possibility of text modification.
Now i have parsed excel file to strings array and passed it to GridView structure.
GridView gvMain;
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
gvMain = new GridView(this);
gvMain.setAdapter(adapter);
Basically, this construction doesn't allow me to modify text in the cells. What i want to achieve is a EditText like behaviour for each cell (or you can say excel like behaviour when you can click on the cell and type what ever you want). As i understand for achieving this i must somehow override Adapter's getView method (?). Is it possible or i'am using unappropriate API (GridView)? Actually, i have tried with TableView, but i mentioned that for some reason this stuff works very slow (i have big excel file).
Can you help me to choose an appropiate way for solving this task? Thanks!

Java & Android: Best way to create a dynamically expandable list of EditTexts in an activity?

as I'm very new to Java and Android programming, I'm slowly getting used to the Android studio and the resource system, xml management and so on..
I'm trying to create an activity with a start count of 10 EditTextes, vertically listed in a LinearLayout.
Till this point, I can manage everything with a XML file & hard-coding the 10 EditTextes.
The problem with it is, that I'd like to provide a button that can be clicked by the user to automatically expand the list, for example add 10 more EditTextes to the list while runtime.
AFAIK, I can't solve this problem with XML only.
I know, I can get the layout (XML layout) to an object in my MainActivity class using LayoutInflater().inflate...
Here's my question:
Is it the right way to define the layout in a XML file and modify it with the method described above or would it be better to create the whole layout with Java in my class:
LinearLayout layout = new LinearLayout(this);
// add EditTexts
// set attributes
//setContentView(layout);
Are there any disadvantages using this way or is it the same as doing it with XML ? I mean, to address the created EditTexts later again, I also do have to pass an ID to them, so I also have to create an ids.xml where all those IDs are listed. That sounds like much work for me..
Am I on the right way or are there better options to create/manipulate layouts?
If you want to fill a listview with multiple views, it'll be best to use an Adapter. An Adapter can be used to fill a list, and add rows to this list.
If you want a tutorial on how to create a list: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
The best part about an Adapter is, that you are able to create a custom layouts for it. So if you want to have an List Item with only EditTexts or ImageViews, it's easy to create.
In the end, you'll only have 1 layout file for the row and 1 layout file which will contain the list.
Inflating a layout is always easier than to create them like " LinearLayout layout = new LinearLayout(this); // add EditTexts // set attributes //setContentView(layout);"
There are a lot of tutorials on how to create an Adapter, you'll get it!

Modify included layout in Android?

I am using an include tag in an Android layout file to include a relative layout row that I would like to repeat a few times. The problem is that after I include the row I cannot modify the text of a textview inside the layout. Is there anyway I can modify the text of a textview that is part of an include? Mainly I would like to insert 4 of these rows with custom text specified in the xml if possible.
You can do it only at runtime. Even if you do it at runtime, make sure you call findViewById from the parent view as Android doesn't allow you to use the same ID at more than one place in an xml file.
You can either add these 4 includes with a different id and get them from code or do what #Anis said and inflate them at runtime and put there your text.

How can I include XML into a main xml several times filled with different data?

I have a list of customer data which should be show as a list. This list contains buttons for contact infos.
I want to create an xml file which contains an "element" (including buttons, TextViews, ect.) and set the costumer data programatically and then add it to the main xml.
How can I do this in case of more customer data?
I was trying to use Viewstub. I have added two "element" but I was not able to set the data to de layout because ID-s were duplicated.
So the main problem is that I want to create the "element" in XML and not programatically and want to add it several times to a main xml filled with different data.
How can I perform this?
Why not ListView?
Inflate view in Activity, fill with data and add to the layout as many times as you want.
For example:
for(int i=0; i<size; i++) {
TableRow row = (TableRow) inflater.inflate(R.layout.table_row, table, false);
TextView name = ((TextView) row.findViewById(R.id.name));
name.setText(data.get(i));
table.addView(row);
}

Displaying tabular data in Android - TableLayout, custom Adapter or both?

In the past when I have needed to display large data sets in a table, I've just looped over the data and on every iteration I'd inflate an xml layout representing a TableRow, obtain references to the child controls, set their properties from the data source and add the row to a TableLayout. When just displaying simple rows of data or items in a grid, I use an adapter. Am I doing this correctly, or is there a way to use an adapter to display tabular data with column alignment like in a TableLayout? Or, is there some way to apply the ViewHolder pattern to the way I'm manually adding rows to a TableLayout to avoid the extra calls to inflate xml and get references to child controls (findViewById)?

Categories

Resources