0

Hi I am a newbie to Firebase .This is how my data looks like enter image description here

I would like to know how I can retrieve the distributorInfo from retailers.I am trying to retrieve distributorInfo from a FirebaseRecyclerAdapter's populateViewHolder method.I am able to get the key for the selected item.How do I get the information inside distributorInfo?

 private void getRetailers() {
        FirebaseRecyclerAdapter<RetailerInfo,RetailerViewHolder> adapter = new FirebaseRecyclerAdapter<RetailerInfo, RetailerViewHolder>(
                RetailerInfo.class,
                R.layout.distributor_row,
                RetailerViewHolder.class,
                mDatabase
        ) {
            @Override
            protected void populateViewHolder(RetailerViewHolder viewHolder, final RetailerInfo model, final int position) {

                viewHolder.setAddress(model.getContactPerson());
                viewHolder.setArea(model.getContactNumber());
                viewHolder.setShopName(model.getShopName());
                viewHolder.setZipcode(model.getZipcode());
                viewHolder.setCity(model.getCity());


                viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

//                        DatabaseReference ref = getRef(position);
//                        String key=   ref.getKey();
//                        Toast.makeText(Distributors.this, key, Toast.LENGTH_SHORT).show();


                        ValueEventListener valueEventListener = new ValueEventListener() {

                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                RetailerInfo retailerInfo = dataSnapshot.getValue(RetailerInfo.class);


                                Iterator it = retailerInfo.getDistributorInfo().entrySet().iterator();
                                while (it.hasNext()) {
                                    Map.Entry pair = (Map.Entry)it.next();
                                    Log.d("TAG","distributorInfo: "+pair.getKey() +  " = "  + pair.getValue());
                                    it.remove(); // avoids a ConcurrentModificationException
                                }
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        };


                        mDatabase.addListenerForSingleValueEvent(valueEventListener);

                    }
                });
            }
        };

        recyclerView.setAdapter(adapter);
    }

Code for RetailerInfo :-

public class RetailerInfo { String id, shopName,address,city,zipcode,area, contactPerson, contactNumber,contact_person_email,location_master,type,last_vist,last_visited_by,cityName,salesPersonName; HashMap distributorInfo;

    public RetailerInfo() {
    }

    public RetailerInfo(String id, String shopName, String address, String city, String zipcode, String area, String contactPerson, String contactNumber, String contact_person_email, String location_master, String type, String last_vist, String last_visited_by, String cityName, String salesPersonName, HashMap<String, String> distributorInfo) {
        this.id = id;
        this.shopName = shopName;
        this.address = address;
        this.city = city;
        this.zipcode = zipcode;
        this.area = area;
        this.contactPerson = contactPerson;
        this.contactNumber = contactNumber;
        this.contact_person_email = contact_person_email;
        this.location_master = location_master;
        this.type = type;
        this.last_vist = last_vist;
        this.last_visited_by = last_visited_by;
        this.cityName = cityName;
        this.salesPersonName = salesPersonName;
        this.distributorInfo = distributorInfo;
    }

    public HashMap<String, String> getDistributorInfo() {
        return distributorInfo;
    }

    public String getId() {
        return id;
    }

    public String getShopName() {
        return shopName;
    }

    public String getAddress() {
        return address;
    }

    public String getCity() {
        return city;
    }

    public String getZipcode() {
        return zipcode;
    }

    public String getArea() {
        return area;
    }

    public String getContactPerson() {
        return contactPerson;
    }

    public String getContactNumber() {
        return contactNumber;
    }

    public String getContact_person_email() {
        return contact_person_email;
    }

    public String getLocation_master() {
        return location_master;
    }

    public String getType() {
        return type;
    }

    public String getLast_vist() {
        return last_vist;
    }

    public String getLast_visited_by() {
        return last_visited_by;
    }

    public String getCityName() {
        return cityName;
    }

    public String getSalesPersonName() {
        return salesPersonName;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public void setContactPerson(String contactPerson) {
        this.contactPerson = contactPerson;
    }

    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }

    public void setContact_person_email(String contact_person_email) {
        this.contact_person_email = contact_person_email;
    }

    public void setLocation_master(String location_master) {
        this.location_master = location_master;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setLast_vist(String last_vist) {
        this.last_vist = last_vist;
    }

    public void setLast_visited_by(String last_visited_by) {
        this.last_visited_by = last_visited_by;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public void setSalesPersonName(String salesPersonName) {
        this.salesPersonName = salesPersonName;
    }
}

Any help or suggestion is appreciated.Thank you.

8
  • google for GSON parsing you will get the idea. Commented Sep 20, 2017 at 6:15
  • Thank you @SohailZahid will try it now. Commented Sep 20, 2017 at 6:20
  • @SohailZahid Firebase doesn't expose the raw JSON of the database values, so GSON is typically pretty useless unless you first convert from Firebase's DataSnapshot to JSON. Commented Sep 20, 2017 at 6:58
  • It'll be hard to help without seeing your RetailerInfo . But my answer here shows the general approach: stackoverflow.com/questions/42893231/… Commented Sep 20, 2017 at 7:00
  • @FrankvanPuffelen thanks for the guidance. I have added my RetailerInfo code please check. Thank you. Commented Sep 20, 2017 at 7:33

2 Answers 2

1

You can for example nest your objects as well:

public class Retailer{
    String Address, area, city, contactNumber, contactPerson, shopName, zipCode;
    DistributorInfo distributorInfo;

    public Retailer(...){
        // Contructor
    }
    // Getter and setters
}

public class DistributorInfo {
    // all fields, constructor, getter and setters
}

usage:

Retailer retailerInfo = dataSnapshot.getValue(Retailer.class);
Sign up to request clarification or add additional context in comments.

1 Comment

looks good. Probably nicer would be to pass the object directly instead of the fields separately, but I used intents too little to help you with that
1

Onclick inside the populateViewHolder I passed the key to getDistributorInfo(key). Please check the code for getDistributorInfo:-

private void getDistributorInfo(String key) {
DatabaseReference mDistributor = FirebaseDatabase.getInstance().getReference().child("/retailer/"+key+"/distributorInfo");

ValueEventListener postListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // Get Post object and use the values to update the UI
        if (dataSnapshot.exists())
        {
            DistributorInfo post = dataSnapshot.getValue(DistributorInfo.class);
            Log.d("DistributorInfo", post.getDist_id());
            Intent intent = new Intent(Distributors.this, DistributorDetailsActivity.class);
            intent.putExtra("id", post.getDist_id());
            intent.putExtra("shopname", post.getShop_name());
            intent.putExtra("address", post.getAddress());
            intent.putExtra("city", post.getCity());
            intent.putExtra("area", post.getArea());
            intent.putExtra("contact_personName", post.getContact_person());
            intent.putExtra("contact_personNumber", post.getContact_person_number());
            intent.putExtra("contact_personEmail", post.getContact_person_email());
            intent.putExtra("zipcode", post.getZipcode());
            intent.putExtra("last_visited_by", post.getLast_visited_by());
            intent.putExtra("lastVisitedBy", post.getLast_visited_by());
            intent.putExtra("cityName", post.getCity());
            intent.putExtra("type", "Retailer");
            startActivity(intent);
        }else{
            Toast.makeText(Distributors.this, "DistributorInfo is missing.", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Getting Post failed, log a message
        Log.d("DistributorInfo", "loadPost:onCancelled", databaseError.toException());

    }
};
mDistributor.addValueEventListener(postListener);


  }

Thank you @FrankvanPuffelen for the guidance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.