What is the most efficient way to display a table in Android - android

I have a post to server which returns a JSONArray. I then populate a TableLayout by dynamically creating TableRows which get inserted into the TableLayout, and also dynamically create TextViews after which I set the text to values from the JSONArray. And add the TextViews to the Rows.
This does work. Although it seem terribly insufficient and I would really like a more elegant solution.
Any suggestions?

You are right, TableLayout is very inefficient, consider you have 100 rows of content, then you have to create 100 rows of widgets.. which is very memory consuming + slow in creation.
Above scenario should be a perfect fit for using ListView, you can simply de-serialize your JSON Array into a custom Adapter to push to the ListView. I usually create/use a JSON-Cursor Adapter so that it can be more reusable (fits to ContentProvider as well).

http://developer.android.com is the most efficient way to learn android...
check out this url for your result :
http://developer.android.com/guide/topics/ui/declaring-layout.html#CommonLayouts

Related

Is it possible to sort data in a textview?

I am using 'Codeofaninja' .
android table scroll code
He generates data. I replaced that with an sqlite database. It works well but...
I need to sort and the displays are textviews with tablerows.
Should I use listviews instead of the textviews/tablerows?
I have seen examples of data being sorted in a collection. I have my data in lists already but I have read that textviews have performance problems.
If the answer is listviews then I have to redesign the views which I am trying to not do. But if technology says I must then so be it.
So I have come up with 2 options:
1:textview gets repopulated with list after any data actions.
2:listview is where data is manipulated then stored back to db. Then I need to put a listview in the relativelayout view?
I have tried deleting the tablerows from the textview and reading data back in but this proves slow.
I searched on textviews and listviews and have seen many examples but it is still not clear as to what method is the preferred.
Thank you for input.
The idea is that the sorting is independent of the view. You sort the data in the collection (list, array, etc.) first, then use the ListAdapter (or ArrayAdapter, .etc) to populate the view.
From what you described, it seems the textviews are re-created every time, i.e, if you have 10 rows, each row has 3 textviews, did you create 30 textviews? In that case, sure it has performance problem. Try reading up on ViewHolder for ListView
Android Viewholder implementation
It is superior to use the idea of loading some data at a time. Both Android ListView and RecyclerView virtually loads data when required, and removes data when they are no longer needed.
One good tutorial about ListView # Populating a ListView With Data. Tell us what you think of it.
The only drawback for these GUI classes is when you only have small amount of data to show, which is not likely, from your post.

Need to make a table: which is better for performances?

In my app I download from the net some data I need to arrange in a table way.
I already know dimensions: about 26 rows and 6 columns, anyway data can change across subsequent calls so I have to create this table in a dynamic way.
As it is a table I though using a TableLayout (never used before), on the other hand I could simply use a list view, using for the single item a 6 elements linear layout with horizontal orientation.
ListView intrigues me because it is an adapter view (I think I'll store this data in a sqllite db, later) so populating it is quite simple and straightforward, anyway I don't know if it is the best for my problem.
What is your opinion?TableLayout, ListView or other?
When it comes to performance, I think you should use ListView because ListView support Recycling of view and you can check this and this post for more detail regarding Recycling of view.
Hope it helps :)
A ListView is a collection of layouts that are automatically displayed one below the other. The developer adds new items through a piece of code in the Activity. An example is the address book where you have lots of contacts one after the other.
A TableView on the other hand is defined in the layout file and does not only need to contain elements below or above each other but can also arrange elements on the left or on the right. A TableView is exactly what it says: A table with rows and columns.
here's a similar question that might answer what you want to know

Android create and populate list layout programmatically

I can't use listview because this layout will already requires scrolling. So this leaves me at a problem: how to create my tables.
What I know:
The size of the JSON array
I was going to try to make every cell in XML and do a for loop in the code, but my predicament is that I can't findViewById very well, because the R.id.myId names are difficult to account for, and are integers instead of strings. (probably a problem not as large as I am making it).
So here, I know the server call will return a json array with up to 5 objects in it. How can I populate my view?
I am using a LinearLayout for each row, much like I would be preparing a custom list view in preparation.
Insight appreciated.
Is there a specific reason you need it to be a ListView? If not you could try a LinearLayout with a vertical orientation as the container for your list. Just put the container in your xml and you can programatically add the rows that you generate using addView().

android database value retrieving

I have a database with three rows and columns.I have managed to insert the data into the table.Now i am trying to retrieve the values in a tabular format in my xml view.
How to go about this.
Any help is appreciated.
Thanks.
What you're asking for is a fair amount of work, but saying that, it's relatively straight forward.
The problem is, your request is so vague that people are reluctant to help.
One possible solution (although it might not fit what you need) would be:
1) Create a ListActivity
2) Create a custom SimpleCursorAdapter.
3) Create a custom xml view for each row.
4) override SimpleCursorAdapter's getView() to inflate said xml view
5) Override SimpleCursorAdapter's bindView() method to assign values from the cursor to the row.
The questions in my mind which stopped me from trying to code this for you are:
1) Might you want to change the number of columns dynamically?
2) Might you want more flexibility as to how the column widths are decided upon? etc..
A slightly more complex setup would use TableLayout and TableRow.

Link array of ImagesViews to TableLayout

Does anyone know, if you have an array of ImageViews[n][n], how to connect the array to a TableLayout? Furthermore, after it's displayed in the tableLayout, is there a way to tap the images in the table layout and swap places with the adjacent tile with a specific label?
Thanks!
You will just need to construct your TableLayout manually using addView().
You might want to check out GridView which provides an easy way to "link" data using Adapters.
Edit GridView may not work for you because it doesn't specify the number of items per row.
You might also think about making a custom View and rendering your images to a Canvas using the graphics calls in your onDraw() method.

Categories

Resources