我正在尝试从Firebase数据库中检索某个级别的数据,但是我收到了警告
类java.util.ArrayList具有泛型类型参数,请改用GenericTypeIndicator
首先,我的数据结构如下:
我试图直接在“游戏”(即Method和Oxley)下检索字段并将其显示在列表视图中,而不是检索游戏的玩家。
这是我的代码
private ListView lv;
List<Game> games = new ArrayList<Game>();
List<String> gameNames = new ArrayList<String>();
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("Games");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sessions);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
lv = (ListView) findViewById(R.id.games);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
gameNames);
lv.setAdapter(arrayAdapter);
ref.addChildEventListener(new ChildEventListener(){
@Override
public void onChildAdded(DataSnapshot dataSnapshot,String prevChildKey){
Game newGame = dataSnapshot.getValue(Game.class);
gameNames.add(newGame.toString());
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
如果它有助于将游戏放入数据库的代码,我认为它可能与“地图”类型有关,因为我的列表视图在此之前工作正常。
public void beginGame(View view) {
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference();
EditText gameNameField = (EditText) findViewById(R.id.gameName);
String gameName = gameNameField.getText().toString().trim();
Collections.shuffle(players);
if (gameName.isEmpty()) {
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(getApplicationContext(),
R.string.toast_no_game_name, duration);
toast.show();
} else if (playerNames.size() < 2) {
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(getApplicationContext(),
R.string.toast_not_enough_players, duration);
toast.show();
} else {
for (int i = 0; i < players.size(); i++) {
if (i != (players.size() - 1)) {
players.get(i).target = players.get(i + 1);
} else {
players.get(i).target = players.get(0);
}
}
Game game = new Game(gameName, players);
DatabaseReference gamesRef = ref.child("Games").child(gameName).child("players");
Map<String, Player> gamePlayers = new HashMap<>();
for(int i = 0; i < players.size(); i++){
Player currentPlayer = players.get(i);
gamesRef.child(currentPlayer.getName()).setValue(currentPlayer);
}