当前位置: 动力学知识库 > 问答 > 编程问答 >

Getting the child list of a node then iterate for similar values in another node in firebase and then get ita value(Firebase android))

问题描述:

My Firebase data stracture:

Bids-

-jobID

-userID : 1

-userID2 : 1

Jobs

-jobID

-name:''

-location:''

-desc:''

Users

-userID

-name:'name1'

-image: 'downloadUrl'

-UserID2

-name:'name2'

-image: 'downloadUrl'

I am able to geta all the keys under jobID in Bids i.e userID and userID2 using this code:

mBidders.child(getIntent().getExtras().getString("jobID")).addValueEventListener(new ValueEventListener() {

@Override

public void onDataChange(DataSnapshot dataSnapshot) {

List<String> lt = new ArrayList<String>();

for (DataSnapshot mSnapshot: dataSnapshot.getChildren()) {

lt.add(String.valueOf(mSnapshot.getKey()));

}

for (String userId : lt) {

Log.d("users", userId);

getUsersInfo(userId);

}

}

@Override

public void onCancelled(DatabaseError databaseError) {

}

});

Now when i want to get all the values under each user in the User using this method:

 getUsersInfo(String userId){

Query query = mUsers.child(userId);

FirebaseRecyclerAdapter<Users, BiddedJobViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, BiddedJobViewHolder>(

Users.class,

R.layout.bidder_profile_row,

BiddedJobViewHolder.class,

query

) {

@Override

protected void populateViewHolder(BiddedJobViewHolder viewHolder, Users model, int position) {

viewHolder.setTitle(model.getUser());

viewHolder.setImge(getApplicationContext(), model.getImage());

}

};

bidRecycleView.setAdapter(firebaseRecyclerAdapter);

}

It worked at first but only retrieving one users information to the recycleview on adding another user its replaced with the new one(in the recycleview). Now when i run the app, i get this error:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.toni.patakazi.model.Users

at com.google.android.gms.internal.zzbqi.zze(Unknown Source)

at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)

at com.google.android.gms.internal.zzbqi.zza(Unknown Source)

at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)

at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:154)

at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:143)

at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:183)

at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6067)

at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6100)

at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5282)

at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5158)

at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)

at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:562)

at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)

at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)

at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)

at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3379)

at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3188)

at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3632)

at android.view.View.layout(View.java:15927)

at android.view.ViewGroup.layout(ViewGroup.java:5113)

at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)

at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)

at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)

at android.view.View.layout(View.java:15927)

at android.view.ViewGroup.layout(ViewGroup.java:5113)

at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)

at android.widget.FrameLayout.onLayout(FrameLayout.java:568)

at android.view.View.layout(View.java:15927)

at android.view.ViewGroup.layout(ViewGroup.java:5113)

at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)

at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)

at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)

at android.view.View.layout(View.java:15927)

at android.view.ViewGroup.layout(ViewGroup.java:5113)

at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)

at android.widget.FrameLayout.onLayout(FrameLayout.java:568)

at android.view.View.layout(View.java:15927)

at android.view.ViewGroup.layout(ViewGroup.java:5113)

at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)

at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)

at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)

at android.view.View.layout(View.java:15927)

at android.view.ViewGroup.layout(ViewGroup.java:5113)

at android.widget.FrameLayout.layoutChildren(FrameLayout.java:633)

at android.widget.FrameLayout.onLayout(FrameLayout.java:568)

at android.view.View.layout(View.java:15927)

at android.view.ViewGroup.layout(ViewGroup.java:5113)

at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2445)

at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2142)

at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1267)

at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6501)

at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)

at android.view.Choreographer.doCallbacks(Choreographer.java:603)

at android.view.Choreographer.doFrame(Choreographer.java:572)

at android.v

I don't seem to understand the error.If there is another better way of doing i'll really appreciate it

网友答案:

The above is a format error you result is of type Users while you are capturing it as a String Try :

  //Get Jobs 
   Map<String, String> mapJobs = new HashMap<String, String>();
    for( DataSnapshot i : dataSnapshot.getChildren()) {
        String jobs = i.getValue(String.class);
        mapJobs.put(i.getKey(),jobs);
    }
    ArrayList<String> jobsList = new ArrayList<>(mapJobs.values());


   //Get Bids 
   Map<String, String> mapBids = new HashMap<String, String>();
    for( DataSnapshot i : dataSnapshot.getChildren()) {
        String bid = i.getValue(String.class);
        mapBids.put(i.getKey(),bid);


    }
    ArrayList<String> bidList = new ArrayList<>(mapBids.values());
分享给朋友:
您可能感兴趣的文章:
随机阅读: