Show search results on RecyclerView using CursorAdapter,CursorLoader - android

I have a RecyclerView which loads data from the database using CursorLoader, CursorAdapter, ContentProvider and all that stuff. It is properly showing the data. But what I want is a search functionality where user types and the RecyclerView's data changes accordingly. I have done it before but with CursorLoader I'm not able to do.
I'm thinking of firing another Loader with different Id? Is it a good idea?
Here's the GitHub link of my project.

Related

How to get notified when child is added in Firebase adapter

So I was working on a project, something like social network and I came across the problem.
I have finished loading the list of data with Firebase UI component RecyclerView Adapter, it worked. For this I was using the docs, since I am new to Firebase. But every time someone adds a new post the database automatically refreshes itself and goes back to top.
Any idea if I could make the firebase adapter to notify me when the data changes? I am not so good with the adapter so I decided not to use custom, but to use already made adapter.
I am using Kotlin language.
Thank You!!

How to save listview in android and fetch it back when required?

I have a listview having three fields , but I don't know how to save my listview in android and fetch it back when user open app and display it.
I want to show the data saved in listview and I have to save the new data which user enter into my listview.
I would recommend having some sort of database on a server that would save that information and that you would need to pull that information from the database to display it in the app. However, android also has something called a sharedpref that allows you to save stuff in the app when the app is closed. The link is below. However this is not the best way a database would be, but it may work for your needs.
http://developer.android.com/reference/android/content/SharedPreferences.html
As per your requirment, there is no direct way to store list view, but you can store the data in the list view and you can set it in to the ListView later, for that you have to use Sqlite Database, refer official documentation of sqlite here, also check out this example.
read this example too (ListView of Data from SQLiteDatabase )
It depends on the purpose and the requirements of your app. If you are developing an offline app, then you should save the data entered in the listview.
Again, there are multiple ways to have the user enter the data. You could use a FAB(floating action bar) or have a text box in the last listview item.
Once the user has entered the data, you should save it to the sqlite database in a background thread and call notifyDatasetChanged() method on the listview adapter for the changes to reflect on the view.
If you have to send it across the network to store it in your server, you should do the networking on the background thread using a AysncTask or Handler thread model, or use libraries like volley or picasso to make it easy.
Hope this helped!
PS: Use ViewHolder pattern to improve performance in a ListView. Or there is RecyclerView from Android 5.0.

How could i manually update the recyclerview of my app?

I am new to Android development. I intended on users to submit their information via Google Docs until I created a website for my app. I currently have a recyclerview to display posts. These posts consist of imageviews and textviews. My question is what is the most efficient way I could update my recylerview to display their submitted information if approved. If not plausible what would you suggest to manage the content in my program? Also keep in my mind I plan on developing the same application in Ios in the future and would like for each app to display the same information.
You could represent the data fetched from your web service in an SQLite Database (a good tutorial on how to create/access sqlite databases in android can be found here:
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/).
Then you could use a CursorLoader and a CursorAdapter for the RecyclerView and swap the cursor when the loader finishes.
If you are modifying your dataset simply call onContentChanged() and your loader will requery and publish the results automatically.
Here you'll find a decent CursorAdapter implementation for RecyclerView: https://gist.github.com/skyfishjy/443b7448f59be978bc59
And here is a good documentation on how to use the CursorLoader with the LoaderCallbacks: https://developer.android.com/training/load-data-background/setup-loader.html

which approach is better to manipulate database in android

In my android app i am performing sql data extensively where i need to download the data from server , store it into database and populating it on the ListView.
moreover, i need to perform the database search and filter the data in a ListView too.
so far in my past projects i have used simple method to manipulate database like manually opening & closing database , getting data from database using cursor and storing it in a array list and populating over ListView.
after exploring i came across the ContentResolver and LoaderManager and tried few samples too but i am not able to decide what to use where i need to perform more database operation with search in a ListView.
I would even love to know in which scenario what approach is preferable.
i am waiting for commonsware comments too ;)
ContentResolver/ContentProvider is meant for providing data to other applications. If you aren't going to do that, then using it is clunky and overkill. It really doesn't provide any value, and even Google's own docs suggest not using it for inside of your app.
LoaderManager- meh. The idea of a Loader is useful, and you're probably already using it- a loader is just the idea of reading large amounts of data from your DB on an AsyncTask. LoaderManager will provide some efficiency gains if you're being restarted due to configuration changes, but there's other ways to achieve them. It isn't a bad thing to use, but it will save you minimal to no work- you still have to write a custom CursorLoader to make your db call, and you'll have some code to manage the loader manager. You aren't wrong to use it, I'm just not convinced of its value.

When to use multiple loaders in an activity or fragment?

I am trying to understand that what is loaders. Can anyone share an example with it? I don't know when we can use multiple loaders in an activity or fragment. I can't figure out one instance of multiple loaders to implement.
Loaders, while commonly used to populate lists, can be used for a whole host of things. Basically, anything you to do on a separate thread can be done in a loader. If you need to make multiple calls to the network and need to do different things when you get the results, that's when you'd use multiple loaders. You could also use one loader to populate a list with a cursor, and another loader to do network calls.
I don't know when we can use multiple loaders in an activity or
fragment. I can't figure out one instance of multiple loaders to
implement.
Here you go!
Let's suppose you are making a news app.
You have a ListView/RecyclerView on your Launcher Activity which displays the news. Each of your listItem has one ImageView to display thumbnail, two TextViews - one for news article title & another for news article category(Ex: Politics, Sports, Technology).
Now, in order to get news, you have to fetch data from a remote server(website) using their API. And when you fetch data, that website returns data in the form of JSON.
You have to connect to that website, get the JSON, parse that JSON (i.e. extract news article title, news article category, thumbnail_URL). Then you have to download thumbnails from extracted thumbnail_URL and bind data to your ListView/RecyclerView.
In this case, you could use one Loader for parsing JSON; use another Loader to download thumbnails from extracted thumbnail_URL.

Categories

Resources