gpt4 book ai didi

java - 在运行时从数组列表中添加和删除用户并保留项目详细信息

转载 作者:行者123 更新时间:2023-11-30 01:52:37 27 4
gpt4 key购买 nike

我有一个游戏,我正在开发一个游戏,在游戏开始之前,有一个选项可以在游戏中添加或删除玩家资料。这个想法是应用所有者可以在游戏中添加或删除玩家资料,这样他们就可以在同一台设备上单独玩或与 friend 一起玩。

这是此 Activity 的主屏幕:

enter image description here

如您所见,它的解释很清楚,所有已保存的用户都已加载,并且在游戏开始之前,必须至少选择一名玩家使用添加或删除按钮来玩游戏。

如前所述,如果只有一个配置文件保存到设备,则此功能可以正常工作。

enter image description here

在用户触摸添加按钮后,播放器会移动到用户播放器部分,详细信息将存储在 Activity 播放器数组中。

我遇到的问题是当您在数据库中保存了多个玩家时。当您再次尝试相同的功能时,只会添加第二个玩家。像这样:

enter image description here

无论添加哪个用户,似乎只移动最后一个用户。这可能是我错过的非常简单的事情,但我已经被困在这里一段时间了。

显示用户功能:

final HashMap<String,String> users = db.getUsers();
if(users.size() > 0){
noSavedPlayers.setVisibility(View.GONE);
}
Iterator it = users.entrySet().iterator();
int userCount = 0;
while(it.hasNext()){
Map.Entry pair = (Map.Entry) it.next();
final String id = pair.getKey().toString();
final String username = pair.getValue().toString();
//add this view to settings
usersClass.addToGameList(PlayerSelect.this, allUsers, username, id, new AddUserCallback() {
@Override
public void addUser(Boolean add, View thisPlayer) {
AddRemoveUser(username,id,add,thisPlayer);
}
});
userCount++;
it.remove();
}

private void AddRemoveUser(final String username, final String id, Boolean add, View thisPlayer){
Log.d("USERNAME",username + " is");
Boolean isAdded;
if(activePlayers.size() == 0){
isAdded = false;
} else {
isAdded = activePlayers.contains(username);
}
if(add){
//check if player is allready add to the game
if(!isAdded){
//add this user to the game
//remove form all players list
activePlayers.add(username);
activeIDs.add(id);
allUsers.removeView(thisPlayer);
usersClass.addToGameList(PlayerSelect.this, activeLayout, username, id, new AddUserCallback() {
@Override
public void addUser(Boolean add, View thisPlayer) {
if(add){
AddRemoveUser(username,id,true,thisPlayer);
} else {
AddRemoveUser(username,id,false,thisPlayer);
}
}
});
}
} else {
if(isAdded){
//remove this user to the game
//add back to all players list
int current = activePlayers.indexOf(username);
activePlayers.remove(current);
activeIDs.remove(current);
activeLayout.removeView(thisPlayer);
usersClass.addToGameList(PlayerSelect.this, allUsers, username, id, new AddUserCallback() {
@Override
public void addUser(Boolean add, View thisPlayer) {
if(add){
AddRemoveUser(username,id,true,thisPlayer);
} else {
AddRemoveUser(username,id,false,thisPlayer);
}
}
});
}
}
Log.d("VIEW",activePlayers.size() + "");
noActivePlayers.setVisibility((activePlayers.size() > 0)? View.GONE : View.VISIBLE);
noSavedPlayers.setVisibility((allUsers.getChildCount() > 0) ? View.GONE : View.VISIBLE);
}

usersClass.addToGameList 函数:

public void addToGameList(final Context context, final LinearLayout view, final String UserName, final String id, AddUserCallback callback){
delegate = callback;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View user = inflater.inflate(R.layout.user_add, null);

//set data
final TextView username = (TextView) user.findViewById(R.id.username);
username.setText(UserName);
Button save = (Button) user.findViewById(R.id.saveChanges);
final Button delete = (Button) user.findViewById(R.id.deleteUserBtn);

delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("GAME","User is to be removed from this game.");
delegate.addUser(false, user);
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("GAME","User to be added to the game");
delegate.addUser(true, user);
}
});
view.addView(user);

}

完整 Activity :

package r1d.org.uk.oiaskatedice;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import LocalDatabase.DatabaseHandler;

/**
* Created by paul on 21/09/2015.
*/
public class PlayerSelect extends Activity {
private ProFeatures PF;
private DatabaseHandler db;
private int maxPlayers = 3;
private LinearLayout activeLayout;
private LinearLayout allUsers;
private TextView noActivePlayers;
private TextView noSavedPlayers;
private ArrayList<String> activeIDs;
private ArrayList<String> activePlayers;
private Users usersClass;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.player_select);
db = new DatabaseHandler(this);
PF = new ProFeatures(this);
activeIDs = new ArrayList<String>();
activePlayers = new ArrayList<>();
activeLayout = (LinearLayout) findViewById(R.id.activePlayers);
allUsers = (LinearLayout) findViewById(R.id.allPlayers);

noActivePlayers = (TextView) findViewById(R.id.noActivePlayers);
noSavedPlayers = (TextView) findViewById(R.id.noSavedPlayers);

PF.checkForPro(new IsPro() {
@Override
public void isPro(Boolean isPro) {
if(!isPro){
new AdManager(PlayerSelect.this);
} else {
maxPlayers = 20;
}
}
});
//get tricks
Bundle extras = getIntent().getExtras();
if(extras.getString("Tricks")instanceof String){
final String usedTricks = extras.getString("Tricks");
} else {
final String[] usedTricks = extras.getStringArray("Tricks");
}
//show active players
//show all players
usersClass = new Users();
final HashMap<String,String> users = db.getUsers();
if(users.size() > 0){
noSavedPlayers.setVisibility(View.GONE);
}
Iterator it = users.entrySet().iterator();
int userCount = 0;
while(it.hasNext()){
Map.Entry pair = (Map.Entry) it.next();
final String id = pair.getKey().toString();
final String username = pair.getValue().toString();
//add this view to settings
usersClass.addToGameList(PlayerSelect.this, allUsers, username, id, new AddUserCallback() {
@Override
public void addUser(Boolean add, View thisPlayer) {
AddRemoveUser(username,id,add,thisPlayer);
}
});
userCount++;
it.remove();
}
//add more players
//check for upgrade
//gibe option to upgrage if needed
//save all players to an array
//save changes to saved player names
//start game when all players are added

}
private void AddRemoveUser(final String username, final String id, Boolean add, View thisPlayer){
Log.d("USERNAME",username + " is");
Boolean isAdded;
if(activePlayers.size() == 0){
isAdded = false;
} else {
isAdded = activePlayers.contains(username);
}
if(add){
//check if player is allready add to the game
if(!isAdded){
//add this user to the game
//remove form all players list
activePlayers.add(username);
activeIDs.add(id);
allUsers.removeView(thisPlayer);
usersClass.addToGameList(PlayerSelect.this, activeLayout, username, id, new AddUserCallback() {
@Override
public void addUser(Boolean add, View thisPlayer) {
if(add){
AddRemoveUser(username,id,true,thisPlayer);
} else {
AddRemoveUser(username,id,false,thisPlayer);
}
}
});
}
} else {
if(isAdded){
//remove this user to the game
//add back to all players list
int current = activePlayers.indexOf(username);
activePlayers.remove(current);
activeIDs.remove(current);
activeLayout.removeView(thisPlayer);
usersClass.addToGameList(PlayerSelect.this, allUsers, username, id, new AddUserCallback() {
@Override
public void addUser(Boolean add, View thisPlayer) {
if(add){
AddRemoveUser(username,id,true,thisPlayer);
} else {
AddRemoveUser(username,id,false,thisPlayer);
}
}
});
}
}
Log.d("VIEW",activePlayers.size() + "");
noActivePlayers.setVisibility((activePlayers.size() > 0)? View.GONE : View.VISIBLE);
noSavedPlayers.setVisibility((allUsers.getChildCount() > 0) ? View.GONE : View.VISIBLE);
}
}

任何关于这个问题的帮助都会很棒,我也同意问题标题,因为我真的不知道这个问题会出现在哪里。

最佳答案

虽然我知道将您的方法分成单一职责不会直接解决问题,但它会更容易阅读和调试,因为您可以隔离用户的删除和添加。

问题的产生很可能是因为做的太多了,这会在您的代码中添加额外的条件和复杂的逻辑,而这些是可以避免的。

我首先将 AddRemoveUser(...) 重构为两个只做一件事的方法:addUser(...)removeUser(。 ..)。这样做可能可以解决问题,因为您将简化代码并删除条件检查和新方法中不需要的东西。

关于java - 在运行时从数组列表中添加和删除用户并保留项目详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32779297/

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