【问题标题】:How to store this list view?如何存储此列表视图?
【发布时间】: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


【解决方案1】:

当您重新加载 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);
    }

【讨论】:

  • 没用。它减少到为列表存储一项
【解决方案2】:

如果你只是要使用 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);

【讨论】:

  • 没用,它只为列表存储了一项
猜你喜欢
  • 2023-03-25
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
  • 2018-12-25
相关资源
最近更新 更多