使用共享首选项保存和重新加载 ListView [保存 onDestroy()] [英] Saving and Reloading ListView using Shared Preferences [Saving onDestroy()]

查看:15
本文介绍了使用共享首选项保存和重新加载 ListView [保存 onDestroy()]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个待办事项列表.现在我的应用程序在 ListView 中添加和删除数据.但是,当我关闭应用程序时,数据会被删除.我想使用 SharedPreferences 来保存 onDestroy() 的数据,然后当我的应用程序打开时,我希望重新加载数据.

I am creating a To Do List. Right now my app adds and removes data to and from a ListView. However, when I close my application the data gets erased. I want to use SharedPreferences to save the data onDestroy(), then when my app opens I wanted the data to reload.

但是,我不知道如何去完成这个任务.任何人都可以帮助我使用共享首选项保存 ListView(也就是我在寻找代码)?

However, I don't know how to go about accomplishing this task. Can anybody help me with saving a ListView using Shared Preferences (aka I am looking for code)?

只有一个共享首选项的教程我希望在我的应用程序关闭时动态添加多个字符串.然后当我重新打开它时,数据就会出现.我只使用 ONE ACTIVITY 页面,一切都将发生在一页上.

There are tutorials for just one shared preference I am looking to dynamically add multiple strings when my application closes. Then when I reopen it, the data will appear. I am only using ONE ACTIVITY page, everything will occur on one page.

这是我的代码:

public class Main_ToDoList extends Activity implements OnClickListener
{
private Button btnAdd;
private EditText et;
private ListView lv;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
final Context context = this;

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

    btnAdd = (Button)findViewById(R.id.addTaskBtn);
    btnAdd.setOnClickListener(this);
    et = (EditText)findViewById(R.id.editText);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);

    // set the lv variable to your list in the xml
    lv=(ListView)findViewById(R.id.list);
    lv.setAdapter(adapter);

    // set ListView item listener
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id)
        {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
            alertDialogBuilder.setTitle("Confirm Delete");
            alertDialogBuilder.setMessage("Sure you want to delete?");
            alertDialogBuilder.setCancelable(false);
            alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialogInterface, int i)
                {
                    adapter.remove(adapter.getItem(position));
                }
            });
            alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialogInterface, int i)
                {
                    dialogInterface.cancel();
                }
            });
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }
    });
}
public void onClick(View v)
{
    String input = et.getText().toString();
    if(input.length() > 0)
    {
        adapter.add(input);
        et.setText("");
    }
}
@Override
public void onDestroy()
{
    super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main__to_do_list, menu);
    return true;
}
} 

推荐答案

Android SharedPreferences 用于存储单个键值对数据.换句话说,它不是用来存储重复和复杂的数据.在您的情况下,列表视图可能包含 100 行或更多行,具体取决于您的应用.如果您要为每一行创建一个 SharedPreferences 对象,它将是 100+,这根本没有效率.对此的解决方案如上所述,是使用 Android 的 SQLite 数据库.

Android SharedPreferences is used to store a single key-value paired data. In other words, it is not made to store repetitive and complex data. In your case, a list view may contain than 100 rows or more depends on your app. If you were to create a SharedPreferences object for each row it would be 100+ of it, which is not efficient at all. The solution for this is as suggested above, is to use the Android's SQLite database.

一个很好的教程:http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

这篇关于使用共享首选项保存和重新加载 ListView [保存 onDestroy()]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆