我在Android列表中遇到了一些问题。我有一个方法,创建一个播放器列表,并从firebase数据库和存储加载播放器。
一些球员有个人资料照片,他们被存储在storage/players/keyname
中作为生成的密钥(一个球员在数据库中的唯一密钥是Firebase存储中的个人资料名称)。
我遇到的问题是当我尝试创建OnSuccessListener/player时。我似乎无法传递数据。
onSuccessListener:
问题- 外部的所有东西都被认为是"outerclass",这意味着当我想创建一个播放器时,它不能读取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);
}
记住addListenerForSingleValueEvent会立即返回。传递给它的侦听器将在稍后被调用。从现在的情况来看,看起来你希望它阻塞,直到结果可用,listPlayers被完全填充。
相反,我建议处理listViewPlayers从你的ondatachchange回调,在你的球员列表被填充后立即。
还要注意gsReference似乎没有在任何地方使用。你确定你的意思不是说gsReference.child(key).getDownloadUrl()...
在"players"文件夹下查找每个在循环中找到的玩家吗?