gpt4 book ai didi

java - 在访问其他 Activity 之前, Activity 不会渲染数组列表

转载 作者:行者123 更新时间:2023-12-02 03:05:34 26 4
gpt4 key购买 nike

我在 ActivityPlayers 中有 Arraylist(mNamelist)。它已被声明为公共(public)静态,因此我可以在 ActivityNewGame 中访问它。我的问题是,当我启动我的应用程序并直接进入 ActivityNewGame 时,在我访问 ActivityPlayers 之前(我必须打开它),它不会渲染该数组列表,之后它会在 ActivityNewGame 中完美渲染该列表在我再次关闭应用程序之前的整个 session 。任何想法为什么会这样,或者我能做什么?这个应用程序是我自己的项目,它没有任何敏感数据,因此“丑陋”的解决方案也可以,只要它不会向用户显示即可。

这是我的 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()));
}
}
}

最佳答案

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

将您的 loadData() 更改为静态函数,并首先在 ActivityNewGameonCreate() 中调用它。

编辑 -

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

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

关于java - 在访问其他 Activity 之前, Activity 不会渲染数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57024641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com