我在ActivityPlayers中有Arraylist(mNamelist)。它被声明为public static,所以我可以在ActivityNewGame中访问它。我的问题是,当我启动应用程序并直接转到该ActivityNewGame时,在我访问ActivityPlayers之前,它不会渲染该数组列表(必须从字面上将其打开),之后,它将在ActivityNewGame中完美地渲染该列表。整个会话之前,我再次关闭该应用程序。有任何想法为什么会这样,或者我该怎么办?
这个程序是我自己的项目,它没有敏感数据,因此“丑陋”的解决方案也很好,只要它不会显示给用户即可。

这是我的ActivityPlayers,addItem()插入并将新名称保存到列表中

public class ActivityPlayers extends AppCompatActivity {
    public static ArrayList<NameItem> mNameList;

    private RecyclerView mRecyclerViewPlayers;
    private NameAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    private Button buttonAdd;
    private EditText textAdd;

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

        loadData();
        buildRecyclerView();
        setButtons();
    }

    private void saveData() {
        /** save data to shared pref **/
        SharedPreferences prefs = getSharedPreferences("shared preference", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        try {
            editor.putString("SharedPrefKey", ObjectSerializer.serialize(mNameList));
        } catch (IOException e) {
            e.printStackTrace();
        }
        editor.commit();
    }

    private void loadData() {
        if (mNameList == null) {
            mNameList = new ArrayList<>();
        }

        SharedPreferences prefs = getSharedPreferences("shared preference", Context.MODE_PRIVATE);
        try {
            mNameList = (ArrayList<NameItem>) ObjectSerializer.deserialize(prefs.getString("SharedPrefKey", ObjectSerializer.serialize(new ArrayList<NameItem>())));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    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 to shared pref **/
        saveData();

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

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


这是我的ActivityNewGame,insertNames()从ActivityPlayers中获取该Arraylist,并将其名称插入recyclerview项中。

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

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

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

        insertNames();
        buildRecyclerView();
    }

    private void insertNames() {
        if (ActivityPlayers.mNameList == null) {
            mPlayerList = new ArrayList<>();
        } else {
            mPlayerList = new ArrayList<>();

            for (int i = 0; i < ActivityPlayers.mNameList.size(); i++) {
                /** false here is for my checkbox, which is in the item **/
                mPlayerList.add(new NewGamePlayerItem(false, ActivityPlayers.mNameList.get(i).getText1()));
            }
        }
    }

最佳答案

在打开mNameList之前,您基本上不会初始化ActivityPlayer。您需要做的只是初始化ActivityNewGame中的值。

将您的loadData()更改为静态函数,然后在onCreate()中的ActivityNewGame中首先调用它。

编辑-

    public void loadData(Context context) {
        if (mNameList == null) {
            mNameList = new ArrayList<>();
        }

        //SharedPreferences prefs = getApplicationContext().getSharedPreferences("shared preference", Context.MODE_PRIVATE);
        SharedPreferences prefs = context.getSharedPreferences("shared preference", Context.MODE_PRIVATE);
        try {
            mNameList = (ArrayList<NameItem>) ObjectSerializer.deserialize(prefs.getString("SharedPrefKey", ObjectSerializer.serialize(new ArrayList<NameItem>())));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


只需根据需要在loadData(this)中调用onCreate

09-30 14:27