简体   繁体   中英

How can I update my data into my activity?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM