I'm studying and learning about Android Java world.
I'm building my first Android App, using Json to get all data from the application, and Java to display it. I created an activity with a Chat between two users, and I am using Asynctask to get and insert the Data.
But today I had an issue, because I have to update the listview everytime that a user inserts a new message, and Asynctask doesn't do that. I searched more about that, and some user said to me that I should use Cursor Loader instead the asynctask.
My questions are, whats the difference between both?
Do Cursor Loader reload my Listview automatically everytime that new Data is inserted into my Database?
Am i able to get the Json Data using Cursor Loader?
Thank you.
Unfortunately, telling you how a CursorLoader works in a simple answer would be confuse because it envolves a lot of code if you preetend to use Content Providers, but there is plenty of Google material explaining how it works!
As stated in Android Developers - CursorLoader Tutorial page:
A CursorLoader runs an asynchronous query in the background against a ContentProvider, and returns the results to the Activity or FragmentActivity from which it was called. This allows the Activity or FragmentActivity to continue to interact with the user while the query is ongoing.
Also, in the second part of that tutorial there is a section that answers your second question:
To display Cursor data returned by CursorLoader, use a View class that implements AdapterView and provide the view with an adapter that implements CursorAdapter. The system then automatically moves data from the Cursor to the view.
And for the third question: you can do anything with Java! :)
There is also a good tutorial on this available at Voggela: Android SQLite database and content provider.
Related
I trying to make the app similar to Nissan Leaf Spy. This app receives data from bluetooth interface ELM 327. My goal is to collect data like:
Speed
Temperature
Power
Battery capacity
And some more data
And display them on real time chart using GraphView.
For one parameter is one chart in Activity. So there are at least as many Activities as parameters I need to display. My guess is to use
Android Services
to do work in background to co collect and save every data in different array via bluetooth. Of course when I change Activity to see another Activity the one that works will stop working and there will be no more real time.
The question is: is there any kind of 'superclass' that is always working or do I need to save this data using SQL? Or should I just use intent.putExtraString(key,value) and getIntent().getStringExtra(key). I will be grateful for your help!
About having different activities for different parameters, you need to have just one activity. You can have a graph and different ArrayLists with adapters for parameters and then use one of them to feed the graph according to the parameter selected say, from a Spinner.
To feed those ArrayLists is just as easy. You can have a Service running, for general data collection, with an AsyncTask inside it, which will keep the feed live for a selected parameter when the app is active and not in the background. The Service, by itself, can collect data in some sort of a buffer large enough to feed those graphs.
Remember, AsyncTasks are good for updating UI components without blocking the main thread.
EDIT: Look, if you have an activity (let's consider some other activity than main) where you're going to show the data or graph, you can have AsyncTask running as soon as you enter the activity(you can define a default parameter for a graph to be shown) or when you select from a drop down, giving you real-time data while you're on the activity.
The reason I am using AsyncTask for the live feed is that you can have different UI views and seamlessly integrate without any future problems and that it'd modularize Service into functionalities for serving Activity and would end when you close the app. The Service running in the background would primarily provide to a temp log file or be an InputStream source for AsyncTask when it runs after app launch or activity launch.
I have a small problem to display data in my application. Actually for updating my data I wrote all the methods in the onCreate() method. So the problem is one all the data getting only the display will start.
But I need to show some data first after that I will update remaining data in the background.So please tell me where can I write the other methods.
code:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.summary_main);
displyProfile(personalDetailsInfo);
displyConditions();
displayAllergies();
displayWellness();
displayVaccine(vaccineHashMap);
}
First of all, creating an Android app that synchronizes with a Web service is not a simple task. You must take care of a lot of different things to do it properly (and it really depends on the application you are building and the nature of it). Sometimes you can use an AsyncTask to communicate with your webservice as #Roshni has said, but IMHO it's not the best option you have for this task (especially if you rotate your device ;D).
If you want a behaviour of Google+ you must keep in mind that it's using a lot of different components:
It uses a Rest Library (like Volley) to consume an API. There are a lot of examples in the web and it's very intuitive (1, 2, 3). Probably if your application is not very complicated you can use only this component and it will suffice.
It uses a SyncAdapter to synchronize its content with the API. Developers docs says this about this component:
The sync adapter component in your app encapsulates the code for the tasks that transfer data between the device and a server.
It stores its data in a SQL database using a ContentProvider:
Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process.
It uses a CursorLoader to query in background the data you need in your Activity/Fragment and when certain things happen, you can notify your observer to load more data. (This good tutorial explains how to work with CursorLoader written by Wolfram Rittmeyer).
There are a lot of very good Open Source applications to take a look on how are resolving this kind of issue like SeriesGuide, WordPress for Android, ioSched 2013.
Probably in your case if your data is simple you can use Volley and query the data you need, it will handle all asynchronous fetching for you and notify a corresponding listener, then in your Activity/Fragment you only have to update the related views.
I'm working on an APP containing quite a few listviews. What is the correct way to fill a listview? I would especially need it to be updated whenever I did an update or delete an entry. I am using a custom adapter extending the cursoradapter. I know about the following way: creating the Adapter and use setAdapter on the view. Then, one would need the onDataSetChanged of the adapter.
Should i implement it with an cursorloader? Not blocking the Main thread seem quite useful to me. But does this recognize a changed database by itself?
And: is this THE way to attach an Adapter to a listview (lets expand: to an adapterview).
I hope you know what I mean :-).
Cheers!
PS: Thanks to the whole stackoverflow community by having answered SO many questions concerning any type of programming!
There are a lot of ways to populate a ListView. For your case, if you want it to be handling stuff not in the Main UI thread, use a combination of SimpleCursorAdapter and AsyncTask. This will keep it in the background, and auto-update whenever entries are changed. Here is a tutorial using the two together that should get you on your way.
I'm a relatively experienced .NET/iOS developer taking first steps in Android development - to help me avoid going down the wrong path I'd be grateful for some guidance:
Essentially, I have an app that displays locations on a map/list. As the user scrolls around the map, the locations are fetched from a JSON web service. A location can be tapped, at which point another JSON web service is called to retrieve live information for that location. The live info is then displayed.
So, having read the various 'getting started' Android docs, I would imagine I need:
An Activity to display the main map view of the locations
A second Activity to display the list view? These seems odd since I get the impression that each Activity has to be an entire screen of the app. I'd like to persist the other UI elements. (e.g. button to switch views, button for settings etc) Is this possible?
A Service (or IntentService?) to retrieve the locations from the web. How should it let the Activity and ContentProvider know when new locations have been retrieved - via Broadcasts or should they bind to it?
A ContentProvider, to cache and persist my locations. Perhaps the content provider should broadcast to the activities when new data is available to display?
Your help would be very much appreciated, since I feel a little lost!
Carlos
PS: I'll be developing with Mono for Android, unless enough people advise against
An Activity to display the main map view of the locations
Yes
A second Activity to display the list view? These seems odd since I
get the impression that each Activity has to be an entire screen of
the app. I'd like to persist the other UI elements. (e.g. button to
switch views, button for settings etc) Is this possible?
Not necessarily so. Take a look at the Fragments API. It allows you to switch only parts of your UI. It was introduced in Android 3.0, but there exists an official backport of it, so that you can also use it in previous Android versions.
With it, you can put your buttons into the activity, the map in one fragment and the list in another, and then just switch the map with list while retaining the buttons.
A Service (or IntentService?) to retrieve the locations from the web.
How should it let the Activity and ContentProvider know when new
locations have been retrieved - via Broadcasts or should they bind to
it?
I would strongly advice against this. You should use a service if you have long-lasting downloads in the background, like downloading a file or something. Short term JSON requests can and should be handled in the UI process. Use AsyncTask or an Executor for that. There has been advice by Google to put almost all of your requests into a service, but believe me, it's bull.
A ContentProvider, to cache and persist my locations. Perhaps the
content provider should broadcast to the activities when new data is
available to display?
Not required. You only really need a ContentProvider if you plan to make your content accessible to other Applications or the System. For HTTP caching, you can directly access the database/filesystem, or better yet, use the Apache HTTP Client Cache. Works well if you use the already embedded Apache HTTP Client, which you should.
Points 1 and 2 : You could use Fragments to update part of the screen, Activity will act as a container for multiple fragments ( use compatibility library for back porting fragments to API level 10 or less
You should use AsyncTask instead of a service to get the locations from a remote web service
AsyncTask has a callback onPostExecute(..) which will be called on completion of remote fetch, this can be used to update List, Maps or Fragments
1 . You can use MapActivity for map view;
2 . use Activity and place listView to include button in a single view instead of ListActiviy
3 .please follow the link for location updator tutorial
http://www.vogella.de/articles/AndroidLocationAPI/article.html
4 . use map overlay technique for your message display
please make comments if u want any suggestions further after u go through it
I have a question about best practise.
I have an Activity which is a form where the user fills in some details. There is a tick box which, if ticked will run a Service which will attempt to get a location.
If the activity is close (onDestroy) I write to a static boolean in a static class that this has happened.
When my Service gets a location it checks this static variable to see if it should pass the location back to the Activity (via a Handled Message). This bit works fine.
If the Activity has been saved to my database I need to give my service the row id so that it can update the database entry rather than pass the location back to the activity.
I decided, based on my limited Java and Android knowledge, to use another static field in my helper class which I can set with the rowID in my Activity. I can then access this rowid in my Service and go and get the database entry and update it. I am pretty sure that this will work but I am not sure it is appropriate
Any tips and advice would be appreciated, although 'that's not a good way to do it' with no other information is useless advice :)
Regards
Stephen
This guy has written an amazing tutorial: http://mindtherobot.com/blog/675/android-architecture-message-based-mvc/#more-675