【发布时间】:2016-10-21 07:47:44
【问题描述】:
我在使用 Android 列表时遇到了一些问题。我有一种方法可以创建玩家列表,并从 firebase 数据库和存储中加载玩家。
有些玩家有头像,它们作为生成的key存储在storage/players/keyname中(玩家在数据库中唯一的key就是Firebase存储中的头像名称)。
我遇到的问题是当我尝试创建 OnSuccessListener/player 时。我似乎无法传递数据。
onSuccessListener 的问题:
- 外部的所有内容都被视为“外部类”,这意味着当我想创建一个播放器时,它无法读取 onsuccesslistener 中的数据。
- 内部的所有内容都不能将任何数据传递到侦听器之外。
- 我无法将其转换为位图
我需要能够使用来自侦听器和地图条目的数据创建“玩家 p”。到目前为止,我失败了(不能同时通过)。
//Method to create the list of players. To do: call again after adding new player
public void createPlayerList(){
//The list of players that will be filled
final List listPlayers = new ArrayList<Player>();
//The path where all our players are stored.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users")
.child( FirebaseAuth.getInstance().getCurrentUser().getUid() ).child("team").child("players");
//Method that just gets called once
ref.addListenerForSingleValueEvent(new ValueEventListener() {
//dataSnapshot contains all data from the path to our players
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, Object> players = (Map<String, Object>) dataSnapshot.getValue();
//We return our player data in a map. Loop through each value in the map and create a player object for each player
//And add that player object to our list of players
//Create Storage instance
FirebaseStorage storage = FirebaseStorage.getInstance();
//Create storageReference
final StorageReference storageRef = storage.getReferenceFromUrl("gs://bucket.appspot.com");
// Create a reference to a file from a Google Cloud Storage URI
StorageReference gsReference = storage.getReferenceFromUrl(storageRef + "/players/");
for (Map.Entry<String, Object> entry : players.entrySet()){
Map singlePlayer = (Map) entry.getValue();
//Get unique key
String key = entry.getKey();
storageRef.child(key).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
//Picasso.with(getActivity()).load(uri).into(R.id.playerProfilePicture);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});
//standard mage
Bitmap profileBits = BitmapFactory.decodeResource(getResources(), R.drawable.player_icon);
PlayersFragment.this.setProfilePicture(profileBits);
//Create player
Player p = new Player((String) singlePlayer.get("name"), (String) singlePlayer.get("preferredPosition"), profilePicture );
//Add player to list
listPlayers.add(p);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Add all results from database to our listview
listViewPlayers = (ListView) fragmentView.findViewById(R.id.players_list);
//Attach adapter to listview, to get a layout for each row
listViewPlayers.setAdapter(new PlayerAdapter(getActivity(), R.layout.row_player_list_item, listPlayers));
//Necessary for handling clicks
listViewPlayers.setItemsCanFocus(true);
}
【问题讨论】:
标签: android firebase firebase-realtime-database firebase-storage anonymous-inner-class