What is the most efficient way to populate a listview? - android

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

Related

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

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.

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.

Android Adapt Data from a SQLite Database to a Listview

EDIT: After spending almost all day on this OutOfMemory Error I was getting, turns out I simply wasnt advancing a cursor. However, I still wish to obtain an answer for my question below. To clarify, my question is:
Is it faster to make a custom adapter for a listview that hooks directly into the database to retrieve the data, or should an arraylist of that data be made first and then passed into a default arrayadapter?
I am currently working on an application and as a part of one of its functions I need to be able to take data from an internal database and display parts of it in various listviews. These listviews are all linked together in a ViewFlipper to make it easy to move between the views. I am working with about 5000 values maximum at once. (That is the stress size for the data set that I am tasked to work with).
Should I write a custom adapter that directly links to the database and extracts the values, or is there a better way to go about this? I tried to create a sort of wrapper class for the database that would extract all necessary data from the database and place it into a POJO but i keep getting OutOfMemory exceptions (5 string values * 5000 rows = 25000 strings doesnt seem to be nicely accepted in my case).
Not with the same amount of values, 5000 but I had a similar problem.
I ended up using a private arrayList on a ListAdapter, the list will contain only partial lists, for example 100 items.
Your cursor can initially contain the values to fill the firsts 100 items and when you scroll down looking for more items you can launch another cursor to retreive the next 50 items. Controlling a range of 100-150 items at your arrayList by adding/removing new/old items and refreshing the adapter.
I vote up your question because maybe someone find a better way to do it and I would like to know as well.

Is it more efficient to load data to a listview from SQLite or create listviews from an array?

Hey guys I was just wondering about some efficiency issues. I am creating an app for android that has a few listviews, some listviews have 50+ items. Basically click one item on one listview will open up another listview that displays more options and so forth.
My question is right now I have these listviews populated from arrays but would it be more efficient to create a database and pull data to populate these listviews?
If this question has been asked already I apologize I have searched but have not found what I was looking for.
Beside of choosing between arrays or sql as datasource, is important to analyze the complexity of your view; sometimes is important "load views on demand" you may want consider this best-practices for optimization of your app: https://developer.android.com/training/improving-layouts/index.html and this https://developer.android.com/training/improving-layouts/smooth-scrolling.html
Best.-
If list data is not dynamic, I feel string arrays are best otherwise database is best option. database calls are always costly in terms of time/resources.
It depends on your data, if your data doesn't change , you can define them in xml file under ./value/. And sqlite just like other database, and it cost another I/O operations and it will reduce efficiency. But I think it doesn't cost too much in an android app.

Large ListView in Android

Building a dictionary application I need a ListView that shows over 100k items. I want to let the user scroll as much as needed.
What's the best practice for searching and showing words?
Is it ok to show 150,000 words in the ListView (for pereformance)?
If not how to add 100 other words after the user reaches to the end of the list?
Currently I show 50 words previous and 50 words next of searched word.
Thank you.
(second answer in response to clarification about performance)
There are different ways to do this based on where your data is.
The best way is to have your data in a sqlite database and use a CursorAdapter. Android then manages the fetching of your data and won't fetch data that isn't currently being shown on the screen.
If your words are in an array in memory, try ArrayAdapter or SimpleAdapter.
The Adapter interface, from which all of the above classes inherit, is designed to provide good ListView performance regardless of the number of objects in the list.
One built-in way to allow fast scrolling is via the fast scroll thumb. In your xml, set:
android:fastScrollEnabled="true"
on your ListView, or try:
listView.setFastScrollEnabled(true)
listView.setFastScrollAlwaysVisible(true)
RecyclerView is very good for this. I have developed an open source library especially designed to scroll through large list with extreme speed. In fact it can move well in excess of 1000 items per second both in single and multiple column layouts You can check out the repo here: https://bitbucket.org/warwick/hgfastlist or you can checkout the OpenGL version here: https://bitbucket.org/warwick/hgglfastlist. Here is a demo video on Youtube: https://www.youtube.com/watch?v=oz7aeAlOHBA&feature=youtu.be
Even if you don't want to use this library, there's loads of code to read through in the demo app that will give you good ideas.

Categories

Resources