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

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)?

Related

How to create a recyclerview wirth different design on first cell

Is there any way to create a recycler where its first cell is twice as big as the other cells and all the other cells are in between the top and bottom of the first cell. Here is a drawing example because I can not explain it properly with words.
Create a POJO class for each different view types and pass the values to the POJO classes and add the POJO as the recycler view item.You can insert and render multiple view types inside a recycler view .

Create dynamic views while scrolling in view like in Instagram

I'm using Retrofit2 to handle http comunication with backend. In response I'm getting data that i wish add to number of views specified in JSON file (size of array).
I want to create views like this but not the same. Basically picture and some text
I don't know what type of layout i should use. Grid, Relative, Linear, List? I also want to scroll this views.
You should maybe use recyclerview with cardview items.
You can set a scroll listener to the recyclerview , to define when you want to upload some new data .

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!

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

How to create a table dynamically?

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

Categories

Resources