为了账号安全,请及时绑定邮箱和手机立即绑定

更改Android中的活动时如何保留ArrayList?

更改Android中的活动时如何保留ArrayList?

绝地无双 2021-09-15 15:51:18
这是我将数据发送到 TambahDataEstimasi 的活动:private void tambahdataEstimasi(String string) {     dialogCustom = new AlertDialog.Builder(PartActivity.this);     dialogCustom.setCancelable(true);     dialogCustom.setIcon(R.drawable.ic_file);     dialogCustom.setTitle("Tambah Data Estimasi");     dialogCustom.setMessage("Apakah Anda Ingin Menambahkan Data : \n" + string + " Ke Daftar Estimasi?");     dialogCustom.setPositiveButton("Ya", new DialogInterface.OnClickListener() {         @Override         public void onClick(DialogInterface dialog, int which) {             Intent intent = new Intent(PartActivity.this, TambahEstimasi.class);             intent.putParcelableArrayListExtra("dataestimasi", adapter.checkedPart);             startActivity(intent);         }     });     dialogCustom.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {         @Override         public void onClick(DialogInterface dialog, int which) {             dialog.dismiss();             adapter.checkedPart.clear();         }     });     dialogCustom.show();}这是我的 TambahDataEstimasi,用于从之前的活动中获取数据:public class TambahEstimasi extends AppCompatActivity {ArrayList<ModelPart> listEstimasi = new ArrayList<>();ModelPart part ;ArrayList<ModelPart> estimasiPart = new ArrayList<>();private RecyclerView mRecyclerView;private Context context;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_tambah_estimasi);    mRecyclerView = (RecyclerView) findViewById(R.id.cardView_estimasi);    Intent intent = this.getIntent();    listEstimasi =  intent.getParcelableArrayListExtra("dataestimasi");    System.out.println("data Intent : " + listEstimasi.size());    for (int i=0; i<listEstimasi.size(); i++) {        part = listEstimasi.get(i);        String nomor = part.getNomorPart().toString();        System.out.println("Isi data Intent : " + nomor);    }当我回到上一个活动时,我在 TambahDataEstimasi 中的 ArrayList 数据丢失了。我应该怎么做才能保留 ArrayList 数据?
查看完整描述

3 回答

?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

当您通过按后退按钮或通过调用完成返回上一个活动时,活动将被破坏,数据不再保留。如果您想保留您的数据,。你可以使用很多技巧。


查看完整回答
反对 回复 2021-09-15
?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

您可以sharedPreferences为此目的使用。


public <T> void putList(String key, List<T> list) {

        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putString(key, gson.toJson(list));

        editor.apply();

    }


    public <T> List<T> getList(String key, Class<T> clazz) {

        Type typeOfT = TypeToken.getParameterized(List.class, clazz).getType();

        return gson.fromJson(getString(key, null), typeOfT);

    }

以上这些函数需要像这样放在一个 PrefManager 类中:


    public class PrefManager {

    private SharedPreferences sharedPreferences;

    private Gson gson;


    public PrefManager(Context context, Gson gson) {

        this.sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

        this.gson = gson;

    } 


    ...


    public <T> void putList(String key, List<T> list) {

        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putString(key, gson.toJson(list));

        editor.apply();

    }


    public <T> List<T> getList(String key, Class<T> clazz) {

        Type typeOfT = TypeToken.getParameterized(List.class, clazz).getType();

        return gson.fromJson(getString(key, null), typeOfT);

    }

}

然后在要设置列表时调用:


prefManager.putList(prefManager.Key.USER_LIST, userList);

然后当你想检索它时:


List<User> userList = prerManager.getList(PrefManager.Key.USER_LIST, User.class);

从这里参考


查看完整回答
反对 回复 2021-09-15
  • 3 回答
  • 0 关注
  • 204 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信