首页 > 解决方案 > 保存新形式的自定义 ArrayList、SharedPreferences

问题描述

菜鸟在这里。我有一个带有问题和答案的自定义 ArrayAdapter。当一个列表项被长按时,它会被删除。现在我想用删除的项目保存 ArrayList 的新状态,所以如果你转到应用程序中的另一个选项卡然后返回,你将只有剩下的问题。我在网上查看并找到了一些看起来像我需要的东西,但我不明白为什么它不起作用。

public class OnePointQuestion extends AppCompatActivity {
ArrayList<Question> TheQuestion;
String key = "ABCD";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    if(TheQuestion == null){
        createList();
    }else{
        getArrayList(key);
    }

    final MyAdapter adapter = new MyAdapter(this, TheQuestion);
    ListView listView = findViewById(R.id.list);
    listView.setAdapter(adapter);

    //When the list item is clicked
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //Identify the current question
            Question currentQuestion = TheQuestion.get(position);

            //Send the answer to a second page to be shown
            Intent i = new Intent(OnePointQuestion.this, TheAnswer.class);
            i.putExtra("TheAnswer", currentQuestion.getAnswer());
            startActivity(i);
        }
    });

    //When the list item is long clicked
    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            //Identify the current question
            Question currentQuestion = TheQuestion.get(position);
            //Remove this element of the list
            TheQuestion.remove(currentQuestion);
            adapter.notifyDataSetChanged();
            Toast.makeText(OnePointQuestion.this, "Urmatoarea intrebare", Toast.LENGTH_SHORT).show();
            return true;
        }
    });
}

//Save the ArrayList
public void saveArrayList(ArrayList<Question> list, String key){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(OnePointQuestion.this);
    SharedPreferences.Editor editor = prefs.edit();
    Gson gson = new Gson();
    String json = gson.toJson(list);
    editor.putString(key, json);
    editor.apply();     // This line is IMPORTANT !!!
    Toast.makeText(OnePointQuestion.this, "Saved", Toast.LENGTH_SHORT).show();
}

//Load the ArrayList
public ArrayList<Question> getArrayList(String key){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(OnePointQuestion.this);
    Gson gson = new Gson();
    String json = prefs.getString(key, null);
    Type type = new TypeToken<ArrayList<Question>>() {}.getType();
    Toast.makeText(OnePointQuestion.this, "Loaded", Toast.LENGTH_SHORT).show();
    return gson.fromJson(json, type);
}

//Load the ArrayList when the app gets to the OnStart
@Override
protected void onStart() {
    super.onStart();
    getArrayList(key);
}


@Override
protected void onPause() {
    super.onPause();
    saveArrayList(TheQuestion, key);
}


public void createList(){
    //Create the list of questions
    TheQuestion = new ArrayList<>();
    TheQuestion.add(new Question("Cum te cheama?", "Irelevant"));
    TheQuestion.add(new Question("Cati ani ai?", "Prea multi"));
    TheQuestion.add(new Question("De ce ai dat la Poli", "Asta ma intreb si eu"));

}

}

我尝试使用的“保存”和“加载”方法已接近尾声。如果你能告诉我我做错了什么,我将不胜感激,谢谢:)

标签: javaandroidsharedpreferences

解决方案


移至getArrayList(key);onResume() 方法。

@Override
protected void onResume() {
    super.onResume();
    getArrayList(key);
}

查看生命周期:

在此处输入图像描述


推荐阅读