How to load data from database for viewpager - android

In simpler terms, I have a viewpager2 which is supposed to display words and their meanings and users can be able to search and go a specific word just like a dictionary application. The problem is that I have a very huge database of a million words and loading it at once that's forever and slows the app down.
What's the best way to implement this?
My idea was to load five words at a time and load another set if the last word is reached but this seems impractical if the user search for another word. Is there a better method?

Before of all load it in another thread, and also try to load by limiting result

Related

Confused after WebView very slow on large data sets

After many days, I almost finished my first app but the WebView ruined it in the end. Search functionality is way too slow with it.
The HTML/CSS page I wanted to display, after search, can sometimes become very big in size like one made with looping and concatenation over 7100 records from DB.
This takes forever for WebView to load.
I thought WebView would be easier option to start with as my data was HTML/CSS.
Now, I am considering to use some other way like TextView with ScrollView etc. I read that this needs my text to have all its apostrophe (') etc escaped.
Can anyone please suggest me what way should I take now. Please keep in mind that my data is going to be big. Like a user can search data(Book) that is about 25MB. So chances are something the user searches may be in all records that would be 25MB data needs to be displayed with smooth scrolling.
What is the best way to from here. I just need WebView to be replaced with some other View.
Thank you
EDIT:
I also have Arabic text along with English text, in case en/decoding should be considered.
You should do the search and pagination on the server, and use something like infinite scrolling on the client. The problem is not the technology that you chose but how you use it. Sending 25 MB worth of data to the client, at once, is never going to go well.
Take a look at this:
infinite scroll in android webview

Android Application - Multiple Pages with Static Content

I am looking to create an app that has about 50 pages of static content. I can give an example of what it would look like, so that it will be easy to understand the questions.
Imagine a Jokes app, with tens or hundreds of pages
The user can see a full list of jokes, which shows the headings in a list view
Selecting a joke subject will take them to the joke page
From there they can go 'Next' or 'Previous'
They should also be able to favorite a joke
Going to the Favorites pages, will list the favorites for them
The joke pages are static. I could add more jokes with an app update but there is no dynamic content. So I am planning to have any server side code that the app can call.
Now the questions:
In Android, can I achieve this with a single activity (for the joke display) and switch the content based on selection?
There are several to store the jokes - sqlite, separate html pages or just strings.xml. Which is better for these use cases?
If there are multiple headings within a single joke (i.e. formatting as bold for them to stand out), I need to store the formatting along with the content. So HTML looks like the option?
This may be out of scope, but I want to capture the content in a standard way so that if I build an iOS app for this, I can just worry about the UI part and use the same content. Again HTML is the option?
Thanks for looking.
Yes you can achieve this with a single activity.
This is really up to you but Android provides support for SQL Databases. You may also consider looking into content providers.
Note: I would not use strings.xml because you can't load new jokes into strings.xml. If you are getting your jokes dynamically from a website, then you really should either load your content into a database and have the app display from the database, or else just load each html page individually. The html page will be easier as you will basically just be making a browser app, but the database will certainly be faster and cleaner.
HTML is certainly an option, though this question seems a little bit vague. It really depends on how you want to get your jokes. If you want to grab them as HTML pages and just display them, then the work is done for you. If you want to parse through them and display them as an android specific app, then it will be more work but you have more control on the app side.
Yes if you want your app to work cross platform you can use HTML to standardize your view across multiple devices.

Load data before or after creating activity?

I know this is a general question, but I don't know where to ask this. I was wondering what is a good design practice: to load data before creating an activity; or to create the activity, load the data and populate the UI.
In my opinion, fetching the data before isn't a good idea since you have to pass the information in a bundle to the next activity. Furthermore, you are handling information that is not relevant to the current activity. The only reason I see to do this is if you have a LOT of data to load and you don't want to stop the user from interacting with the app the time being.
However, I've seen a lot of people who load the information in an AsyncTask before launching the activity, even if it's a few bytes. Why? Is this the expected way to do it?
You can't give 1 perfect answer for this question as there is many different ways of doing this.
If you have something that is similar to a news app you'll be able to load content minus images while browsing a list and if the user goes to detail you could load the image on the fly and just display the text. But thats just one way of doing it. Some might prefer loading the image before displaying a detail screen.
My advice will be to think about your users and their work flow and you won't fail completely. And take this question on a case to case basis.

best way to load data from a webservice into a listview

I'm making an adapter that it underline data loads from a web service and each time the user scrolls down it loads more data and adds it to the ArrayList behind that adapter. The data contains image URLs which I then lazy load in my getView method.
Is there a better solution for that?
And how do I properly cache those images?
And can I retain the data of the adapter when the user rotates the device? So no need to load them again from the beginning?
I use fragment to display that list?
I'll try to answer all three questions one by one.
is there a beter solution for that? (other then lazy-loading)
That depends. The nice part about lazy-loading is, that you don't need to have all data right at the start when you display the component. The problem often is, how to get new data, as it is needed.
So, when loading the new data from the network, it is really a question of how big the chunks of loaded data are. I would not load one new entry after the next one, but load e.g. 20 new ones, when the end of the list has been reached.
Of course this is just a random idea for a number, you'll need to find the perfect point between speed and usability for your case.
So when you are loading chunks of data from your web-service, which are delivered fast enough, it really is a cool concept. But if you're just reading one entry after another (and therefore, the user almost always waits for new data), this is really just annoying.
And how to proper cache the images?
There are multiple possible solutions available, some of those are:
Using the system-cache, like the browser (seems to be the
finest).
Implementing your own system which uses device-storage (e.g. the
SD-Card or the internal application storage (the first one is
preferred)).
You might also like the contents of this question (and it's accepted answer), giving you some general advises on memory-management.
can i retain the data of the adapter when the user rotates the device?
Old, deprecated anser:
Yes, you can use the "instance state" of the Activity to do this.
Basically, the onSaveInstanceState()-method from the Activity
allows you to populate a given Bundle with state-information, which
is then passed to the onCreate()-method.
The data you would save in the Bundle would be (for example) the
current position of the list or maybe even the list itself (depending
on the size).
Yes, use the Loaders API. It is also available in the compatibility library.

Is a listview with webview items appropriate for a news app?

I am building an app that scrapes a certain web site and presents the latest news in a listview, html formattted with an image, a title and a summary. When the user clicks on a row, the news article is showed. It works a bit like the CNet app and similar news apps.
I have no problems with the scraping part, but I am in doubt on how to manage the summary list.
My initial idea is to have a listview where each item is a webview. The listview is populated by a custom ArrayAdapter filled with the scraped html content. Each screen will show the summaries from the 30 most recent articles, ie. up to a month old.
Is this approach recommended or will 30 webviews take up too many system resources?
Would it actually be better to use on big webview, using simple html (ul) to show the summary list?
Also, is an array adaptor the right way to go, or would a cursor be better?
If there are completely different ways to do this, please let me know!
Thanks.
Webview is not a bad approach, but there are better ways. I'd suggest to show the text normally (I mean, in a TextView). I'd use WebView if you are going to show long HTML content that has a complex format or use CSS... but, what you want is showing a preview, so keep it simple. It will be also faster, and more maintainable if you do it in a TextView.
With regards to the adapter... it really depends on how are you persisting the data. If you are just fetching those feeds from internet, parsing them and showing them right away, then you have no choice but using an ArrayAdapter or something like that. On the other hand, if you are persisting your data into a Sqlite database, then CursorAdapter is much better.
Romain Guy, the developer on the Android team who is most vocal on the web helping developers actually said in one of his talks (which you can find on YouTube...not sure which one) that technically you "could" do that, but he'd be extremely upset with you if you did. lol
There is probably too much going on in a WebView to make this the ideal choice of ListView items. I would create a "model" object representing the data for each item that you're abstracting and just make an xml layout that you can populate in a custom adapter.
This video is GREAT info if you're working with ListView
http://www.youtube.com/watch?v=wDBM6wVEO70
I don't see why the application wouldn't be able to handle the 30 html feeds, so you should be good there.
A ListView should be fine to use. Obviously, this all depends on how you want the UI to look and feel.
Lastly, a cursor adapter is used when using a cursor from a db query. Unless you're storing the feeds in a local db, this doesn't seem to be what you're doing. So, the array adapter should work fine for you.

Categories

Resources