简体   繁体   English

如何在每次创建活动时不创建列表

[英]How to not create a list every time the activity is created

My code does this - List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();我的代码是这样做的 - List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>(); every time the activity is created, but I want it to persist the list throughout the application life cycle, and even when the application is closed then re-opened... Any help would be appreciated.每次创建活动时,但我希望它在整个应用程序生命周期中保留列表,即使应用程序关闭然后重新打开......任何帮助将不胜感激。

You are going to need to save it somewhere, SQLite DB, TextFile, XML etc. Take a look at the android activity life-cycle http://developer.android.com/reference/android/app/Activity.html and decide where you want to save and where you want to load your list. You are going to need to save it somewhere, SQLite DB, TextFile, XML etc. Take a look at the android activity life-cycle http://developer.android.com/reference/android/app/Activity.html and decide where您要保存以及要加载列表的位置。

To clarify you will need to pull the values out of your HashMap and store them in a text format using one of the methods mentioned above.为了澄清,您需要从 HashMap 中提取值,并使用上述方法之一以文本格式存储它们。 Then when your app starts you have to get the values from storage and load them into the HashMap.然后,当您的应用程序启动时,您必须从存储中获取值并将它们加载到 HashMap 中。

Answer here?在这里回答?

Activity Result Fix 活动结果修复

UPDATE: 更新:

After that you will need to cache the information on the device and set it to refresh.之后,您需要在设备上缓存信息并将其设置为刷新。 in onClose() serialize the painItems ArrayList to a file using FileOutputStream fed by ObjectOutputStream to write your list out to a file.在 onClose() 中,使用 ObjectOutputStream 提供的 FileOutputStream 将 painItems ArrayList 序列化到文件中,以将您的列表写入文件。 Use ObjectInputStream from a FileInputStream to read it back in. I recommend doing this in onPostResume() and notify the empty initialized adapter of the data set change.使用 FileInputStream 中的 ObjectInputStream 将其读回。我建议在 onPostResume() 中执行此操作,并通知空的已初始化适配器数据集更改。

UPDATE 2: 更新 2:

The shamelessly terse Exception handling below needs to be thought out but this should get you started.需要考虑下面无耻简洁的异常处理,但这应该可以帮助您入门。

 String serfilename = "painitems.ser"; ... private ArrayList<HashMap<String, String>> loadListFromFile( ArrayList<HashMap<String, String>> masterlistrev) { try { FileInputStream fis = openFileInput(serfilename); ObjectInputStream ois = new ObjectInputStream(fis); masterlistrev = (ArrayList<HashMap<String, String>>) ois.readObject(); } catch (Exception e) { e.printStackTrace(); } return masterlistrev; } private void writeListToFile(ArrayList<HashMap<String, String>> masterlistrev){ File myfile = getFileStreamPath(serfilename); try { if(myfile.exists() || myfile.createNewFile()){ FileOutputStream fos = openFileOutput(serfilename, MODE_PRIVATE); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(masterlistrev); } } catch (Exception e) { e.printStackTrace(); } }

When the activity is closed you will probably be out of luck, if it was destroyed, but, otherwise make it part of the class level properties, and check if it exists before creating it again, but doing it in the onCreate method is probably your best bet, since that is only called once/activity.当活动关闭时,如果它被破坏,你可能会很不走运,但是,否则使它成为 class 级别属性的一部分,并在再次创建它之前检查它是否存在,但在onCreate方法中执行它可能是你的最好的选择,因为这只被称为一次/活动。

Now, if you have data there and you want that created only one time then you may want to look at something like SharedPreferences ( http://developer.android.com/guide/topics/data/data-storage.html ) so you can create the list, then just pull it in and display it from then on.现在,如果您在那里有数据并且您希望只创建一次,那么您可能需要查看SharedPreferences之类的内容( http://developer.android.com/guide/topics/data/data-storage.ZFC35FDC70D5FC67D236E )so youC822可以创建列表,然后将其拉入并从那时起显示它。

I did this, which is related to what you are asking.我这样做了,这与您的要求有关。 The nice part of final is that you only set it once, which is what you want. final的好处是你只设置一次,这就是你想要的。 ( http://www.javamex.com/tutorials/synchronization_final.shtml ) http://www.javamex.com/tutorials/synchronization_final.shtml

public class StartGameActivity extends GraphicsActivity implements
    final List<PaintColorCanvas> pccList = new ArrayList<PaintColorCanvas>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM