本文介绍了使用Android / Java从嵌套对象Firebase中检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是Firebase的新手。这是我的数据看起来像



我想知道我怎么能从零售商检索distributorInfo。我试图从FirebaseRecyclerAdapter的populateViewHolder方法检索distributorInfo。我能够得到所选项目的关键。如何得到distributorInfo里面的信息?

  private void getRetailers(){
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());

$ b 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(){
$ b $ @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(); //避免ConcurrentModificationException异常


$ b @Override
public void onCancelled(DatabaseError databaseError){

}
};


mDatabase.addListenerForSingleValueEvent(valueEventListener);

}
});
}
};

recyclerView.setAdapter(adapter);

$ / code>

零售商信息代码: - $ /

公共类RetailerInfo {
字符串id,shopName,地址,城市,邮编,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;

$ b $ 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;


$ / code $ / pre

感谢您的任何帮助或建议。谢谢。 / b>

解决方案

例如,您也可以嵌套您的对象:

 公共类零售商{
字符串地址,区域,城市,联系人编号,联系人,shopName,zipCode;
DistributorInfo distributorInfo;

公共零售商(...){
//构造函数
}
//吸气和吸气器
}

public class DistributorInfo {
//所有字段,构造函数,getter和setter
}





Hi I am a newbie to Firebase .This is how my data looks like

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.

解决方案

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);

这篇关于使用Android / Java从嵌套对象Firebase中检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 13:44