【发布时间】:2017-08-21 17:03:30
【问题描述】:
我有 2 个更新列表视图的活动(一个是添加项目,一个是删除项目并显示列表)我想在应用关闭后存储列表(命名为计划),并在应用重新打开时从我的列表中恢复。
将项目添加到名为 Schedule 的列表中的活动
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(CloudEvents.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Save Event")
.setMessage("Do you want to save this event into your schedule?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(CloudEvents.this, "Saved", Toast.LENGTH_SHORT).show();
String itemAtPosition = parent.getItemAtPosition(position).toString();
Intent intent = new Intent(CloudEvents.this, MySchedule.class);
MySchedule.schedule.add(itemAtPosition);
startActivity(intent);
}
})
.setNegativeButton("No", null)
.show();
return true;
}
});
包含列表视图并在长按时具有删除选项的活动
public class MySchedule extends AppCompatActivity {
static ArrayList<String> schedule = new ArrayList<>() ;
ListView scheduleListView;
ArrayAdapter myArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_schedule);
setTitle("Schedule");
scheduleListView = (ListView) findViewById(R.id.scheduleListView);
myArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, schedule);
scheduleListView.setAdapter(myArrayAdapter);
scheduleListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(MySchedule.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure ?")
.setMessage("Do you want to delete this note")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
schedule.remove(position);
Toast.makeText(MySchedule.this,"Deleted",Toast.LENGTH_SHORT).show();
myArrayAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("No", null)
.show();
return true;
}
});
}
我尝试在 onPause 和 onResume 方法中使用共享首选项,但它不能正常工作并且没有更新超过 2 个项目的列表。我这样做了
@Override
protected void onPause() {
super.onPause();
sharedPreferences = getApplicationContext().getSharedPreferences("com.yatin.whatshappeningdtu", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(schedule);
editor.putString("Key", json);
editor.commit();
}
@Override
protected void onResume() {
super.onResume();
sharedPreferences = getApplicationContext().getSharedPreferences("com.yatin.whatshappeningdtu", Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("Key", null);
Type type = new TypeToken<ArrayList<String>>() {}.getType();
schedule = gson.fromJson(json, type);
}
Kimdly 建议一种方法来保存我的列表。谢谢!
【问题讨论】:
-
首先,我不建议将SharedPreferences用于大数据(它是键值)存储,其次,您可以在添加项目时直接保存到存储,从第一个活动开始,然后在当您删除iten(在第二个活动中)使用新列表更新存储文件时,第二个活动只需读取内容并填充列表。现在..你能在每行之后记录
json值吗:String json = sharedPreferences.getString("Key", null);和String json = gson.toJson(schedule);,看看两个日志中的数据是否正确? -
刚刚做了,只有空字符串打印在日志上,每当我删除一个项目时,应用程序就会崩溃。我如何继续直接存储?
-
您需要调试代码并查看发生了什么,直接存储我的意思是,而不是
MySchedule.schedule.add(itemAtPosition);从存储中获取数据,添加项目,然后将其保存回来。然后在其他活动中读取数据并填充列表,同样当您删除项目时,您需要更新存储的数据。
标签: android listview arraylist sharedpreferences