- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我之前曾尝试获得此问题的答案,但找不到可以解决我的问题的答案。我正在制作保存圆盘高尔夫球分数的应用程序。我的 MainActivity 有 4 个按钮。新比赛、恢复比赛、类(class)和球员。
Atm 当用户转到 ActivityPlayers(主 Activity 中的“玩家”按钮)时,用户可以添加和删除玩家,并且共享首选项会保存该列表。但现在我还需要以某种方式获取相同的玩家列表,即 ActivityNewGame(主 Activity 中的新游戏按钮),我需要循环该列表,并使用这些名称在 ActivityNewGame 列表中创建新项目。 (不能直接从 ActivityPlayers 复制列表,因为在该回收器 View 中,这些项目具有与新游戏回收器 View 不同的按钮,即使它们使用相同的名称列表。)
我尝试执行 Intent ,但我意识到我不能使用它,因为当用户从 ActivityPlayers 添加或删除玩家时,我必须访问该 ActivityNewGame...
这是我尝试过的方法
这是我的 ActivityPlayers.java 方法。当用户向 mNamelist 添加新名称时将调用此方法。
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 changes to shared preferences **/
saveData();
/** Show changed list to user **/
mAdapter.notifyItemInserted(position);
/** Clear the input field **/
textAdd.getText().clear();
/** Send namelist to ActivitynewGame **/
Intent i = new Intent(ActivityPlayers.this, ActivityNewGame.class);
i.putExtra("PlayerList", mNameList);
startActivity(i);
}
这是我的 savedata() 方法,用于保存用户对 ActivityPlayers 中的名单所做的更改:
private void saveData() {
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(mNameList);
editor.putString("task list", json);
editor.apply();
}
这是我的 ActivityNewGame:
public class ActivityNewGame extends AppCompatActivity {
private ArrayList<NewGamePlayerItem> mPlayerList;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_game);
mPlayerList = new ArrayList<>();
Intent intent = getIntent();
ArrayList<NameItem> mNameList = (ArrayList<NameItem>) intent.getSerializableExtra("PlayerList");
/** Check if mNameList has any items in it. **/
if (mNameList.size() != 0) {
/** Loop through that arraylist and set its names into this Activity items. ("true" is for checkbox that this item has.) **/
for (int i = 0; i < mNameList.size(); i++) {
mPlayerList.add(new NewGamePlayerItem(true, mNameList.get(i).getText1()));
}
}
buildRecyclerView();
}
这里的问题是我不想访问 ActivityNewGame,我只想从 ActivityPlayers 传递该名单,当用户选择去制作新游戏(ActivityNewGame)时,该玩家列表就会在那里。那么我应该采取什么不同的做法才能做到这一点?
如果您有的话,请给我一些想法的例子,谢谢。
最佳答案
您可以将列表设为静态,然后就不需要传递它。您可以在应用程序中的任何地方使用它。
通过使其静态,可能会出现内存泄漏
问题,因此在使用后正确处置
它们。
关于java - 如何将数组列表从 Activity 发送到另一个 Activity ,而不访问其他 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56990830/
我是一名优秀的程序员,十分优秀!