Android - Best way to show data without large amount of layout files - android

Thanks for all the help recently! Another question!
In the app I'm building, I currently have a layout file with an expandable lstview where you can choose 10 options. When you choose one of those options, you pick another 4 options. From picking the week, you then have 3 more options. Each of those 3 options will currently open a new activity that has a listview with a checkbox and textview in each row, with about 10 rows each.
Instead of making 30 layout files which will probably crash the app(not sure how many it would take to crash the app), I'm thinking there has to be a better way. I have looked it up and can't really find what I'm looking for. I have read a little bit about sqlite, but wouldn't you still need separate layout files to call different parts of the tables? Is there a way to make a single layout file with a list view, and fill the listview with different data from sqlite, depending on where the click came from in the expandable list view?
For example, if they click workout 29 on expandable list view, then click Week 1, then click Day 1.. can that single layout load data from sqlite db. Then if they were to go back and say click workout 30, week 2, day 3, that same layout load different info from the db?
I'm also trying to find out how to make a double expandable list view still
Thanks again!

So this is very simple all you have to do is create a DataAdapter for the listview. You can then add list view items which bind to the onclick listener and then you can clear that list and add new elements to it. a useful link would be.
https://guides.codepath.com/android/Using-an-ArrayAdapter-with-ListView

You should be using a SQLite database for all your data (unless you plan on holding the data in memory which is not a good idea if it is a rather large amount). You should just create a simple view and keep reusing it. What you will have to do is create your own adapter (extending from BaseExpandableListAdapter for Expandable list views and BaseAdapter for normal list views) and then query the SQLite databse table for the data that you need.
If you are only displaying String items, you can directly extend ArrayAdapter and then provide the array of items that you want to display for the normal list.
As far as crashing goes, there are many apps that have well over 30 layout files. Besides even if you did start loading a lot of views, Android will automatically start destroying views as it starts running low on memory and recreate them when it is needed to do so.

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.

displaying data in ListView programmatically

I'm developing an Android App and I need to display some data in a scrollable Listview.
I will extract some strings from objects that I right now have stored in linked lists (until i come up with a better solution).
I want to display a view that looks almost like the contacts app. A list with a small picture to the left and a larger text to the right of the picture with a smaller text the bigger one. When I click on one of the items it should open a new activity.
How do I create the view? If I adds an object in the linked list it should appear in the list.
I have checked some sample code, but they all uses Arrayadapter and I dont understand how I am supposed to do.
Check this article about Adapters: http://www.vogella.com/articles/AndroidListView/article.html
If you want to add new objects in ListVew use:
adapter.notifyDataSetChenged();
Check the libraries in this question, you have what you need there:
Lazy load of images in ListView

How can I have multiple pages of one ListView?

I currently have one long ListView in my android application and I would like to separate that one ListView into multiple pages so that when the user selects the next button, that will be in the actionbar, it goes to the next set of ListView items.
How do I go about achieving this?
You can write your own ListAdapter/ArrayAdapter and have it only load the items you want to. The arrayadapter is good and easy enough to use.
ArrayAdapter example,
here is another example

What is the most efficient way to populate a listview?

I have an app which mains view is a listview. Each item consist of multiple textviews and imageviews. Now, what is the most efficient way to populate these items? String arrays? External text file? Online text files?
for example this is an item in the listview:
The listview will contain over 50 items and these items will increase every week. So it should be as easy as possible to add new items!
First of all you pick an efficient way that works for YOUR project.
I would recommend just using an SQLite database for such.
You could use Text files, but you have a database availible for such, so i would recommend using it.
Here is more info on SQLite and how to get started using it.
EDIT:
Also you could possibly use online text files. And just read the information from them. This may take some time when populating the list, so i would recommend doing this in he background, and trying to write the information to a abase.
Using SQLite and getting started

How does android display a list with 200 elements?

When I have a ListActivity and an Adapter how does Android handle a list with 200 elements.
Does it try to load all of them directly how does it wait till the user scrolls and then renders those elements?
Do I have to worry with performance when a list is too long?
Depends how are the adapters implemented.
If you have an adapter that is not subclassed (you use one that is provider by SDK) Android will try to load all of them directly.
I recommend to subclass SimpleCursorAdapter and implement your custom adapter. This way you will have for example 10 views (as many your screen needs), and the view it will be reused for the rest of the 190 records.
There are several parts to this question. First of all, the data itself. Is that coming from a SQLite database via a query? If so, you have a Cursor object, which contains the entire result. So if you have a query that yields 200 rows, you will have all 200 rows in memory (which is why it's so important to narrow your projection).
As for the list itself, that part is pretty efficient - Android will only create views for the elements that you can actually see. And, depending on what kind of views you have, and whether they support recycling, Android will actually recycle existing objects to minimize the amount of overhead for initialization and memory management.
I'm not sure how Android handles it internally. But most programs I've seen handle the issue by loading 20 or so items and then making the last item say "Load next 20 items". Then when you click it, it loads the next 20 items.

Categories

Resources