Access data from sqlite database - android

i have data stored in sqlite database, now it has to be fetched and display it on UI. To do this i have a cursoradapter.
is this the efficient way to display data?, do we need to use content providers & loaders for this purpose? what are there advantages & disadvantages ?

I usually dont use cursorAdapter instead i create a class "datasource" that handles all databases interactions: create update delete and read where read returns either a single or list of objects containing the data from the database. From there i use a custom adapter to show the data on a list or a gridview.
This tutorial shows how to make that database interaction class http://www.vogella.com/tutorials/AndroidSQLite/article.html
and this one show how to create a custom adapter for a listview
http://www.vogella.com/tutorials/AndroidListView/article.html

Related

How to put data to spinner on another layout?

How can I to put data to spinner which is on another layout?
I am newby on android, and now I have problem with spinners.
I have data at first class with his layout. And I want to put that data to other spinner which is on another layout. What I need to do? Create new Class or What?
Please help! :)
Data is- Array
You should put your data where it is accessible from everywhere.
so please put your data in database using SQLite or ORMs that help you work with Native database like SugarORM, ORMLite and Realm.
another way is to apply singleton pattern to store your data in a different class.
You said you have an Array
Then make Array static
eg:- public static ArrayList<> new_array = new ArrayList<>;
now call this array any where in the activity
you will get data
there is many to manage array data.
- create arraylist and use that data anywhere in the activity.
- using database also you can user.
- using Read & writing arrays of Parcelable objects
- using global application class ....etc

Best practice for loose coupling between data & UI in Android - Adapter, Filter, CursorLoader and ContentProvider

Assume we have an Activity with n TextViews that represent one line notes. These notes are stored somewhere (local database, network etc), and each time onResume() being called, the proper number of TextViews are drawn according to that stored data.
Now, lets say the user want to delete a note, what would be the best way the resolve the specific TextView, back to its storage entity?
At the moment, the only way I know is by using View.Tag, and having some manager to translate it to data entity, but it look rather messy.
Are there any other options?
In Android, the Adapter acts a bridge between the view and the data model. You could display the n TextViews in either a ListView or a GridView, and when the user adds or deletes a note, the local or server database is first updated. Upon completion of the web service call and/or the local database update, the new data is added to the underlying Adapter. The View is then refreshed by calling adapter.notifyDataSetChanged(). This would be the way to do it.
Approaches:
If updating the local SQLite database, you could consider using a
CursorAdpater
to hold the data for the View, as it directly maps the entries in
the local database to the View.
If making use of a ContentProvider, it is even possible to combine
a CursorAdapter with a
LoaderManager
and a
CursorLoader:
these plug into the Activity / Fragment life-cycle and monitor
the underlying ContentProvider for changes that are published
automatically to the View on a separate thread.
It is also possible to use a
Filter
in conjunction with the Adapter to define a dynamic mechanism that
sorts the data entries on-the-fly. The filtering is performed by the
Filter on a separate thread, as per a query entered by the user,
possibly in an
AutoCompleteTextView.
References:
See the Retrieving a List of
Contacts
tutorial. The example here retrieves a set of contacts from the
contacts ContentProvider based on a dynamic, alphabetical search by
the user. It makes use of CursorAdapter, CursorLoader and
LoaderManager to monitor and update the data, and it displays the
search results in a ListView.
See also the Android Realtime (Instant) Search with Filter Class example, which shows how a Filter is to be used.
Android AutoCompleteTextView with Custom Adapter filtering.
Android AutocompleteTextView using ArrayAdapter and Filter.

BaseAdapter vs CursorAdapter

I would like to know when to use each Adapter. According to my experience, and this article BaseAdapters are useful when I am getting data from an API for example, and I store it in a Collection object.
However a CursorAdapter is used to query contents from a database, phone agenda...In general, contents which have also a content provider to query the info out of them.
So basically a BaseAdapter is used to queries that dont have a content provider to access them, because in that case a CursorAdapter will be the best choice. Is that right?
BaseAdapter,ArrayAdapter,SimpleAdapter etc are mostly used if you are getting dynamic data from a remote connection (like a web service or API) and can be modified as your wish.
CursorAdapter is mostly used for local files or database to query the database and the content of it.
In your case CursorAdapter seems like the one to go.
#serdar explaination is almost right, if you are dealing with any database either your own or device(like contacts, sms etc) CursorAdapter is used, and if you want to make your custom list with Images and Textviews etc, then BaseAdapter is generally used. Any if your dealing with more complex custom listview like sorted contacted list along with seprater like A,B,C... then you have to use EfficientAdapter.

Need efficient data mappings example for a ListView drill down in Android/Java

I'm pretty new to java programming and am looking to do basic data mappings. I have a ListView object with a simple data array setup like this:
setListAdapter(new ArrayAdapter<String>(this, R.layout.my_list_xml, MY_DATA));
What I want to happen is when you click an item it goes to it's subcategory. I'm not worried about switching the data when an item is clicked. I'm just not sure how to create the data set in an efficient way using Android. Eventually this will be driven by a database but I want to figure out how to use mock-data to get this working first. An example of what I need the data structure will be is like:
myData = { 'category1' =>
{ 'subcategory1' => 'subcategorydetail1',
'subcategory2' => 'subcategorydetail2'},
'category2' =>
{ 'subcategory3' => 'subcategorydetail3',
'subcategory4' => 'subcategorydetail4'}
}
I'd like to be able to access it like myData['category2'] or myData['category2']['subcategory3']
Should I create this in xml files and link them up or is it best to create a new class structure for it?
Thanks for your help!
If eventually this app'll display data from a local SQLite database, then I think you'd be better off designing those DB schemas, filling the resulting tables up with mock data, and using that. The reason is, that you should write custom CursorAdapter classes for filling up your ListViews with data coming from the DB (from cursors), and those are a bit different than adapters extending BaseAdapter - which you'd have to use if you fill up those lists with common objects.
This way you might be able to evade the tedious task of mapping data from the DB to objects - tho that depends on what you'd like to do what that data.

Android: making a custom ListView independent of adapters?

I am adding a local database as a cache to a remote web service in my android application to answer queries. I used ArrayAdapters before for list views to display the results from the web service. Now with a database cache, the result could be either a Cursor(from database) or a List(from web), which means the adapter can be CursorAdapter or ArrayAdapter too. Creating two adapters for one query doesn't seem to be a good idea. So I am wondering what would be the best way to refactor my current code to add this database feature?
Thanks,
You should create a new class that extends from BaseAdapter and add that logic there.

Categories

Resources