Reputation: 721
i have an application made with tab host. i need to refresh my listview (the second tab) when clicked. this view contain the db data, it's obvious that it need to be always up to date (because in the entire app i make update,insert,delete,... how can i?
this is the onCreate in the class called by the tab
super.onCreate(savedInstanceState);
setContentView(R.layout.bookmarks);
final Database info=new Database(this);
final Timer t = new Timer();
info.open();
final Cursor data=info.getData();
final ListView listView1 = (ListView) findViewById(R.id.list_mia);
String[] from = new String[] {"contenuto"};
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1, data, from, to);
listView1.setAdapter(adapter);
and this is the tab class (the "incriminated class is bookmarks)
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
TabHost tabHost = getTabHost();
// Tab per lettore
TabSpec lettoretab = tabHost.newTabSpec("Lettore");
lettoretab.setIndicator("Lettore", getResources().getDrawable(R.drawable.lettore));
Intent lettoreIntent = new Intent(this, Lettore.class);
lettoretab.setContent(lettoreIntent);
// Tab per bookmarks
TabSpec bookmarkstab = tabHost.newTabSpec("Bookmarks");
bookmarkstab.setIndicator("Bookmarks", getResources().getDrawable(R.drawable.bookmarks));
Intent bookmarksIntent = new Intent(this, Bookmarks.class);
bookmarkstab.setContent(bookmarksIntent);
// Tab per create
TabSpec createtabs = tabHost.newTabSpec("Create");
createtabs.setIndicator("Create", getResources().getDrawable(R.drawable.create));
Intent createIntent = new Intent(this, Create.class);
createtabs.setContent(createIntent);
// Tab per feed
TabSpec feedtabs = tabHost.newTabSpec("Feed");
feedtabs.setIndicator("Feed", getResources().getDrawable(R.drawable.feed));
Intent feedIntent = new Intent(this, Feeds.class);
feedtabs.setContent(feedIntent);
// Tab per actionmobileapp
TabSpec actionmobileapptabs = tabHost.newTabSpec("App");
actionmobileapptabs.setIndicator("App", getResources().getDrawable(R.drawable.actionmobileapp));
Intent actionmobileappIntent = new Intent(this, Actionmobileapp.class);
actionmobileapptabs.setContent(actionmobileappIntent);
// Aggiungo i tabs
tabHost.addTab(lettoretab);
tabHost.addTab(bookmarkstab);
tabHost.addTab(createtabs);
tabHost.addTab(feedtabs);
tabHost.addTab(actionmobileapptabs);
i've tried recalling the code in onResume but no success...
Upvotes: 0
Views: 1792
Reputation: 12642
make 'adpater' variable public in 'Activity' and in onResume
call this mettadapter.swapCursor(c)
here c
is your new-updated cursor. don't forget to call notifyDataSetChanged()
after it.
Upvotes: 0
Reputation: 592
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(
R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
add this in TabActivity class inside the addTab().
Thanks....
Upvotes: 1
Reputation: 22246
This is likely a duplicate of this:
SimpleCursorAdapter not Updating with DB Changes
You should check the Android documentation for LoaderManager which is what you want to use.
LoaderManager Class Reference with Code Example
Upvotes: 0