【问题标题】:show listview item after restarting application重新启动应用程序后显示列表视图项
【发布时间】:2016-08-09 10:03:16
【问题描述】:

我的应用中有两个编辑文本和一个按钮。一个用于身份证,另一个用于出生日期。用户可以输入 id & dob 然后点击添加按钮。

此 id 和出生日期必须存在于数据库中。如果用户单击添加按钮,用户的 id 和名称将显示在列表视图中。

在这里我可以轻松检索 json 数据并将其显示在列表视图中,但是当我关闭应用程序并重新启动应用程序时,这些添加的项目会被删除。

我该如何解决这个问题?假设在 facebook 中,如果我们在好友列表中添加好友,它在关闭应用程序后从未被删除。这是我的代码链接:

 code here: https://ideone.com/LM4HzE

【问题讨论】:

    标签: android arrays json listview


    【解决方案1】:
    1.Better to store the Data in SQlite Or Shared preference
     Retreived data should be stored in Shared preference or Sqlite the data will not be Erased otherwise the data will be erased,
    
    
        Store:
          SharedPreferences app_preferencesnew = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                SharedPreferences.Editor editor = app_preferencesnew.edit();
                editor.putBoolean("bfirsttime", true);
                 editor.putString("selected", selected);
                editor.commit();
    
         Retreive:
             String userId = app_preferencesnew.getString("selected", "");
    

    【讨论】:

    • 我已经尝试过共享偏好但不明白如何解决。这是我的代码:ideone.com/LM4HzE
    • put key value 应该是一样的,你放 id name value 的地方
    • studentID.setText(prefs.getString("autoSave1", "")); DOB.setText(prefs.getString("autoSave2", "")); editor.putString(autoSave1, id); editor.putString(autoSave2, dob);
    【解决方案2】:

    如果您想要更长时间的数据,有两种选择

    1. 共享偏好 (http://www.tutorialspoint.com/android/android_shared_preferences.htm)

    2. SQLite (http://www.tutorialspoint.com/sqlite/)

    首先将数据存储在 sqlite 或共享首选项中,然后从这里加载到列表视图中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 2012-10-28
      • 2014-11-21
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多