在Listview中显示Firebase中的数据时出错

时间:2019-06-22 22:25:46

标签: java android firebase listview firebase-realtime-database

当前,我正在尝试使活动在列出一个班级的地方工作,并在单击时在带有Listview的单独活动中返回用户的姓名和成绩。当用户注册该类时,其uid将保存在该类下。首先,我检索类名下列出的uid,这很有效,然后,我使用uid导航回用户信息,检索名称和等级并将其组合为一个变量,因此可以显示它在列表视图中。不幸的是,当我运行该应用程序时,出现错误:

Can't convert object of type java.lang.String to type com.samuelford48gmail.thsconnect.User

当我在调试模式下运行该应用程序时,它说名称和等级为空。另外,当我使用system.out.println显示数据快照时,它仅返回用户uid下的Classs。我认为部分问题可能是我在Firebase中的结构。

这是我的Firebase结构:

{
  "Science" : {
    "-LhrSfhNaQX1ZiNToeYQ" : {
  "Students" : {
    "-LhzXqvBPJl-gvN7bZ6c" : "9TKqqNqge5cKpFgjHByAFpZ3iRD2"
  },
  "date_clasname" : "dd",
  "room_number" : "dd",
  "teacher" : "dd",
  "uid" : "-LhrSfhNaQX1ZiNToeYQ"
},
"-LhrSfwx3nKd3DhRNJRp" : {
  "date_clasname" : "dd",
  "room_number" : "dd",
  "teacher" : "dd",
  "uid" : "-LhrSfwx3nKd3DhRNJRp"
},
"-LhrSg-4YJ52a1Zj9LoK" : {
  "Students" : {
    "-LhrfCKXsrjDWC4dJU8H" : "9TKqqNqge5cKpFgjHByAFpZ3iRD2"
  },
  "date_clasname" : "dd",
  "room_number" : "dd",
  "teacher" : "dd",
  "uid" : "-LhrSg-4YJ52a1Zj9LoK"
   }
  },
   "Users" : {
"9TKqqNqge5cKpFgjHByAFpZ3iRD2" : {
  "Classes" : {
    "-LhrfCKVETBZrgOIrCLR" : {
      "date_clasname" : "dd",
      "room_number" : "dd",
      "teacher" : "dd"
    },
    "-LhzWYnqD-qi8v-MFWKF" : {
      "date_clasname" : "dd",
      "room_number" : "dd",
      "teacher" : "dd"
    },
    "-LhzXqv0RZMts7Df1Ehg" : {
      "date_clasname" : "dd",
      "room_number" : "dd",
      "teacher" : "dd"
    }
  },
  "email" : "svhsdev@vigoschools.org",
  "grade" : "11",
  "name" : "Samuel"
},
"NnJlSVeqGXhJ2Nab2bhjr0HIpku2" : {
  "Classes" : {
    "-LhNEZI73xZWbu8tb4ZL" : {
      "date_clasname" : "March 5",
      "room_number" : "101",
      "teacher" : "pence"
    },
    "-LhNFH_LB8PGKKOML-Ta" : {
      "date_clasname" : "d",
      "room_number" : "",
      "teacher" : "s"
    },
    "-LhNFHjC0sYKaXd_OcX2" : {
      "date_clasname" : "d",
      "room_number" : "",
      "teacher" : "s"
    },
    "-LhrbM1htXYd0CxZiKMZ" : {
      "date_clasname" : "dd",
      "room_number" : "dd",
      "teacher" : "dd"

  },
  "email" : "samuelford48@gmail.com",
  "grade" : "12",
  "name" : "Samuel Ford"
},
}
 }
  }

这是尝试获取用户名称和等级的Listview活动的代码:

public class admin_show_students_uid extends AppCompatActivity {

  private FirebaseDatabase database;
  private DatabaseReference myRef;
  String user_info = null;
  String name = null;
ListView lv;
ArrayList<String> list=new ArrayList<>();
@Override
protected void onCreate( Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.admin_show_students_uid);

    lv = (ListView) findViewById(R.id.lv_science_students);
    final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
    lv.setAdapter(adapter);
    lv.setStackFromBottom(true);
    final String post_key = getIntent().getStringExtra("post_key");
    database = FirebaseDatabase.getInstance();
    myRef = database.getReference("Science").child(post_key).child("Students");
    myRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, String s) {
    //name gets the uid saved
            name = dataSnapshot.getValue(String.class);

      myRef = database.getReference("Users").child(name);

     getusersinfo();        
  adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

public void getusersinfo(){
    myRef.addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot dataSnapshot2 : dataSnapshot.getChildren()) {
                User user = dataSnapshot2.getValue(User.class);
                String name = user.getName();
                String grade = user.getGrade();
             user_info = ("Name " + name + "Grade" + grade);
  list.add(user_info);

            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

           }
       });

    }
  }

预先感谢您,如果您对我的代码有任何疑问!

1 个答案:

答案 0 :(得分:0)

问题是您获得了对特定用户的引用,但是在getuserinfo()中您正在迭代其属性。相反,只需从User获取dataSnapshot。参见:

public void getusersinfo(){
    studentRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            User user = dataSnapshot.getValue(User.class);
            String email = user.getName();
            String grade = user.getGrade();
            user_info = ("Name " + email + "Grade" + grade);
            list.add(user_info);               
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}