简体   繁体   English

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

[英]Activity won't render arraylist before visiting other activity

I have Arraylist(mNamelist) in ActivityPlayers. 我在ActivityPlayers中有Arraylist(mNamelist)。 It's been declared as public static so I can access it in ActivityNewGame. 它被声明为public static,所以我可以在ActivityNewGame中访问它。 My problem is that when I launch my app and go straight to that ActivityNewGame, it won't render that arraylist, before I have visited ActivityPlayers (I have to literally just open it), after that it renders that list in ActivityNewGame perfectly fine within that whole session before I again shut down the app. 我的问题是,当我启动应用程序并直接转到该ActivityNewGame时,在我访问ActivityPlayers之前,它不会渲染该数组列表(必须从字面上打开它),之后,它会在ActivityNewGame中完美地渲染该列表。整个会话之前,我再次关闭该应用程序。 Any ideas why this is, or what could I do? 有任何想法为什么会这样,或者我该怎么办? This app is my own project and it has nothing sensitive data so "uglier" solutions are also fine, as long as it won't show to the user. 这个程序是我自己的项目,它没有敏感数据,因此“丑陋”的解决方案也很好,只要它不会显示给用户即可。

Here is my ActivityPlayers, addItem() insert and save new name to the list 这是我的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();
    }

And here is my ActivityNewGame, insertNames() takes that Arraylist from ActivityPlayers and insert its names in to the recyclerview items. 这是我的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()));
            }
        }
    }

This is Due to the mNamelist in ActivityPlayers is empty 这是由于ActivityPlayers中mNamelist为空

Data pass in the mNamelist is in the ActivityNewGame So you have to pass the data from the ActivityNewGame in order to render the arrayList mNamelist中的数据传递位于ActivityNewGame中,因此您必须传递ActivityNewGame中的数据以呈现arrayList

This is my suggestion without seeing the code. 这是我的建议,没有看到代码。 But I think this works for you and you might understand. 但是我认为这对您有效,您可能会理解。

You're basically not initializing your mNameList until the ActivityPlayer is opened. 在打开ActivityPlayer之前,您基本上不会初始化mNameList All you need to do is initialize the values in ActivityNewGame . 您需要做的就是初始化ActivityNewGame的值。

Change your loadData() into a static function and call it first in your onCreate() in ActivityNewGame . 将您的loadData()更改为静态函数,然后在ActivityNewGame onCreate()中首先调用它。

Edit - 编辑 -

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

Just call loadData(this) in the onCreate as necessary. 只需根据需要在onCreate调用loadData(this)

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

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