1

I am developing an Android application. The content for the application is taken from the webserver using webservice. I am taking the data from webserver and store in mobile db and accessing across the application and its works fine but now problem is while taking more records from mobile db it tooks some time and it application hangs.

Is it possible to maintain the java collection such as hashmap or hashtable to maintain throughout the application until user logout the application and user can add or update the data in collection. If so please guide me.

Cœur
  • 37,241
  • 25
  • 195
  • 267
deepa
  • 2,496
  • 1
  • 26
  • 43

5 Answers5

1

you should use Application class that ships with Android API .

See this link..

http://www.xoriant.com/blog/mobile-application-development/android-application-class.html

Sujit
  • 10,512
  • 9
  • 40
  • 45
0

use a standalone class or static class to maintain run time static data you need to store.

jeet
  • 29,001
  • 6
  • 52
  • 53
  • by using standalone class, is it possible to update, delete the existing data in collection? can you please explain little more or some reference link – deepa Jan 10 '12 at 07:33
  • using this method you might get out of memory issue . – Code_Life Jan 10 '12 at 07:42
  • By Standalone you may define as many methods, which you need to manipulate data, or collections, for standalone search google, and you would get enough guide on this. – jeet Jan 10 '12 at 07:42
  • yes, it may result in outOfMemoryIssue, but this risk would exists everytime when you would want to maintain some data in run time memory. – jeet Jan 10 '12 at 07:45
0

Created a global singleton in the Application class and was able to access it from the activities I used.

You can pass data around in a Global Singleton if it is going to be used a lot.

public class YourApplication extends Application  {  

 public SomeDataClass data = new SomeDataClass(); 
// Your JAVA collections... 
} 

Then call it in any activity by:

YourApplication appState = ((YourApplication)this.getApplication());
appState.data.UseAGetterOrSetterHere(); // Do whatever you need to with the data here. 

For more info look at Android Application Class

user370305
  • 108,599
  • 23
  • 164
  • 151
0

Here are few possible solutions :

How to store hashmap so that it can be retained it value after a device reboot?

Saving a hash map into Shared Preferences

One is serialize/deserialize and other is using sharedpreference, as your requirement is to save/edit data until user logout.

Community
  • 1
  • 1
Sourab Sharma
  • 2,940
  • 1
  • 25
  • 38
0

First of all it's better to user db for storing purpose. you should look for optimizing you db querys and the they way you are using it. But anyways you can create a static hashmap or arraylist of your object in your first activity. Then you can use this collection object anywhere you wanted and perform any opration, it will be intect untill and unless you logout of application or any exception occures while working on the collection.

class MylunchedActivity extends Activty{

public static HashMap map = new HashMap(); }

MylunchedActivity should be your first activity. Then by using MylunchedActivity.map, you can work on you map.

i hope this will help.

Regards, Ravi

Ravi Bhojani
  • 1,032
  • 1
  • 13
  • 24