简体   繁体   English

我需要在另一个活动中复制我的arraylist

[英]I need to copy my arraylist in another activity

I need my arraylist (mNamelist) from ActivityPlayers.java and use that arraylist in my ActivityNewGame.java file. 我需要来自ActivityPlayers.java的arraylist(mNamelist),并在ActivityNewGame.java文件中使用该arraylist。 Also, it should update at the same time. 另外,它应该同时更新。 For example in ActivityPlayers, the user gives a new name or delete an existing name, it should also insert or delete that name to the same list in ActivityNewGame. 例如,在ActivityPlayers中,用户提供一个新名称或删除现有名称,它还应该将该名称插入或删除到ActivityNewGame中的同一列表中。

This is my ActivityPlayers.java method. 这是我的ActivityPlayers.java方法。 This method will be called when the user adds a new name to mNamelist. 当用户向mNamelist添加新名称时,将调用此方法。

private void addItem(int position) {
    /** Get user input (name) **/
    textAdd = findViewById(R.id.name_input);

    /** Add name to the list **/
    mNameList.add(position, new NameItem(textAdd.getText().toString().trim()));

    /** sort that list **/
    sortArrayList();

    /** save changes to shared preferences **/
    saveData();

    /** Show changed list to user **/
    mAdapter.notifyItemInserted(position);

    /** Clear the input field **/
    textAdd.getText().clear();

    /** Send namelist to ActivitynewGame **/
    Intent i = new Intent(ActivityPlayers.this, ActivityNewGame.class);
    i.putExtra("PlayerList", mNameList);
    startActivity(i);
}

This is my ActivityNewGame: 这是我的ActivityNewGame:

    public class ActivityNewGame extends AppCompatActivity {
        private ArrayList<NewGamePlayerItem> mPlayerList;

        private RecyclerView mRecyclerView;
        private RecyclerView.Adapter mAdapter;
        private RecyclerView.LayoutManager mLayoutManager;

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

            mPlayerList = new ArrayList<>();
            Intent intent = getIntent();
            ArrayList<NameItem> mNameList = (ArrayList<NameItem>) intent.getSerializableExtra("PlayerList");

            /** Check if mNameList has any items in it. **/
            if (mNameList.size() != 0) {
            /** Loop through that arraylist and set its names into this Activity items. ("true" is for checkbox that this item has.) **/
                for (int i = 0; i < mNameList.size(); i++) {
                    mPlayerList.add(new NewGamePlayerItem(true, mNameList.get(i).getText1()));
                }
            }

            buildRecyclerView();
        }

The problem here is that I don't want to visit ActivityNewGame, I just want to pass that name list from ActivityPlayers, and when user choose to go to make a new game (ActivityNewGame), then that player list would be there. 这里的问题是我不想访问ActivityNewGame,我只想从ActivityPlayers传递该名称列表,并且当用户选择去制作一个新游戏(ActivityNewGame)时,该玩家列表就在那里。 So what should I do differently to manage to do that? 那么我应该怎样做才能做到这一点呢?

You have several options: 您有几种选择:

  1. Save the list in a SqlLite database and load/reload in activities. 将列表保存在SqlLite数据库中,并在活动中加载/重新加载。
  2. Pass the list in an Intent object, see this question 在Intent对象中传递列表,请参见此问题
  3. Serialize in a shared preferences 序列化共享首选项

To pass data from one Activity to Another, you can use: 要将数据从一个活动传递到另一个活动,可以使用:

  1. SQlite Database SQlite数据库
  2. Passing Object through Intent 通过意图传递对象
  3. Using the Shared Preference 使用共享首选项

I feel Passing Object through Intent will best suit your requirement. 我觉得通过Intent传递对象最适合您的需求。 To pass the Object through Intent the Object should be Serialized. 要通过Intent传递对象,应将对象序列化。 ie, It must implement the Serializable Interface. 即,它必须实现可序列化接口。

In your example you are required to pass the ArrayList of NameItem. 在您的示例中,您需要传递NameItem的ArrayList。 ArrayList itself is Serializable, so you now need to implement the Serializable Inferace in NameItem Class as below: ArrayList本身是可序列化的,因此您现在需要在NameItem类中实现以下可序列化的推断:

class NameItem implements Serializable{
    String name;
    //getters and setters
}

Now you can pass the ArrayList using Intent from ActivityPlayers to ActivityNewGame as below: 现在,您可以使用Intent将ArrayList从ActivityPlayers传递到ActivityNewGame,如下所示:

Intent i = new Intent(ActivityPlayers.this,ActivityNewGame.class);
i.putExtra("PlayerList",mNameList);
startActivity(i);

You can fetch the Data in ActivityNewGame out of Intent as below: 您可以从Intent中获取ActivityNewGame中的数据,如下所示:

Intent intent = getIntent();
ArrayList<NameItem> mNameList = (ArrayList<NameItem>) intent.getSerializableExtra("PlayerList");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM