gpt4 book ai didi

java - 离开 android 中的 Activity 后如何保留 ListView ?

转载 作者:太空宇宙 更新时间:2023-11-04 11:43:10 24 4
gpt4 key购买 nike

我有一个 ListView ,每次用户单击添加按钮时我想在其中存储特定数据。然而到目前为止,我在实现方面还没有取得什么成功。我知道这必须通过使用sharedpreferences来完成,并尝试实现sharedpreferences的保存状态和恢复状态,但我的listview仍然只添加当前单击的元素。

    package com.example.daniel.pset3_daniel_jacob;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class listview extends AppCompatActivity {

ArrayList<String> dataa;


@Override
protected void onCreate(Bundle savedInstanceState) {

onRestoredPreferences();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
Bundle extras = getIntent().getExtras();
String dataExtra = extras.getString("key");
dataa = new ArrayList<String>();
dataa.add(dataExtra);
ListView listView = (ListView) findViewById(R.id.listview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, dataa);
assert listView != null;
listView.setAdapter(adapter);
Savedpreferences(dataa);


}

public void Savedpreferences(List<String> dataa) {


SharedPreferences preferences = this.getSharedPreferences("object", 0);
SharedPreferences.Editor editor = preferences.edit();
List<String> arraylist = new ArrayList<String>(dataa);
Set<String> newset = new HashSet<String>(arraylist);
editor.putStringSet("stringset", newset);
editor.commit();
}


public void onRestoredPreferences()
{
SharedPreferences preferences = getApplicationContext().getSharedPreferences("object", 0);
Set<String> getdataback = preferences.getStringSet("stringset", null);
List <String> arraylist = new ArrayList<String>(getdataback);
}

}

我希望有人能帮助我告诉我我做错了什么

最佳答案

尝试下面的代码,如果有任何问题请告诉我。

public class ListviewActivity extends AppCompatActivity {

ArrayList<String> dataa;

List <String> arraylist = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {

onRestoredPreferences();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
Bundle extras = getIntent().getExtras();
String dataExtra = extras.getString("key");
dataa = new ArrayList<String>();
dataa.addAll(arraylist);
dataa.add(dataExtra);
ListView listView = (ListView) findViewById(R.id.listview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, dataa);
assert listView != null;
listView.setAdapter(adapter);
Savedpreferences(dataa);


}

public void Savedpreferences(List<String> dataa) {


SharedPreferences preferences = this.getSharedPreferences("object", 0);
SharedPreferences.Editor editor = preferences.edit();
List<String> arraylist = new ArrayList<String>(dataa);
Set<String> newset = new HashSet<String>(arraylist);
editor.putStringSet("stringset", newset);
editor.commit();
}


public void onRestoredPreferences()
{
SharedPreferences preferences = getApplicationContext().getSharedPreferences("object", 0);
Set<String> getdataback = preferences.getStringSet("stringset", null);
arraylist.clear();
arraylist.addAll(getdataback);
}

}

关于java - 离开 android 中的 Activity 后如何保留 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42599505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com