如何保存ListView,以便在启动新应用程序时再次打开它?

如何保存ListView,以便在启动新应用程序时再次打开它?,listview,Listview,嗨,我真的是编程新手,所以很抱歉我犯了错误,我的问题是我想要一个动态列表视图。 到目前为止,我已经获得了代码,可以向列表视图添加新值。但如果我关闭应用程序并重新启动,它们就会消失 因此,现在我正在寻找一种可能性,将这个ListView保存在一个.txt文件中,我可以在每次应用程序启动时读取该文件。我发现: String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos = openFileO

嗨,我真的是编程新手,所以很抱歉我犯了错误,我的问题是我想要一个动态列表视图。 到目前为止,我已经获得了代码,可以向列表视图添加新值。但如果我关闭应用程序并重新启动,它们就会消失

因此,现在我正在寻找一种可能性,将这个ListView保存在一个.txt文件中,我可以在每次应用程序启动时读取该文件。我发现:

String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
但我不知道如何使用列表视图来实现这一点

也许你能给我一些帮助

背景


Fab

如果您了解SharedReferences,您可以将列表存储在SharedReferences中。使用Gson()将列表转换为json字符串,并将该json字符串存储在SharedReferences中。每次启动应用程序时,检查SharedReferences中是否有json字符串,如果有,则从该json字符串填充列表

检查下面的代码

创建一个单独的类来管理您的SharedReference,因为我在下面有一个类

public class PrefsManager {
            // final string below representing name of sharedPreferences
            private final String SHARED_PREFERENCES="com.example.myApp.sharedPreferences";

            private final String TASKS_LIST="tasks_list";
            private final String IS_TASKS_LIST_AVAILABLE="is_tasksList_available";

            private static PrefsManager prefsManager=null; //reference to PrefsManager object

            private Context context;
            private SharedPreferences sharedPreferences;
            private SharedPreferences.Editor editor;

            // constructor is private in order 
            //to make this class a Singleton 

            private PrefsManager(Context context){

           this.context=context;
 sharedPreferences=context.getSharedPreferences(SHARED_PREFERENCES,context.MODE_PRIVATE);
                editor=sharedPreferences.edit();
            }

            //public method used to make an object of PrefsManager class, 
           //and return reference of it.

            public static synchronized PrefsManager getPrefsManager(Context context){
                if(prefsManager==null){
                    prefsManager=new PrefsManager(context);
                }
                return prefsManager;
            }

        //public method which receive list of Task, 
        //convert it to Json String, and stores in sharedpreferences

        public void saveTasksList(List<Task> tasksList){
                if(tasksList.size()>0){
                    editor.putBoolean(IS_TASKS_LIST_AVAILABLE,true);
                    editor.putString(TASKS_LIST,new Gson().toJson(tasksList)); // if size > 0, save the list
                }
                else{
                    editor.putBoolean(IS_TASKS_LIST_AVAILABLE,false);
    editor.putString(TASKS_LIST,""); // else save empty string in sharedpreferences
                }
                editor.apply();
            }

            // check if list is saved in sharedpreferences or not and return true or false
            public boolean isTasksListAvailable(){
                return sharedPreferences.getBoolean(IS_TASKS_LIST_AVAILABLE,false); 
            }

            // public method which convert stored json string into
            //  ArrayList<Task> and return this list.

            public ArrayList<Task> getTasksList(){
                return new Gson().fromJson(sharedPreferences.getString(TASKS_LIST,""),
                        new TypeToken<ArrayList<Task>>(){}.getType());
            }
        }

谢谢,我试试这个。但首先我需要阅读共享偏好。谢谢你的快速回答。你可以浏览这些关于SharedReferences的教程。请点击以下链接
public class MyActivity extends Activity{
    private TodaysTasks_Adapter adapter;
    private List<Task> tasksList;
RecyclerView rvTodaysTask;
    private PrefsManager prefsManager;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activit_addnewtask);

        rvTodaysTask.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvTodaysTask.setHasFixedSize(true);
        tasksList=new ArrayList<>();
        prefsManager=PrefsManager.getPrefsManager(getActivity());
        if(prefsManager.isTasksListAvailable()){
            tasksList.addAll(prefsManager.getTasksList());
        }

        adapter=new TodaysTasks_Adapter(getActivity(), tasksList);
        rvTodaysTask.setAdapter(adapter);
}

    @Override
    public void onPause() {
        super.onPause();
        prefsManager.saveTasksList(tasksList);
    }

}// activity ends here
dependencies {
    compile 'com.google.code.gson:gson:2.3'
}