I'm kinda new in android development and I am little confused about content providers. Content providers are used to share your data with other application legally, but is it correct to use it for retrieving your data from SQLite databaseand display it in, for instance listView? What is better solution for displaying data from db?
Thanks
Yes it is legit using the ContentProvider to retrieve data from a SQLite database while its perfectly fine creating an Handler to handle the SQLiteOpenHelper and just using it.
Its all depends on your app's needs,
Me individually for my own app if no need for sharing data wouldn't use a content provider.
if you don't need to share the data base from your app it's not necessary to use a Content Provider
Related
I am trying to make a tutorial app with the help of Android, but I am confused where I should put the whole content (topic details). Actually I want to use it with the help of SQLite database. I used hashmap in my app.
if you really want to store your data on app, then SQL is best option. Otherwise you can use Remote server to store data. and SQLite for offline access to already viewed data.
Is it possible to create a content provider for a different database like MySQL or SQL. How can I achieve this? Any suggestions?
ContentProvider can be applied to any kind of data even not SQL one. If you know how to get connection to MySQL (not easy task) - you can easily implement your own ContentProvider over MySQL data - just create your own class extending ContentProvider - and you're there.
This is not possible to use android content provider for different database.Please have a look at Does the content provider allow for MySql synchronization?. As Android doesn't currently include support for automated MySql server synchronization.
The Alternate Option
Content provider can be applied to any kind of data even not SQL one. If you know how to get connection to MySQL (not easy task) - you can easily implement your own content provider over MySQL data - just create your own class extending content provider - and you're there.
But As currently Android content provider or resources are not available to connect or create content provider which can be able to server synch.
If you want to expose the data to your another app you can use content provider. Otherwise it is not needed.
Unfortunately, Android doesn't have too much support for MySQL unless you have a web server. As far as SQL Server, there may be some classes that are available for you to use but many of them are targeted at ASP.NET and Microsoft-based platforms. This page practically says it all.
Well, as far as I kwnow a content provider is a database, and is used to pass data through apps.
But this data can be accessed only in the phone that it was saved? Or if I saved some data using the app with phone1, can I retrieve it using the app with phone 2?
If it can't do the second option, what could I use to do it?
Thanks.
Content provider is not a database. You can think this as a layer between your app and underlying data. The data can be in sqlite database, file or something else. Content provider is very useful to access and store your data to database or file. You need this when you use syncadapter or widgets in your app. Now to answer your question, content provider of your app can be accessed by a different app in your same phone provided you use correct permission. As I mentioned it's not a database, so answer of second question is not. I would recommend you to read about content provider on android developer site to get the basic concepts.
A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network.
I am creating app that should have offline mode, so previously downloaded data should stored somewhere, the most common way is to store data in SQLite database.
Mostly SQLite database is used with Content Provider in android. I have clear understanding what is the purpose of content provider (to share data between different apps), but in my case application will never need to share the data with other apps in the system.
Content provider has the similar interface as HTTP request (GET,POST,PUT,DELETE).
My idea is to create facade class which can be used like this getAllLatestNews(); firstly it will try to get latest data from the internet, if it fails - data from database will be used and if request is successful it also will save retrieved data to the database. This class will be facade for separating different layers of application (not to make requests from activities directly).
But now I am a little bit puzzled deciding whenever I need Content Provider or not. I can use SQLiteOpenHelper classes to retrieve and save data to the database or even use ORM library to do this.
At first I wanted to implement REST API Pattern B by Virgil Dobjanschi. But now I am not sure about this, maybe it would be better to create facade for Robospice(in my case, network request in the service) requests and do persistence there ?
Please share you thoughts about this topic, I would be grateful for any help.
EDIT
I asked this question because I feel that it is not good practice to make requests directly from activities even if they are made in service under the hood, I want to separate different layers of my application in order to make it more flexible and maintainable.
As you don't intend to share your data, i would say that implementing a ContentProvider is overkill.
Personally im a huge fan of ORM libraries (Currently i use SugarOrm in several projects), so i would go down that road.
Then at app startup, you check whether or not you have an active internet connection, and based on that you either get the latest information online, or retrieve older information from the database.
To seperate the logic a bit, i would most likely implement the getting of online information in a service, which would then store it in the database and broadcast to the activity that the information is now available, and then the activity could retrieve the information from the newly updated database.
Content Providers are absolutely for sharing data between applications and are of no use without this purpose.
If you want to use those data only in your app privately, you could use SQLite databases. Also there other objects available:
Shared Preferences
Files
Content provider has the similar interface as HTTP request (GET,POST,PUT,DELETE)
I don't think so. It's more like to SQL language.
A content provider is only required if you need to share data between multiple applications.
Is this the only purpose of using it? What about simply getting data from the WS and writing it in the CP and then reading from here in Activities?
What about iosched:
CP is used here to share a database for several apps?
you just can achive that with a database, but I strong recommend the use of contentprovider even if you are not going to share data between aplications. classes such as CursorLoader (correction) load directly datas from contentprovider, if you use just a database you have to extend this class to get the same performance
so to conclude it's all made for working with contentprovider.