简体   繁体   中英

Activity won't render arraylist before visiting other activity

I have Arraylist(mNamelist) in ActivityPlayers. It's been declared as public static so I can access it in 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. 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

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.

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

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

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. All you need to do is initialize the values in ActivityNewGame .

Change your loadData() into a static function and call it first in your onCreate() in ActivityNewGame .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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