Shahzeb Mehdi
Shahzeb Mehdi

Reputation: 41

Unable to retrieve data from firebase database android, recycler view

Model Class Code

public class DeleteEditEventsModel
{
    private String title, description, date, time, venue;

    DeleteEditEventsModel() { }

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

    public String getTitle() {
        return title;
    }

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

    public String getDescription() {
        return description;
    }

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

    public String getDate() {
        return date;
    }

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

    public String getTime() {
        return time;
    }

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

    public String getVenue() {
        return venue;
    }

    public void setVenue(String venue) {
        this.venue = venue;
    }
}

Is there any issue with this class?

Adapter Class Code:

public class DeleteEditEventsAdapter extends FirebaseRecyclerAdapter<DeleteEditEventsModel, DeleteEditEventsAdapter.myViewHolder> {

    public DeleteEditEventsAdapter(@NonNull FirebaseRecyclerOptions<DeleteEditEventsModel> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull final myViewHolder holder, final int position, @NonNull final DeleteEditEventsModel model) {

        holder.title.setText(model.getTitle());
        holder.description.setText(model.getDescription());
        holder.date.setText(model.getDate());
        holder.time.setText(model.getTime());
        holder.venue.setText(model.getVenue());


        holder.delete_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                FirebaseDatabase.getInstance().getReference().child("Events")
                        .child(getRef(position).getKey()).removeValue();
            }
        });
    }

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

    class myViewHolder extends RecyclerView.ViewHolder {

        TextView title, description, date, time, venue, edit_btn, delete_btn;


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

            title = (TextView) itemView.findViewById(R.id.delete_event_Title);
            description = (TextView) itemView.findViewById(R.id.delete_event_Description);
            date = (TextView) itemView.findViewById(R.id.delete_event_Date);
            time = (TextView) itemView.findViewById(R.id.delete_event_Time);
            venue = (TextView) itemView.findViewById(R.id.delete_event_Venue);
            edit_btn = (TextView) itemView.findViewById(R.id.delete_event_edit_btn_tv);
            delete_btn = (TextView) itemView.findViewById(R.id.delete_event_btn_TV);
        }
    }

}

Activity class

public class DeleteEditEventsActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    DeleteEditEventsAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_events);

        recyclerView = (RecyclerView) findViewById(R.id.delete_events_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);



        FirebaseRecyclerOptions<DeleteEditEventsModel> options =
                new FirebaseRecyclerOptions.Builder<DeleteEditEventsModel>()
                .setQuery(FirebaseDatabase.getInstance().getReference("Events"), DeleteEditEventsModel.class)
                .build();

        adapter = new DeleteEditEventsAdapter(options);

        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

JSON FILE

{
  "CSE Notification" : {
    "cse notification" : "Computer Science and Engineering"
  },
  "Central Notification" : {
    "central notification" : "Inn-sha-ALLAH we'll be doing"
  },
  "Civil Notification" : {
    "civil notification" : "civil"
  },
  "ECE Notification" : {
    "ece notification" : "ece"
  },
  "EEE Notification" : {
    "eee notification" : "eee"
  },
  "Events" : {
    "21:00" : {
      "EventDate" : "08/05/21",
      "EventDescription" : "laptops",
      "EventTime" : "21:00",
      "EventTitle" : "Lenovo",
      "EventVenue" : "Hyderabad"
    },
    "5:16" : {
      "EventDate" : "17/05/21",
      "EventDescription" : "To retrieve",
      "EventTime" : "5:16",
      "EventTitle" : "Trying",
      "EventVenue" : "Hyderabad"
    }
  },
  "Examination_Branch_Notification" : {
    "em_noti" : "AstagfiruILLAH"
  },
  "IT Notification" : {
    "it notification" : "Information Technology"
  },
  "MBA Notification" : {
    "mba notification" : "mba"
  },
  "MECH Notification" : {
    "mech notification" : "MECH"
  },
  "M_TECH_Notification" : {
    "mTech" : "Master of Technology"
  }
}

I think there is a problem in my model class

As you can see Data is not being fed into the card

I am using recycler view for the second time but I've checked my code against the previous code it is exactly the same(As I've validated), but can't understand the problem here.

Upvotes: 2

Views: 302

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

When you try to map a node from the Realtime Database into an object of type "DeleteEditEventsModel", the name of the fields that exist in your class must match the name of your properties that exist in your database. Unfortunately, in your case, the fields don't match. See, the fields in your database start with "Event", while in the class are not, and this is not correct.

To solve this, you have two options, you either change the name of your properties in the database to match the one in the class, or you can use an annotation in front of the getters. For example, if you have a field called "date" and the property in the database is called "EventDate", your getter should look like this:

@PropertyName("EventDate")
public String getDate() {
    return date;
}

In this way, you tell the compiler to look for a property called "EventDate" and not "date".

Upvotes: 2

Related Questions