使用ValueEventListener显示配置文件的单独数据

时间:2018-10-28 14:01:02

标签: android firebase-realtime-database firebase-authentication google-cloud-firestore

我使用适配器和firebase部分通过使用listView和simple_list_item_1布局来显示数据来检索数据

兹附上Firebase供参考: firebase architecture

这是我的ViewDetails类

public class viewMyDetails  extends AppCompatActivity {

public static  final String TAG = "viewMyDetails";

private FirebaseDatabase mFirebaseDb;
private FirebaseAuth mFireAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

private DatabaseReference dbRef;
private String userId;
private ListView mListView;
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile_distinct_activity);

    //getting instances
    mFireAuth =  FirebaseAuth.getInstance();
    mFirebaseDb = FirebaseDatabase.getInstance();

    dbRef = mFirebaseDb.getReference();
    FirebaseUser user = mFireAuth.getCurrentUser();
    userId = user.getUid();

    mListView = (ListView)findViewById(R.id.listView);

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = mFireAuth.getCurrentUser();
                if(user != null){
                    toastFunc("successfully signed as"+ user.getEmail());
                }
                else{
                    toastFunc("successfully signed out!");
                }
        }
    };

    dbRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            showData(dataSnapshot);
        }

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

        }
    });
}

private void showData(DataSnapshot dataSnapshot){

    for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
        storeStudentDetails sd = new storeStudentDetails();
        sd.setStudentName(dataSnapshot1.child(userId).getValue(storeStudentDetails.class).getStudentName());
        sd.setEmail(dataSnapshot1.child(userId).getValue(storeStudentDetails.class).getEmail());
        sd.setBook(dataSnapshot1.child(userId).getValue(storeStudentDetails.class).getBook());
        sd.setFine(dataSnapshot1.child(userId).getValue(storeStudentDetails.class).getFine());
        sd.setDept(dataSnapshot1.child(userId).getValue(storeStudentDetails.class).getDept());
        sd.setRollNo(dataSnapshot1.child(userId).getValue(storeStudentDetails.class).getRollNo());

        Log.d(TAG,"show data : name" +sd.getStudentName());
        Log.d(TAG,"show data: rollNum" + sd.getRollNo());

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add(sd.getStudentName());
        arrayList.add(sd.getRollNo());
        arrayList.add(String.valueOf(sd.getFine()));
        arrayList.add(sd.getDept());
        arrayList.add(String.valueOf(sd.getFine()));
        arrayList.add(sd.getBook());

        ArrayAdapter mAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
        mListView.setAdapter(mAdapter);

    }
}
@Override
public void onStart(){
    super.onStart();
    mFireAuth.addAuthStateListener(mAuthListener);
}

@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mFireAuth.removeAuthStateListener(mAuthListener);
    }
}


private void toastFunc(String message){
    Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}

}

但已按预期登录,但未获取任何数据! valueEventListener运行不正常,我认为是这样! 很高兴获得建议

github链接:https://github.com/THIYAGU22/DuesettlementDetails 谢谢!

1 个答案:

答案 0 :(得分:0)

  

valueEventListener运行不正常

这根本不起作用,因为您正在尝试使用Firebase实时数据库SDK之外的监听器,而不是Cloud Firestore的监听器,后者是完全不同的产品。如果您想使用ListView将Cloud Firestore数据库中的数据显示到ArrayAdapter中,请查看我对 post 的回答(请忽略随机元素部分),我已经解释了如何做到这一点。