我有2个活动更新列表视图(一个是添加项目和一个删除项目并显示列表)我想存储列表(命名计划),即使应用程序关闭后,当应用程序重新打开时从我的列表中恢复。
将项目添加到名为Schedule
的List的活动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建议保存我的列表的方法。谢谢!
答案 0 :(得分:0)
当您重新加载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);
}
答案 1 :(得分:0)
如果您要使用String对象,可以保存以逗号分隔的列表项
PATHEXT
然后你只需要获取String并将其拆分:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < schedule.size(); i++) {
sb.append(schedule.get(i)).append(",");
}
editor.putString("Key", sb.toString());