【发布时间】:2017-05-30 15:21:28
【问题描述】:
我有 setOnItemClickListener() 方法的列表视图,当点击时我希望它打开新活动并给它 2 个额外内容,项目编号和登录用户(返回主屏幕),问题是活动开始两次,一次具有良好的附加功能,另一种具有“0”位置的附加功能...
此代码应该检查 Firebase 服务器是否在其中(不超过 5 个玩家),如果有,则将用户添加到服务器并加入房间(启动 ServerActivity)。
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
myRef = database.getReference("Servers/S"+position);
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int i = 0;
while (dataSnapshot.child("player"+i).child("username").getValue() != null)
i++;
if (i < 5) {
Log.d("Implementing Child","player"+i);
CustomUser customUser = new CustomUser(false,currentUser.getUsername(),"",0,currentUser.getWins());
myRef.child("player" + i).setValue(customUser);
Intent intent = new Intent(GameActivity.this,ServerActivity.class);
intent.putExtra("serverNum",position);
intent.putExtra("currentUser",currentUser);
Log.d("position", position + "");
startActivity(intent);
finish();
}
else{
Toast.makeText(getApplicationContext(), "Server Full", Toast.LENGTH_SHORT).show();
myRef.child("isJoinable").setValue(false);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Toast.makeText(getApplicationContext(), "" +
"Internet Error", Toast.LENGTH_SHORT).show();
}
});
Intent intent = new Intent(GameActivity.this,ServerActivity.class);
intent.putExtra("currentUser",currentUser);
startActivity(intent);
finish();
//TODO FIX: activity probebly launches twice
}
});
我尝试在清单中修改 LaunchMode,但结果失败,导致“serverNum”额外为 0 而不是位置。
【问题讨论】:
标签: android android-studio android-activity firebase firebase-realtime-database