android将对象保存在firebase中 [英] android save object in firebase

查看:346
本文介绍了android将对象保存在firebase中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了登录应用程序,并使用了Firebase数据库。
但我有一个问题,当我试图保存对象在数据库中。虽然当我试图保存简单的字符串或简单的 HashMap 对象一切正常。



我得到一个错误: / b>


com.google.firebase.database.DatabaseException:发现名称为setWallpaper的冲突setter(与在android.content.ContextWrapper上定义的setWallpaper冲突)


这是我的代码:
$ b

主要活动:

  public class MainActivity extends AppCompatActivity {
EditText userName;
EditText密码;
按钮signInbtn;
DatabaseReference url;
地图< String,User> users = new HashMap< String,User>();
@Override
protected void onCreate(Bundled savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final FirebaseDatabase database = FirebaseDatabase.getInstance();
url = database.getInstance()。getReference();
userName =(EditText)findViewById(R.id.userName);
password =(EditText)findViewById(R.id.password);
signInbtn =(Button)findViewById(R.id.signInBtn);
signInbtn.setOnClickListener(signObject);

$ b View.OnClickListener signObject = new View.OnClickListener(){
@Override
public void onClick(View view){
User user =新用户(Moshe,1234);
users.put(101,user);
DatabaseReference userUrl = url.child(Users);
userUrl.setValue(users);
System.out.println(users);
}
};

类用户

  public class User extends Application {

public static String password;
public static String name;

public User(){

}

public User(String name,String password){
this.name = name;
this.password = password;
}

public String getName(){
return name;
}

public void setName(String name){
User.name = name;
}

public String getPassword(){
return password;
}

public void setPassword(String password){
User.password = password;


$ / code $ / pre

我将permmision.Internet添加到清单中。我读了很多关于这个主题的问题,特别是关于Firebase的问题,但是我没有找到解决我的问题的方法。

Firebase只能读取和写入相当基本的Java对象(即所谓的普通旧Java对象或POJO)到数据库中。通过从 Application 扩展您的 User 类,您违反了这些规则。



解决方案是让你的 User 类完全独立:

  public class User {
...


I created sign in app and I am using with firebase database. But I have a problem when I am trying to save object in the database. Although when I am trying to save simple string or simple HashMap object everything working.

I got an error:

com.google.firebase.database.DatabaseException: Found a conflicting setters with name: setWallpaper (conflicts with setWallpaper defined on android.content.ContextWrapper)

Here is my code:

Main Activity:

public class MainActivity extends AppCompatActivity {
    EditText userName;
    EditText password;
    Button signInbtn;
    DatabaseReference url;
    Map<String, User> users = new HashMap<String, User>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final FirebaseDatabase database = FirebaseDatabase.getInstance();
        url = database.getInstance().getReference();
        userName = (EditText) findViewById(R.id.userName);
        password = (EditText) findViewById(R.id.password);
        signInbtn = (Button) findViewById(R.id.signInBtn);
        signInbtn.setOnClickListener(signObject);

    }  
    View.OnClickListener signObject = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            User user = new User("Moshe","1234");
            users.put("101",user);
            DatabaseReference userUrl = url.child("Users");
            userUrl.setValue(users);
            System.out.println(users);
        }
    };

Class User:

public class User extends Application {

    public static String password;
    public static String name ;

    public User(){

    }

    public User(String name,String password){
        this.name = name;
        this.password = password;
    }

    public  String getName() {
        return name;
    }

    public  void setName(String name) {
        User.name = name;
    }

    public  String getPassword() {
        return password;
    }

    public  void setPassword(String password) {
        User.password = password;
    }
}

I added permmision.Internet to the manifest. I read a lot of question about this subject and specially about Firebase, but I did not find any solution to my problem.

解决方案

Firebase can only read and write fairly basic Java objects (so-called Plain Old Java Objects or POJOs) to the database. By extending your User class from Application, you're violating those rules.

The solution is to make your User class completely standalone:

public class User {
    ...

这篇关于android将对象保存在firebase中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆