ANDROID-如何将ListView中的所有项目保存在文本文件、文本视图或类似文件中?
我有一个动态ListView,我通过单击另一个普通ListView中的项目不断向其中添加元素 后来我有一个按钮,我需要保存所有的ListView项目,当我点击这个按钮,因为后来我必须在另一个活动中显示这些项目,我不知道如何直接显示ListView 排列 文本文件 文本框 编辑文本 类似的地方 我的问题ANDROID-如何将ListView中的所有项目保存在文本文件、文本视图或类似文件中?,android,listview,android-listview,Android,Listview,Android Listview,我有一个动态ListView,我通过单击另一个普通ListView中的项目不断向其中添加元素 后来我有一个按钮,我需要保存所有的ListView项目,当我点击这个按钮,因为后来我必须在另一个活动中显示这些项目,我不知道如何直接显示ListView 排列 文本文件 文本框 编辑文本 类似的地方 我的问题 我怎么能做到呢?你可以这样做-- //your listview defined final ListView listview = (ListView) findViewById(R.id.li
我怎么能做到呢?你可以这样做--
//your listview defined
final ListView listview = (ListView) findViewById(R.id.listview);
//your listview data, you may have your own data in adapter
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
"Android", "iPhone", "WindowsMobile" };
//keep the data in arraylist and pass as Serializable
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
//onclick to pass to another activity
prev.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Current.this, Next.class);
intent.putExtra("data", new DataWrapper(list));
startActivity(intent);
}
});
保存数据arrayList的已实现类-
在另一个活动中检索-
您可以将列表文件以字符串形式保存在SharedReferences中,其中每个项目用逗号分隔 通过将列表保存到SharedReferences,即使应用程序在保存后关闭,然后在其他时间重新打开,您也可以使用它。但如果你不想这样做,就用@Ved Prakash提到的方法 用逗号分隔:
ArrayList<String> yourData =...
private void saveList(ArrayList<String> data)
{
String items = "";
for(String s : data)
items+= s+ ",";
SharedPreferences spf = getSharedPrefernces("myappname_data",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = spf.edit();
editor.putString("listview_data",items);
editor.commit();
}
然后,要取回您的数据:
如果不是永久性的,则可以将字符串[]或ArrayList传递给其他活动:
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("some_key", new String[] {});
intent.putStringArrayListExtra("some_another_key", new ArrayList<String>());
startActivity(intent);
在SharedReference中存储所需的字符串值,如下所示 将字符串除以;:
String list = "str1;str2;str3";
然后
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
Editor prefsEditor = prefs.edit();
prefsEditor.putString("YOURKEY", list);
prefsEditor.commit();
在其他活动中检索信息:
String list = prefs.getString("YOURKEY", "");
if(!list.equals(""))
String[] listArray = list.split(";");
如果你只需要显示它们一次,我会选择使用应用程序类存储ArrayList的路径,然后在其他活动中显示它们,如果我理解的话。那么你想知道在哪里存储它们,或者如何在另一个列表视图中存储和加载它们?@VyprNoch如何存储,我不在乎的地方…@xooloo是的,但我如何将每个项目保留在arraylist中?正如@xooloo所建议的,您可以使用arraylist,但当用户退出您的应用程序时,这些值将丢失。如果您希望在退出后仍保留值,请使用数据库或共享引用。我会尝试告诉您。可以通过Intent传递ArrayList,而无需包装:Intent.putStringArrayListExtrasome\u另一个\u键,new ArrayList@armansimonyan13-谢谢!我假设任何形式的复杂数据,而不仅仅是字符串。以便可以与序列化一起使用。更广义的范围。@VedPrakash-我明白了,在这种情况下,我建议使用Parcelable而不是Serializable:
String list = "str1;str2;str3";
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
Editor prefsEditor = prefs.edit();
prefsEditor.putString("YOURKEY", list);
prefsEditor.commit();
String list = prefs.getString("YOURKEY", "");
if(!list.equals(""))
String[] listArray = list.split(";");