简体   繁体   中英

Error in retrieving data from firebase into recyclerView

In the below code, At start I am retrieving user's profile info which they saved in previous activity(This is working fine) Now, user tries to save some targets in firebase which I want to retrieve in recyclerView whenever it is saved.There is no error in saving the data. My app crashes whenever I tries to retrieve data into recyclerView. It works fine if I retrieve a particular data into a textView.

I have tried various ways given on Stack OverFlow but nothing seems to be working Please help me with this.

ProfileActivity.class

   @Override
    protected void onStart() {
        super.onStart();

        //retrieving profile data

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();

        reference = FirebaseDatabase.getInstance().getReference("User's Details").child(uid).child("profile info");

        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
             String name= (String) snapshot.child("name").getValue();
             String bio = (String) snapshot.child("bio").getValue();
             String imageUrl = (String) snapshot.child("imageUrl").getValue();

                final_name.setText(name);
                final_Bio.setText(bio);
                picasso.get().load(imageUrl).into(final_profileImage);


            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Log.w("TAG",error.getMessage());
                Toast.makeText(Doc_ProfilePage.this,error.getMessage(),Toast.LENGTH_SHORT).show();

            }
        });    


//saving challenge
    private void SaveChallenge(String titleText, String description) {

        int year = myCalender.get(Calendar.YEAR);
        int month = myCalender.get(Calendar.MONTH);
        int day = myCalender.get(Calendar.DAY_OF_MONTH);
        String dateText = new StringBuilder().append(day).append("/").append(month).append("/").append(year).toString();

        //getting TimeFormat
        int hour = myCalender.get(Calendar.HOUR);
        int minute = myCalender.get(Calendar.MINUTE);
        String timeText = new StringBuilder().append(hour).append(":").append(minute).toString();



        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        String uid = user.getUid();

    DatabaseReference Challengereference = FirebaseDatabase.getInstance().getReference("User's Details").child(uid).child("ChallengeDetails");

        if (!timeText.equals("") && !titleText.isEmpty() && !description.isEmpty()) {
id = Challengereference.push().getKey();
            ChallengeDetails ChallengeDetails = new ChallengeDetails(titleText, description, dateText, timeText);
            Challengereference.child(id).setValue(ChallengeDetails).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Toast.makeText(Doc_ProfilePage.this, "Successfully saved", Toast.LENGTH_SHORT).show();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(Doc_ProfilePage.this, e.getMessage(), Toast.LENGTH_SHORT).show();

                }
            });

        }

        //adding data in recyclerView
        FirebaseDatabase.getInstance().getReference("User's Details").child(uid).child("ChallengeDetails").orderByKey().
                addChildEventListener(new ChildEventListener() {
           @Override
           public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
               final ChallengeDetails challengeDetails = snapshot.getValue(ChallengeDetails.class);
               challengedetail.add(challengeDetails);

               GridLayoutManager layoutManager = new GridLayoutManager(Doc_ProfilePage.this,3,RecyclerView.HORIZONTAL,false);
               recyclerView.setLayoutManager(layoutManager);
               GridAdapter adapter = new GridAdapter(challengedetail);
               recyclerView.setAdapter(adapter);

           }

           @Override
           public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

           }

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

           }

           @Override
           public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

           }

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

           }
       });
    }

GridAdapter class

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {

    private static final String TAG = "ActivityName";

    private List<ChallengeDetails> Challengedetails;

public GridAdapter( List<ChallengeDetails> detailss) {
   //  this.mcontext = context;
this.Challengedetails = detailss;
}


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.challenge_grid_view,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final ChallengeDetails challenge = Challengedetails.get(position);
holder.ChallengeTitleGrid.setText(challenge.getTitle());
holder.ChallengeDescriptionGrid.setText(challenge.getDescription());
holder.ChallengeGridDate.setText(challenge.getDate());
holder.ChallengeGridTime.setText(challenge.getTime());

    }

    @Override
    public int getItemCount() {
        return Challengedetails.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView ChallengeTitleGrid, ChallengeDescriptionGrid, ChallengeGridDate, ChallengeGridTime;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            ChallengeTitleGrid = itemView.findViewById(R.id.ChallengeTitleGrid);
            ChallengeDescriptionGrid = itemView.findViewById(R.id.ChallengeDescriptionGrid);
            ChallengeGridDate = itemView.findViewById(R.id.SelectDate);
            ChallengeGridTime = itemView.findViewById(R.id.SelectTime);

        }
    }}

ChallengeDetails Class

public class ChallengeDetails {
 String title;
 String description;
 String date;
 String time;


    public void setTitle(String title) {
        this.title = title;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public ChallengeDetails(String title, String description, String date, String time){
        this.title = title;
        this.description = description;
        this.date = date;
        this.time = time;
    }



    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public String getDate() {
        return date;
    }

    public String getTime() {
        return time;
    }


}

Image of Firebase Database

I think orderbychild send you array of datasnapshot not single document snapshot so you have to iterate through that snapshot.

public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
   for(DataSnapshot ds : dataSnapshot.getChildren()) {
       final ChallengeDetails challengeDetails = snapshot.getValue(ChallengeDetails.class);
       challengedetail.add(challengeDetails);
   }
   GridLayoutManager layoutManager = new GridLayoutManager(Doc_ProfilePage.this,3,RecyclerView.HORIZONTAL,false);
   recyclerView.setLayoutManager(layoutManager);
   GridAdapter adapter = new GridAdapter(challengedetail);
   recyclerView.setAdapter(adapter);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM