【问题标题】:Store & retrieve a list of HashMap/Map objects to Local Storage将 HashMap/Map 对象列表存储和检索到本地存储
【发布时间】:2017-08-27 18:36:46
【问题描述】:

我对编码和移动开发非常陌生。我正在为待办事项编写一个移动应用程序

对于每个待办事项,我将其存储为Map,并将它们放入Arraylist myItems。

现在我想将 myItems 保存/检索到本地存储,因此每次重新打开文件时仍会保留以前的数据。有人告诉我可以保存到 JSON 文件,

我怎样才能做到这一点?提前致谢。

以下是我的 MainActivity 仅供参考的方法。

public class MainActivity extends AppCompatActivity {

//define variables
ListView listview;
ArrayList<Map<String,Object>> myItems=new   ArrayList<Map<String,Object>>();
SimpleAdapter adapter;
EditText addItemEditText;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //use "activity_main.xml" as the layout
    setContentView(R.layout.activity_main);

    listview = (ListView) findViewById(R.id.listview);

    //Create an adapter for the list view using Android's built-in item layout
     adapter = new SimpleAdapter(this,myItems,android.R.layout.simple_list_item_2,
            new String[]{"Name","Time"},new int[]{android.R.id.text1,android.R.id.text2});


    //connect the listview and the adapter
    listview.setAdapter(adapter);


    //Below two examples of how the item look like 
    Map<String,Object> item1 = new HashMap<String,Object>();
        item1.put("Name","Item1");
        item1.put("Time","Time1");
        myItems.add(item1);
        Map<String,Object> item2 = new HashMap<String,Object>();
        item2.put("Name","Item2");
        item2.put("Time",null);
        myItems.add(item2);

    //set up a list view listener
    setupListViewListener();

}

public void CreatNewActivity(View view) {

   ...
    }
...

【问题讨论】:

  • 这能回答你的问题吗? stackoverflow.com/questions/29648630/…
  • 是的,将复杂对象保存到本地存储的最简单、最快速的方法是以 json 格式将它们保存在 SharedPreferences 中。您需要使用 Gson 库将对象转换为 json 字符串,反之亦然。您可以使用this库来实现这种快速保存对象的方法。

标签: android json hashmap local-storage


【解决方案1】:

首先,您需要将您的ArrayList 序列化为 JSON。您可以使用 GSON 库来做到这一点。

ArrayList<String> yourArrayList = new ArrayList<String>();
String jsonStr = new Gson().toJson(yourArrayList);

现在你有了一个 JSON 字符串,你可以把它保存到内部存储中:

FileOutputStream outputStream;
String fileName = "jsonStorage.txt";
try {
  outputStream = openFileOutput(fileName , Context.MODE_PRIVATE);
  outputStream.write(jsonStr.getBytes());
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

In order to read the JSON and deserialize it, here are the steps:
Read the file:
        InputStream inputStream = context.openFileInput(fileName);

        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String jsonString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ( (stringBuilder = bufferedReader.readLine()) != null ) {
                stringBuilder.append(stringBuilder );
            }

            inputStream.close();
            ret = stringBuilder.toString();
        }
    }
    catch (Exception e) {
        Log.e(TAG, "File read error: " + e.toString());
}

要将 JSON 字符串反序列化为 ArrayList,您可以使用多个库,例如 org JSON libraryGSON。那么解析字符串就变得简单了:

JSONObject jsonObj = new JSONObject(stringBuilder.toString());

【讨论】:

  • 所以我应该在我的 MainActivity 页面的 onCreate 下添加这个?我做对了吗?
  • 抱歉,对应用程序开发很陌生,感觉迷茫;'(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-04
  • 2016-04-02
  • 2011-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-01
相关资源
最近更新 更多