I am trying to create a refesh function for my main page. I have searched many site but i can’t seem to find a (for me) acccesseble exaple. I am loading information from a sQLLite database. When i use my add activity and i return to the MainScreen activity the item i have added do not appear. How could i refresh this data the moment de activity is resumed.
Any help is welcome, thx in advance.
public ListView whiskeylist;
String[] DataArryWhiskey;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Start db view of whiskey
DBConfig info = new DBConfig(this);
info.open();
DataArryWhiskey = info.getDataInArray();
info.close();
whiskeylist = (ListView) findViewById(R.id.listofWhiskey);
whiskeylist.setOnItemClickListener(this);
whiskeylist.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, DataArryWhiskey));
}// end onCreate
On The advice of Adil i change the code to
public ListView whiskeylist;
String[] DataArryWhiskey;
ListAdapter Adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Start db view of whiskey
DBConfig info = new DBConfig(this);
info.open();
DataArryWhiskey = info.getDataInArray();
info.close();
whiskeylist = (ListView) findViewById(R.id.listofWhiskey);
Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, DataArryWhiskey);
whiskeylist.setAdapter(Adapter);
// End db view of whiskey
}// end onCreate
@Override
public void onResume()
{
super.onResume();
DBConfig info = new DBConfig(this);
info.open();
DataArryWhiskey = info.getDataInArray();
info.close();
Adapter.notifyDataSetChanged(); // refresh adapter
}
however i get a error on notifyDataSetChanged “the method notifyDataSetChanged is undefined for the type ListAdapter” <- fixed it by changing the ListAdapter to ArrayAdapter but the app still crashes.
//made changes in your oncreate method, see below
another method give below, call this method from on ActivityResult(), but before doing that ensure that
the field you added with another activity also saved into database.
call show Data() from onActivityResult method or from onResume()