我正在使用FireBase使用Android应用程序。在我添加的数据库中,我向其中一个用户出版物添加了两个表。我将发布信息和URI的数量放在一起,以便通过另一个表格为每个用户的信息检索信息。当我这样做时,我需要使用数据库事件
(.addValueEventListener).
嵌套但是我收到错误
(错误:(157,33)错误:局部变量UserName是从内部类中访问的;需要声明为final)
这是使用的代码
private void UserPostInformation(final String key) {
DatabaseReference historyDatabase = FirebaseDatabase.getInstance().getReference().child("ThePostting").child(key);
historyDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
String IdHistory = dataSnapshot.getKey();
Long time = 0L;
String foodname11 = "";
String imagefood1 = "";
String s = null;
String name = "name";
String UserName = "";
for (DataSnapshot child : dataSnapshot.getChildren())
{
if (child.getKey().equals("time")) {
time = Long.valueOf(child.getValue().toString());
}
if (child.getKey().equals("FoodName")) {
foodname11 = child.getValue().toString();
}
if (child.getKey().equals("profileImageUrl")) {
imagefood1 = child.getValue().toString();
}
if (child.getKey().equals("UserID")) {
s = child.getValue().toString();
}
}
UserIDGetInfo = s;
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(UserIDGetInfo);
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
Map<String,Object> map = (Map<String,Object>) dataSnapshot.getValue();
if (map.get("name")!=null)
{
//error is her where using the String UserName get the error local variable UserName is accessed from within inner class; needs to be declared final)
UserName= map.get("name").toString();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
HistoryObject obj =new HistoryObject(IdHistory , getDate(time),foodname11 , imagefood1 , UserName);
resultHistory.add(obj);
mhistoryAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
答案 0 :(得分:1)
将UserName
声明为该类的属性,您也可以正确获取UserName
的空值,因为您在加载User之前设置了HistoryObject
的实例
结帐我的修复
private String UserName = "";
private void UserPostInformation(final String key)
{
DatabaseReference historyDatabase = FirebaseDatabase.getInstance().getReference().child("ThePostting").child(key);
historyDatabase.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
String IdHistory = dataSnapshot.getKey();
Long time = 0L;
String foodname11 = "";
String imagefood1 = "";
String s = null;
String name = "name";
UserName = "";
for (DataSnapshot child : dataSnapshot.getChildren())
{
if (child.getKey().equals("time")) {
time = Long.valueOf(child.getValue().toString());
}
if (child.getKey().equals("FoodName")) {
foodname11 = child.getValue().toString();
}
if (child.getKey().equals("profileImageUrl")) {
imagefood1 = child.getValue().toString();
}
if (child.getKey().equals("UserID")) {
s = child.getValue().toString();
}
}
UserIDGetInfo = s;
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(UserIDGetInfo);
databaseReference.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
Map<String,Object> map = (Map<String,Object>) dataSnapshot.getValue();
if (map.get("name")!=null)
{
UserName= map.get("name").toString();
}
HistoryObject obj = new HistoryObject(IdHistory , getDate(time),foodname11 , imagefood1 , UserName);
resultHistory.add(obj);
mhistoryAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
答案 1 :(得分:0)
要么将username声明为类变量,要么将修饰符更改为final,错误是谨慎的local variable UserName is accessed from within inner class; needs to be declared final
因此将其声明为final Stirng userName