有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java列表保存在模型中,而不在setter中进行设置

我目前正在从事一个Android项目,在Getter和Setter方面遇到了问题。问题是我的模型(getter&setter)被设置为一个值,而我没有设置它。如果模型为空,我会从服务器获取数据并将其存储在模型中

以下是我的代码片段:

以下是IntentService的代码:

  public GameLobbyService() {
    super("GameLobbyService");
}

@Override
protected void onHandleIntent(Intent intent) {

    while (true){
        if (GameLobbyActivity.inGameRoomStatus.equals("")){
            try {
                Thread.sleep(5000);
                updateLobby();
                Intent broadcastIntent = new Intent();
                broadcastIntent.setAction(GameLobbyActivity.LobbyUpdater.PROCESS_RESPONSE);
                broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
                broadcastIntent.putExtra(RESPONSE_STRING, responseObj);
                sendBroadcast(broadcastIntent);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else {
            break;
        }
    }
}

以下是我的接收器的代码:

public class LobbyUpdater extends BroadcastReceiver {
    public static final String PROCESS_RESPONSE = "com.rudethingstosay.updatelobby.PROCESS_RESPONSE";
    @Override
    public void onReceive(Context context, Intent intent) {

        String responseString = intent.getStringExtra(GameLobbyService.RESPONSE_STRING);
        int counter =0;
        String roomName = "";
        String gameRoomId = "";
        Boolean gameRoomStatus;
        gameLobby.clear();
        try {
            JSONArray jsonResultArray = new JSONArray(responseString);
            for (counter = 0; counter < jsonResultArray.length(); counter++){
                JSONObject jsonobj = (JSONObject) jsonResultArray.get(counter);
                roomName = (String) jsonobj.get("email");
                gameRoomId = (String) jsonobj.get("gameRoomId");
                gameRoomStatus = (Boolean) jsonobj.get("gameRoomStatus");
                gameLobby.add(roomName + " room");
            }

            if (GameRoom.getCurrentGameLobby() == null) {
                GameRoom.setCurrentGameLobby(gameLobby);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(lvGameViewLobby.getContext(),安卓.R.layout.simple_list_item_1,gameLobby);
                lvGameViewLobby.setAdapter(adapter);
                Log.d("TEST", "NO DATA FROM DB, SAVED CHANGES");
            } else {

                Log.d("MODEL", String.valueOf(GameRoom.getCurrentGameLobby())); //Luma
                Log.d("MODELDB", String.valueOf(gameLobby)); //Bago
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

结果必须是,如果我从服务器获得一个新项目,ModelDB日志必须是最新的

示例ModelDb:1 2 3 4 那么模型日志必须是:1 2 3

但正在发生的是。 他们都得到了更新

D/模型:[试验2室、试验3室、试验4室、试验5室、试验6室、试验7室] D/MODELDB:[试验2室、试验3室、试验4室、试验5室、试验6室、试验7室]

提前谢谢你的帮助


共 (0) 个答案