活动在访问其他活动之前不会呈现arraylist

时间:2019-07-14 04:37:39

标签: java android

我在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()));
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

这是由于ActivityPlayers中的 mNamelist为空

  

mNamelist中的数据传递在ActivityNewGame中   因此,您必须从   ActivityNewGame以便呈现arrayList

这是我的建议,没有看到代码。 但是我认为这对您有效,您可能会理解。

答案 1 :(得分:0)

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

loadData()更改为静态函数,然后在onCreate()的{​​{1}}中首先调用它。

编辑-

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)