Java 重新启动应用程序后,不会保存ListView中的更新
我的申请由两个活动组成。 第一个包含一个列表,从中我将行逐个发送到第二个活动: 第二个活动(我添加了一些行): 当我关闭应用程序并再次启动它时,我的问题就会出现,在第二个活动中,我只收到第一个活动中发送的最后一行: 我还尝试创建另一个类来保存填充listview的arraylist,但我想它似乎不起作用 ~第一项活动~Java 重新启动应用程序后,不会保存ListView中的更新,java,android,arrays,listview,onresume,Java,Android,Arrays,Listview,Onresume,我的申请由两个活动组成。 第一个包含一个列表,从中我将行逐个发送到第二个活动: 第二个活动(我添加了一些行): 当我关闭应用程序并再次启动它时,我的问题就会出现,在第二个活动中,我只收到第一个活动中发送的最后一行: 我还尝试创建另一个类来保存填充listview的arraylist,但我想它似乎不起作用 ~第一项活动~ @Override public boolean onItemLongClick(AdapterView<?> arg0, View view,
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view,
int position, long arg3) {
SharedPreferences sp = context.getSharedPreferences("Save",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
textView4 = (TextView)view.findViewById(R.id.titleTextView);
textView5 = (TextView)view.findViewById(R.id.userNameTextView);
n =textView4.getText().toString();
m=textView5.getText().toString();
MyObject=new CustomObject(n,m);
Gson gson = new Gson();
String json = gson.toJson(MyObject);
editor.putString("MyObject", json);
editor.apply();
@覆盖
长单击(AdapterView arg0,视图,
整数位置,长arg3){
SharedReferences sp=context.getSharedReferences(“保存”,
上下文。模式(私人);
SharedReferences.Editor=sp.edit();
textView4=(TextView)view.findViewById(R.id.titleTextView);
textView5=(TextView)view.findViewById(R.id.userNameTextView);
n=textView4.getText().toString();
m=textView5.getText().toString();
MyObject=新的CustomObject(n,m);
Gson Gson=新的Gson();
字符串json=gson.toJson(MyObject);
putString(“MyObject”,json);
editor.apply();
~第二项活动~
public class Favorites extends Activity{
private ListView lv;
private String n, m;
private CustomObject obj;
private SaveData save;
SharedPreferences sharedpreferences;
List<CustomObject> favorites=new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorites);
lv = (ListView) findViewById(R.id.forthlistview);
sharedpreferences = getSharedPreferences("Save",
Context.MODE_PRIVATE);
}
private void fillFavoriteList() {
Gson gson = new Gson();
String json = sharedpreferences.getString("MyObject", "");
obj = gson.fromJson(json, CustomObject.class);
favorites.add(obj);
HashSet<CustomObject> hs = new HashSet<CustomObject>();
hs.addAll(favorites);
favorites.clear();
favorites.addAll(hs);
save.setGlobalArrayOfCustomObjects(favorites);
adapter = new AdapterFavorites(Favorites.this, favorites);
lv.setAdapter(adapter);
}
protected void onResume() {
super.onResume();
if(favorites==null)
{favorites=save.getGlobalArrayOfCustomObjects(); fillFavoriteList();}
else fillFavoriteList();
}
}
公共类收藏夹扩展活动{
私有ListView lv;
私有字符串n,m;
私有对象对象;
私有存储数据存储;
SharedReferences SharedReferences;
列表收藏夹=新建ArrayList();
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_收藏夹);
lv=(ListView)findViewById(R.id.forthlistview);
SharedReferences=GetSharedReferences(“保存”,
上下文。模式(私人);
}
私有void fillFavoriteList(){
Gson Gson=新的Gson();
String json=SharedReferences.getString(“MyObject”,即“”);
obj=gson.fromJson(json,CustomObject.class);
收藏夹。添加(obj);
HashSet hs=新的HashSet();
hs.addAll(收藏夹);
收藏夹。清除();
收藏夹.addAll(hs);
save.setGlobalArrayOfCustomObjects(收藏夹);
适配器=新适配器收藏夹(Favorites.this,Favorites);
低压设置适配器(适配器);
}
受保护的void onResume(){
super.onResume();
如果(收藏夹==null)
{favorites=save.getGlobalArrayOfCustomObjects();fillFavoriteList();}
else-fillFavoriteList();
}
}
~SaveData类~
public class SaveData extends Favorites {
private List<CustomObject> globalArray;
public void setGlobalArrayOfCustomObjects(List<CustomObject> newArray){
globalArray = newArray;
}
public List<CustomObject> getGlobalArrayOfCustomObjects(){
return globalArray;
}
}
public类SaveData扩展了收藏夹{
私人名单全球阵列;
public void setGlobalArrayOfCustomObjects(列表新数组){
globalArray=newArray;
}
公共列表getGlobalArrayOfCustomObjects(){
返回全球阵列;
}
}