Usage of Android SimpleCursorAdapter and CursorLoader - android

I am new to Android and am trying to get my header round the SimpleCursorAdapter and CursorLoader classes. From my understanding, all of the examples that I have seen use these two classes to load data into a ListView in a background thread (to not block the UI).
I am OK with this and have used this general approach with my own list of items, however I now want to be able to click on an item in the list and get the full information for the item. Is it usual practice to use SimpleCursorAdapter and CursorLoader to retrieve the details for a single item? or are they just meant for lists?.
Thanks.

They are not meant for lists only. You can - and should - use them in detail views (activities) as well.
I've sketched a possible way to do so on my blog:
http://www.grokkingandroid.com/using-loaders-in-android/

Think of Adapters as a layer of abstraction between your data (Cursor) and whatever you attach that Adapter to (ListView for example). This way, you have a common interface between your data (Cursor, ArrayList, whatever) and the View you display that data on (ListView, TableView, etc.), this is helpful because if you later find that you want to access your data through an ArrayList rather than a Cursor, then you simply swap out the adapter with a different one and you're ready.
Now considering your question, Adapters give an abstract access to information, therefore you can "ask" it for what information is stored and where. You could attach an OnItemClickListener to your ListView and then access your data from there.

Related

ListView adapter to display certain fields

I have ListView that is populated like so:
ArrayList<MyFullDataClass> myFullDataClassList = Utilities.getDataFromSQLite(getActivity()); // populate list from SQLite
ArrayAdapter<MyFullDataClass> adapter = new ArrayAdapter<MyFullDataClass>(getActivity(), android.R.layout.simple_list_item_1, MyFullDataClassList);
setListAdapter(adapter);
MyFullDataClass contains many things: Name, address, phone, email, web site, etc. So as it is, each row of the list contains all of this information. It's a little busy. I would like to make it so that each row in the list contains only, say, name and email (and then touching the row would popup with all information in MyFullDataClass)
I could do this by creating a class, call it MyPartialDataClass, that contains fields for only name and email, then create an ArrayList<MyPartialDataClass> and copy data from myFullDataClassList to myPartialDataClassList and use this partial class for the adapter. (Then when a row is clicked, use myFullDataClassList.)
Not particularly elegant, but it would work.
Is there a better way?
I don't think there's a need to have two separate classes which ultimately hold the same information.
Having one class to hold necessary information is fine, unless you absolutely need minute efficiency gains of having two classes. What I would do is make your Adapter take a MyFullDataClass object list, but only populate the views with name and email.
From there, you can listen for an onClick event on your Adapter and pass the MyFullDataClass object associated with the clicked view to a fragment which will display the rest of the information associated with the MyFullDataClass object (i.e. the fragment will display address, phone, etc in addition to name and email).
You wouldn't make two separate tables in a database to hold parts of the same information. You would select what rows/properties from each entry that you need. The same concept applies here, IMO.

How to show data from database into a RecyclerView with high performance?

I wrote my own CursorAdapter for RecyclerView like following link: https://gist.github.com/skyfishjy/443b7448f59be978bc59
Then I found whenever I change something in database and want to show it in RecyclerView, I need to create a new Cursor by db.query() and use CursorAdpater's changeCursor(). Since query() will scan all rows in database, the RecyclerView will refresh slowly when data amount is big even I insert only one row into database.
Besides, as we all know, RecyclerView provides notifyItemInserted/Removed(position) for developers so that the RecyclerView can refresh partly, which is useful and beneficial to memory/time. However, when I use CursorAdapter, I don't know when and how I can use these methods because changing cursor isn't adding something directly to dataset binding with RecyclerView but refreshing all items in fact.
So are there any better ways to show data from database in RecyclerView and use RecyclerView's improving method to show variety of database?
I can tell you what i've done...
A. Loaded a cursor using Loader.
B. Copied the cursor into arraylist that is attached to the adapter (the cursor isnt attached to the adapter directly), close the cursor. Works well if there isnt a lot of data - if there is a lot rows then i would have load some of it to the arraylist and then when the user would scroll down i would query again and load from the last row of the array.
C. When the user would like to delete or add something i would do the operation on arrayList first (UI thread) notifiyItemChanged and then change the db (Back thread)
Hope i helped.

android clear arraylist or adapter?

After working on an app for a while I realize I use
adapter.clear()
and
arraylist.clear()
I can see both are working just fine, I would like to know the difference between the two!
Both are called before I start and asyncTask that updates my list with information from my server!
You should not be clearing the ArrayList directly. The ArrayAdapter makes absolutely no guarantees that it maintains the same referenced list given to it. In fact it will change when you perform a search with it's filter. Which would make arrayList.clear() fail.
Rule of thumb, if you ever need to mutate or retrieve the associating data...do it directly from the adapter. Not the list you used to construct it.
Adapter = it contains copies of diff views,arrays
aaraylist holds the data which we want to display in our view.
ex: arraylist<HashMap<String,String>> ah= new ArrayList<HashMap<String,String>>();
the above list contains hashmap
if i clear the arraylist there will be no data to show on listview or gridview so it will be empty
if i clear adapter than it will destroy the copies of array and views so the output will be same

How does an Android adapter decide what to re-render?

Say I have a List<User>. Now I can wrap this list in an ArrayAdapter.
List<User> users = Users.getAll();
ArrayAdapter<User> = new ArrayAdapter<User>(this, android.R.layout.simple_list_item_1, users);
I then bind the adapter to a listview to display the list of Users.
Users.getAll() uses Sugar ORM to query the database and return a list of users. Items can be added to the user list from the activity that displays the user list. I am wondering how do I keep the listview updated.
Option 1
One way is to manually update the users as a I add to the database and then call adapter.notifyDataSetChanged(). This works, but it doesn't feel right because I am maintaining a "fake" list that represents what is in the database.
Option 2
I am wondering how bad is it if I just clear the items in users, update it with the results of a new database query and then call adapter.notifyDataSetChanged()?
Will all the child views be thrown away and be re-rendered? Or does it call the equals() method to see if the models bound to each child is the same and then update only what is new?
Other Info
Since I am using SugarORM, I don't think I can get access to the Cursor to do something more efficient. However if there is a better way to keep the list synced with SugarORM, I am happy to hear that as well.
In answer to your option 2: No, it doesnt call equals, because the adapter works in conjunction with the widget to re-use the views, it doens't create a new view foreach item in the list, it create a view foreach visible item and as you scroll re-uses view that left the screen.
The best option here is to create your own adapter, creating a class extending BaseAdapter and creating your own logic inside it requerying the database and notifying the change to the listview (or gridview)..
On the other hand doing what you said here:
I am wondering how bad is it if I just clear the items in users, update it with the results of a new database query and then call adapter.notifyDataSetChanged()?
isn't bad either.
Create a DAO class that extends Observable, then have your Adapter implement Observer. Now every time you add or remove a SugarRecord, do through the DAO class and whoever is register as the Observer will get notified through the following method:
#Override
public void update(Observable observable, Object o)
You can more about Observable/Observer pattern here. This is just one of the many examples and tutorials out there.

Passing information between views

I am new to android programming, but I am trying to learn. I have written some code that takes in some parameters through a "normal" view with checkboxes and textviews. Then I use this information to generate a lot of numbers that I want to display in a listview. I have managed to create a listview when I press a run button, but how do I pass the information from the main view to the listview. Is it best to pass the information one number at the time or a large array with all the numbers. The list of numbers can be really large.
What you probably what to do is create an adapter with the numbers as the data source. If the numbers are in an array you can create a new ArrayAdapter and set the ListView adapter as that adapter:
ArrayAdapter adapter = new ArrayAdapter<Double>(getApplicationContext(), R.id.id_of_textbox, arrayOfDoubles);
listView.setAdapter(adapter);
In this code I've assumed the numbers are doubles, however ArrayAdapter is a generic class so it can be any object contained in the array. The array can also be presented as a List (like an ArrayList).
Hope that helps you out. Here are some bit of documentation to read and some good video sessions to watch:
ArrayAdapter
ListView.setAdapter()
The World of ListView Google I/O 2010 Session
How big is the array can get?
Most likely that displaying the list as another activity and passing the data as intent's extra will be the solution.

Categories

Resources