开发者

How to update current tab activity after finish processing HttpGet query in first tab activity?

I have two tabs application. In first tab I perform an http search query, dysplaying the results and inserting them into database. Second tab shows the data from database. If user perform a search in the first tab and navigates to the second tab before the search is finished, after search is finished the data in second tab must be updated.

I imagine that the first tab after finishing the search must tell to the second tab about it and in 开发者_C百科the second tab I should start new Intent. But I don't know how to do this. Please help.


There are various ways you could approach this, and since I've only got the above details to go by, this suggestion may or may not be the optimal solution, but here goes:

The way I see it, we have the following tasks to address

  1. You want to make an HTTP call and then update the UI, being tab 1
  2. You want to query an SQLite instance and display data on the UI, being tab 2
  3. Anytime a search is invoked in tab 1, on completion it must update tab 2

Let me comment on each of the 3 tasks you need to do

You want to make an HTTP call and then update the UI, being tab 1

Since you're going to be making external calls here, that could take time, you're best bet is to use an AsyncTask. If you try and do an HTTP call on the main application thread, and it takes too long, your application will crash, therefore use an AsyncTask to do the call, and update the UI as necessary.

You could invoke the AsyncTask in the onResume() of your tab 1 activity, or hook it onto a button, whichever suits you best.

This might help you :

private class CallHTTP extends AsyncTask<String, Integer, Bitmap>
    {
        @Override
        protected void onPreExecute()
        {
            // initialise any UI stuff here
        }

        @Override
        protected Bitmap doInBackground(String... strings)
        {
            //actually make the HTTP call here
        }

        protected void onPostExecute(final Bitmap result)
        {
            //when you're done, update the UI here or fire off another activity
        }
    }

In the onPostExecute(), you could create an intent for your second tab activity, maybe something like

Intent i = new Intent(this, MyActivity.class);
            startActivity(i);

You want to query an SQLite instance and display data on the UI, being tab 2

Pretty much the same as above here, you'll want an AsyncTask to do the heavy(ish, in relative terms) work of querying the database, use the above strategy and update UI when you're done. This might help you get started on the SQLite side.

Any time a search is invoked in tab 1, on completion it must update tab 2

If you follow the advice I gave above, you should implicitly get updates on tab 2 whenever the tab 1 search completes, since its onPostExecute() will start the activity, forcing it to hit the onResume() where you can reset the content view.

I hope that has provided you with enough info to get you started

Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜