如何修复com.google.firebase.DatabaseException:无法将类型java.util.HashMap的值转换为String

时间:2019-04-16 12:14:52

标签: java android

错误为com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String 我想在回收者视图中获取数据,但这给了我错误。 我尽力解决了这个问题,但我做不到。 我给出一些代码片段

Firebase模型:

数据属于我的Firebase数据库

     "user" : {
        "9gmWSVMhN4erdOeNCcXw7uB5Itq2" : {
          "email" : "look@gmail.com",
          "name" : "sagar",
          "password" : "asdfgh",
          "user_id" : "9gmWSVMhN4erdOeNCcXw7uB5Itq2"
        },

        "template" : {

          "FYKw520tCiPxrwi622xzdPzyGCi1" : {
            "-LbWzneRw0zTnE5naR2F" : {
              "c_address" : "aaa",
              "c_logo" : "https://www.freelogodesign.org/Content/img/logo-ex-7.png",
              "c_name" : "aaa",
              "p_designation" : "CTO",
              "p_email" : "yan@com",
              "p_name" : "name",
              "p_phone" : "012"
            },
            "-LbX2_P-KIaCEAkn9I3B" : {
              "c_address" : "bbb",
              "c_logo" : "https://www.freelogodesign.org/Content/img/logo-ex-7.png",
              "c_name" : "bbb",
              "p_designation" : "CEO",
              "p_email" : "gmail",
              "p_name" : "bbbb",
              "p_phone" : "098"
            }
     }

所以我想检索下的孩子

我尝试了这段代码来检索数据


 DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference usersRef = rootRef.child("Accounts").child("Users");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String email = ds.child("email").getValue(String.class);
                String name = ds.child("name").getValue(String.class);
                Log.d("TAG", email + " / " + name);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    usersRef.addListenerForSingleValueEvent(eventListener);


这是我接收数据的模型类 我认为此类很适合获取数据

Template.java


package com.example.look.np.models;

public class Template
{
  private String p_name;
  private String p_designation;
  private String p_email;
  private String p_phone;
  private String c_name;
  private String c_address;
  private String c_logo;
  private String tempID;

  //empty constructor
  public Template()
  {
  }
  //Constructor with variables
  public Template(String p_name, String p_designation, String p_email, String p_phone, String c_name, String c_address, String c_logo, String tempID)
  {
    this.p_name = p_name;
    this.p_designation = p_designation;
    this.p_email = p_email;
    this.p_phone = p_phone;
    this.c_name = c_name;
    this.c_address = c_address;
    this.c_logo = c_logo;
    this.tempID = tempID;
  }
  //Getter and Setter Methods 
  public String getP_name()
  {
    return p_name;
  }

  public void setP_name(String p_name)
  {
    this.p_name = p_name;
  }

  public String getP_designation()
  {
    return p_designation;
  }

  public void setP_designation(String p_designation)
  {
    this.p_designation = p_designation;
  }

  public String getP_email()
  {
    return p_email;
  }

  public void setP_email(String p_email)
  {
    this.p_email = p_email;
  }

  public String getP_phone()
  {
    return p_phone;
  }

  public void setP_phone(String p_phone)
  {
    this.p_phone = p_phone;
  }

  public String getC_name()
  {
    return c_name;
  }

  public void setC_name(String c_name)
  {
    this.c_name = c_name;
  }

  public String getC_address()
  {
    return c_address;
  }

  public void setC_address(String c_address)
  {
    this.c_address = c_address;
  }

  public String getC_logo()
  {
    return c_logo;
  }

  public void setC_logo(String c_logo)
  {
    this.c_logo = c_logo;
  }

  public String getTempID()
  {
    return tempID;
  }

  public void setTempID(String tempID)
  {
    this.tempID = tempID;
  }

}

这是我的Fragment类,我想在“回收者”视图中显示数据

MyCardFragment.class


public class MyCardFragment extends Fragment
{
  private static final String TAG = "MyCardFragment";

  RecyclerView templateRecycler;
  RecyclerviewAdapter adapter;

  List<Template> templateList;

  FirebaseDatabase firebaseDatabase;
  DatabaseReference databaseReference;
  DatabaseReference myref;
  FirebaseUser firebaseUser;
  FirebaseAuth auth;

  String key;

  //This is the empty constructor
  public MyCardFragment()
  {
    // Required empty public constructor
  }

  //This is the oncreate view where i wanna retrieve the data
  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
  {

    View fragmentView = inflater.inflate(R.layout.fragment_mycards, container, false);

    templateRecycler = fragmentView.findViewById(R.id.recyclerView);
    templateRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));

    auth = FirebaseAuth.getInstance();
    firebaseUser = auth.getCurrentUser();
    firebaseDatabase = FirebaseDatabase.getInstance();

    databaseReference = firebaseDatabase.getReference("user").child("template").child(firebaseUser.getUid());

    return fragmentView;
  }

在下面的onstart方法中,我想获取数据


  @Override
  public void onStart()
  {
    super.onStart();
    //Getting Template List From Database
    //The folloeing code block give me error
    databaseReference.addValueEventListener(new ValueEventListener()
    {
      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot)
      {
        templateList = new ArrayList<>();
        for (DataSnapshot templateSnap : dataSnapshot.getChildren())
        {

          Template template = templateSnap.getValue(Template.class);
          //--->This line gives me error i don 't know what happed here
          //  -- > This line should give what i want
         // -- > I can 't understand whether the model class is incorrect or database refrence
          Log.d(TAG, "onDataChange: " + template.toString());
          templateList.add(template);
        }
        adapter = new RecyclerviewAdapter(getActivity(), templateList);
        templateRecycler.setAdapter(adapter);
      }

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

      }
    });

  }
}

注意:通过使用此代码,数据将被检索一次,但我不知道发生错误后会发生什么情况

请帮助我,已经两天了,我想纠正错误

2 个答案:

答案 0 :(得分:0)

1。根据您的验证数据库引用的代码添加此语句

Log.i("Uid",firebaseUser.getUid());
databaseReference = firebaseDatabase.getReference("user").child("template").child(firebaseUser.getUid());

添加此行后,检查记录Uid是否重用数据库中的相同密钥。

2。如果Key匹配,则用于检查模型类尝试打印日志

Log.i("template Snap key",templateSnap.child("c_address").getValue(String.class));
Template template = templateSnap.getValue(Template.class);
Log.i("template name",template.c_address);

在这两个日志之后,您将了解确切发生问题的位置,并在此处添加这些日志,以便我可以了解确切发生问题的位置。 BTW Model类似乎很好。

3。用户的父级是什么?

4。其次在OnDatachange方法之外创建List <>对象。

答案 1 :(得分:0)

这很有趣,但这是IDE的错。

我刚刚将IDE更新到最新版本,它解决了我的问题。