我想为在列表视图中选择的用户项运行getKey(),以便可以使用此键确定将用户带到何处,我尝试了很多stackoverflow解决方案,但失败了,并出现了许多不同的错误。 。 非常感谢您的帮助,非常感谢!
详细信息如下:
Firebase数据填充到Adapter Listview-没有错误
UserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference gameInfoRef = rootRef.child("user").child(UserId).child("gameInfo");
gameInfoRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ListView lv_SelectGame = findViewById(R.id.lv_SelectGame);
GameList.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Game game = ds.getValue(Game.class);
GameList.add(game);
}
History_SelectGameAdapter adapter = new History_SelectGameAdapter(ida_History_SelectGame.this, GameList);
lv_SelectGame.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
}
});
用户单击Listview项,返回该特定游戏的键(由先前在其他活动中的push()生成,存储在firebase中)-请提供解决方案,谢谢!
Firebase数据:(列表视图中的每个项目均按日期显示)
gameInfo
-LMIdDTDiUd3ajVVMdzC
gameDate: "2018-09-01"
gameMode: "SomeMode"
-LMOGEUbCTsaeywPICqi
gameDate: "2018-09-06"
gameMode: "SomeMode"
-LMQfCQ7TO21WP3qW7v3
gameDate: "2018-09-15"
gameMode: "SomeMode"
现在这是我不知道如何写的部分,谢谢!
(当我单击Listview项时,显示Toast时没有任何错误,但是我不知道如何获取位置并基于该位置运行getKey())
lv_SelectGame.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
ListView lv_SelectGame = findViewById(R.id.lv_SelectGame);
Toast.makeText(ida_History_SelectGame.this, "getKey() of this item", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:1)
您想要ID,例如LMIdDTDiUd3ajVVMdzC,LMOGEUbCTsaeywPICqi ...?
然后,您需要做的是向对象游戏键添加一个新属性(如果没有)。
当您从Firebase获取数据时,获取ID并将其添加到对象Game:
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Game game = ds.getValue(Game.class);
String idYouWant = String.valueOf(ds.getKey());
//We add to the object game
game.setKey(idYouWant)
GameList.add(game);
}
最后,您只需要从数组GameList中获取它,就像这样:
lv_SelectGame.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
Game game = GameList.get(position);
String key = game.getKey();
Toast.makeText(ida_History_SelectGame.this, "getKey() of this item", Toast.LENGTH_SHORT).show();
}