0

Hi I have an application which after login, loads a main activity which contains a tabhost with 3 tabs containing listviews. Each tab is populated via sqlite + custom adapter. The database is initially populated via xml data (sync'd asynchronously to the sqlite database).

My problem: I want to allow allow manual syncing of the sqlite database with XML data, which then updates the current tab listview. I have the asynchronous thread working fine to do the background update of the database. What i can't figure out is how to force a refresh of the current tab's listview. The problem is i don't know how to access a sub activity within the tabhost from the parent activity.

I'm fairly sure notifyDataSetChanged() is what i need but i can't figure out how to access the tab's activity > adapter to do this. How do i access a sub activities adapter in order to call notifyDataSetChanged()? The parent (containing) activity has the button which will be used to refresh the tabhost's listview.

An alternative: i could try is broadcasting an intent from my background thread's onPostExecute() callback. ie, when it finishes updating the database with xml data i have my tab receive the broadcast and trigger it to refresh via notifyDataSetChanged()... but this is probably NOT how broadcast/receive intents are designed is that right?? ie, communicating between a activity and contained tab's listview activity?

This is my 2nd post on stack overflow I hope I've been clear in my explanation. Thanks for any help or advice you can provide

EDIT (16-Jan-2012): Been a long time since I worked on this issue. I originally used a solution of broadcasting an intent and consuming it in the child activity. BUT I've since taken the advice from many others (and you should too)! and instead stopped using Activities inside tabs. I'm now using views within the tabs. This works MUCH better and cleaner.

Apart from the code getting too cumbersome, I was also getting weird "Database not closed" type errors when going between Activities also when rotating the devide from portrait/landscape. Views within tabs instead of activities fixed many issues and I'm sure its better on resource usage.

wired00
  • 13,930
  • 7
  • 70
  • 73

2 Answers2

2

I just finished implementing "a" solution. I basically went with the broadcast intent alternative i mentioned above.

Incase anyone else wants to do it this way, in the onPostExecute() method of my AsyncTask task (which does the background syncing of my XML with database). I raise a Intent Broadcast with :

    @Override
    protected void onPostExecute(Integer result) {
        Intent i = new Intent("com.myapp.app.DATA_REFRESH");
        sendBroadcast(i);

    }       

Then within the tabhost > tab activity i receive that broadcasted intent with :

public class updaterBroadcastReceiver extends BroadcastReceiver {       
        @Override
        public void onReceive(Context context, Intent intent) {
            fillData(); // rebuild my listview with new database data
        }
}

Register / setup the above intent receiver with :

@Override
public void onResume(){         
         IntentFilter filter = new IntentFilter("com.myapp.app.DATA_REFRESH");
         updaterBroadcastReceiver r = new updaterBroadcastReceiver();
         registerReceiver(r,filter);

         super.onResume();
}   

FillData() is used to update the listview with new data from the database.

Well this seems to work for me if anyone has a suggestion on a better way to do this please let me know. I'm quite new to android.

Cheers, Rob

wired00
  • 13,930
  • 7
  • 70
  • 73
1

Have a look at this thread.

I hade a similar problem where I wanted to refresh a tab on an event of the TabHost. I defined an Interface which all my Sub(Tab)Activities implemented and invoked the message for the Sub(Tab)Activity from the TabHost (have a look at the posted onTouch() method).

Community
  • 1
  • 1
FrVaBe
  • 47,963
  • 16
  • 124
  • 157
  • Thanks KClaszen, That looks good how you get the context of the sub tab activity. I could try changing mine to do it that way (ie `View currentView = getTabHost().getCurrentView(); Context currentViewContext = currentView.getContext();` Do you think there is any disadvantage to doing things using a intent broadcast / receive implementation like i have vs this? I would mark you up but not enough reputation :) – wired00 Apr 07 '11 at 21:46
  • @wired00 Unfortunately I have not used intent broadcast yet and can not comment on this solution. – FrVaBe Apr 08 '11 at 09:48