我有两个更新列表视图的活动(一个是添加项目,另一个是删除项目并显示列表)我想要存储列表(名为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);
}金迪建议一个方法来拯救我的list.Thank你!
发布于 2017-03-28 23:03:59
在重新加载this.schedule时,适配器this.myArrayAdapter仍然使用旧内容。尝试重新加载适配器
@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);
myArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, schedule);
scheduleListView.setAdapter(myArrayAdapter);
}发布于 2017-03-28 23:43:06
如果您只打算使用String对象,则可以保存以逗号分隔的列表项
StringBuilder sb = new StringBuilder();
for (int i = 0; i < schedule.size(); i++) {
sb.append(schedule.get(i)).append(",");
}
editor.putString("Key", sb.toString());然后,您只需获取字符串并将其拆分:
String[] scheduleArray= sharedPreferences.getString("Key", "").split(",");
schedule = Arrays.asList(scheduleArray);https://stackoverflow.com/questions/43072667
复制相似问题