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

这是我将数据发送到 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;


@Override

protected 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 数据?


绝地无双
浏览 205回答 3
3回答

凤凰求蛊

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

梵蒂冈之花

您可以sharedPreferences为此目的使用。public <T> void putList(String key, List<T> list) {&nbsp; &nbsp; &nbsp; &nbsp; SharedPreferences.Editor editor = sharedPreferences.edit();&nbsp; &nbsp; &nbsp; &nbsp; editor.putString(key, gson.toJson(list));&nbsp; &nbsp; &nbsp; &nbsp; editor.apply();&nbsp; &nbsp; }&nbsp; &nbsp; public <T> List<T> getList(String key, Class<T> clazz) {&nbsp; &nbsp; &nbsp; &nbsp; Type typeOfT = TypeToken.getParameterized(List.class, clazz).getType();&nbsp; &nbsp; &nbsp; &nbsp; return gson.fromJson(getString(key, null), typeOfT);&nbsp; &nbsp; }以上这些函数需要像这样放在一个 PrefManager 类中:&nbsp; &nbsp; public class PrefManager {&nbsp; &nbsp; private SharedPreferences sharedPreferences;&nbsp; &nbsp; private Gson gson;&nbsp; &nbsp; public PrefManager(Context context, Gson gson) {&nbsp; &nbsp; &nbsp; &nbsp; this.sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);&nbsp; &nbsp; &nbsp; &nbsp; this.gson = gson;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; ...&nbsp; &nbsp; public <T> void putList(String key, List<T> list) {&nbsp; &nbsp; &nbsp; &nbsp; SharedPreferences.Editor editor = sharedPreferences.edit();&nbsp; &nbsp; &nbsp; &nbsp; editor.putString(key, gson.toJson(list));&nbsp; &nbsp; &nbsp; &nbsp; editor.apply();&nbsp; &nbsp; }&nbsp; &nbsp; public <T> List<T> getList(String key, Class<T> clazz) {&nbsp; &nbsp; &nbsp; &nbsp; Type typeOfT = TypeToken.getParameterized(List.class, clazz).getType();&nbsp; &nbsp; &nbsp; &nbsp; return gson.fromJson(getString(key, null), typeOfT);&nbsp; &nbsp; }}然后在要设置列表时调用:prefManager.putList(prefManager.Key.USER_LIST, userList);然后当你想检索它时:List<User> userList = prerManager.getList(PrefManager.Key.USER_LIST, User.class);从这里参考
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java