Minte Temple
Minte Temple

Reputation: 55

How to Delete Items from RecyclerView and Read from two children in Firebase

Am new to android development am trying to figure out how to do two thing.

  1. Use a close button to delete items from a RecyclerView and Firebase
  2. Retrieve items from two children in a database. In my code I was able to retrieve from just one child (offer_Rides) but I will like to retrieve from another child called "Users".

enter image description here

public class WallActivity extends AppCompatActivity {


private Button ivClose;

private RecyclerView offerList;

private DatabaseReference mDatabase;

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

    mDatabase = FirebaseDatabase.getInstance().getReference().child("Offer_Rides");


    ivClose = (Button) findViewById(R.id.ivClose);
 //   ivClose.setOnClickListener((View.OnClickListener) this);


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

}

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

  FirebaseRecyclerAdapter<OfferPost, OfferPostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<OfferPost, OfferPostViewHolder>(
          OfferPost.class,
          R.layout.offer_list_row,
          OfferPostViewHolder.class,
          mDatabase

          ){
      @Override
      protected void populateViewHolder(OfferPostViewHolder viewHolder, OfferPost model, int position) {
            viewHolder.setFirstname(model.getFirstname());
          viewHolder.setLastname(model.getLastname());
          viewHolder.setPhone(model.getPhone());
          viewHolder.setPrice(model.getPrice());
          viewHolder.setSeats(model.getSeats());
          viewHolder.setLocation(model.getLocation());
        viewHolder.setDestination(model.getDestination());
      }
  };

offerList.setAdapter(firebaseRecyclerAdapter); }

public static class OfferPostViewHolder extends RecyclerView.ViewHolder{

    View mView;
    public OfferPostViewHolder(View itemView) {
        super(itemView);

        mView = itemView;
    }

    public void setFirstname(String firstname){
        TextView post_firstname = (TextView) mView.findViewById(R.id.tvFirstname);
        post_firstname.setText(firstname);

    }
    public void setLastname(String lastname){
        TextView post_firstname = (TextView) mView.findViewById(R.id.tvLastname);
        post_firstname.setText(lastname);

    }
    public void setPhone(String phone){
        TextView post_phone = (TextView) mView.findViewById(R.id.tvPhone);
        post_phone.setText(phone);

    }
    public void setPrice(String price){
        TextView post_price = (TextView) mView.findViewById(R.id.tvCash);
        post_price.setText(price);

    }
    public void setSeats(String seats){
        TextView post_seats = (TextView) mView.findViewById(R.id.tvSeats);
        post_seats.setText(seats);

    }
    public void setLocation(String location){
        TextView post_location = (TextView) mView.findViewById(R.id.tvLocation);
        post_location.setText(location);

    }
    public void setDestination(String destination){
      TextView post_destination = (TextView) mView.findViewById(R.id.tvDestination);
    post_destination.setText(destination);

}

}

}

Upvotes: 0

Views: 1130

Answers (1)

Ajin kumar
Ajin kumar

Reputation: 311

If you want to delete users values in firebase use this code

DatabaseReference databaseReference = firebaseDatabase.getReference().child("Users");
    databaseReference.removeValue();

Upvotes: 2

Related Questions