首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何存储此列表视图?

如何存储此列表视图?
EN

Stack Overflow用户
提问于 2017-03-28 22:43:57
回答 2查看 654关注 0票数 0

我有两个更新列表视图的活动(一个是添加项目,另一个是删除项目并显示列表)我想要存储列表(名为schedule),即使在应用程序关闭后,并在应用程序重新打开时从我的列表恢复。

将项目添加到指定计划列表中的活动

代码语言:javascript
复制
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;
        }
    });

包含列表视图并在长按下时具有删除选项的活动

代码语言:javascript
复制
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个项目的列表。

代码语言:javascript
复制
  @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你!

EN

回答 2

Stack Overflow用户

发布于 2017-03-28 23:03:59

在重新加载this.schedule时,适配器this.myArrayAdapter仍然使用旧内容。尝试重新加载适配器

代码语言:javascript
复制
    @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);
    }
票数 0
EN

Stack Overflow用户

发布于 2017-03-28 23:43:06

如果您只打算使用String对象,则可以保存以逗号分隔的列表项

代码语言:javascript
复制
StringBuilder sb = new StringBuilder();
for (int i = 0; i < schedule.size(); i++) {
   sb.append(schedule.get(i)).append(",");
}
editor.putString("Key", sb.toString());

然后,您只需获取字符串并将其拆分:

代码语言:javascript
复制
String[] scheduleArray=  sharedPreferences.getString("Key", "").split(",");
schedule = Arrays.asList(scheduleArray);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43072667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档