Android:使用FirebaseUI读取孩子的值

时间:2018-05-30 04:55:04

标签: android firebase firebaseui

我正在使用FirebaseUI从我的实时数据库中获取一些值到Firebase RecyclerView。所以..我的数据看起来像那样:

//Check for play service availablity
int available=GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
if(available!= ConnectionResult.SUCCESS) {
     if(available== ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){
        Toast.makeText(this,"please update your google play service",Toast.LENGTH_SHORT).show();
     }
     else {
        Toast.makeText(this, "please download the google play service", Toast.LENGTH_SHORT).show();
     }
}

我不知道如何获取Name的值,这意味着我应该在Users类中做什么?这是我的代码(我认为问题出在类用户,我只是不知道如何访问子信息)

users:
      userid:
            info:
                 Name:Muhammad

我的主要活动 myDB引用和启动方法(在SUPERCILEX评论后更新)

public class User {
    private String name;
    private String email;
    private String state;
    private String image;
    private String thumbnail;

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

    public User() {
    }

    public String getName() {
        return name;
    }

    public String getState() {
        return state;
    }

    public String getImage() {
        return image;
    }
}

我的ViewHolder

mDb = FirebaseDatabase.getInstance().getReference().child(App_Constants.USERS_COLUMN);



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

        Query query = mDb;

        FirebaseRecyclerOptions<User> options = new FirebaseRecyclerOptions.Builder<User>()
                .setQuery(query, new SnapshotParser<User>() {
                    @NonNull
                    @Override
                    public User parseSnapshot(@NonNull DataSnapshot snapshot) {
                        String Name = snapshot.child(App_Constants.INFO_COLUMN).child(App_Constants.NAME_COLUMN).getValue().toString();
                        String State = snapshot.child(App_Constants.INFO_COLUMN).child(App_Constants.STATE_COLUMN).getValue().toString();
                        String Image = snapshot.child(App_Constants.INFO_COLUMN).child(App_Constants.IMAGE_COLUMN).getValue().toString();
                        User user = new User(Name,State,Image);

                        return user;
                    }
                }).build();

        FirebaseRecyclerAdapter<User,Users_ViewHolder> adapter = new FirebaseRecyclerAdapter<User, Users_ViewHolder>(options) {
            @NonNull
            @Override
            public Users_ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_user,parent,false);
                return new Users_ViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(@NonNull Users_ViewHolder holder, int position, @NonNull User model) {

                holder.Set_Name(model.getName());
                holder.Set_Image(model.getImage());
                holder.Set_State(model.getState());
            }
        };
        mUsers.setAdapter(adapter);
    }

感谢

2 个答案:

答案 0 :(得分:0)

我相信它会在Map<String, Object>字段info上出现。但是,您始终可以使用自定义SnapshotParser根据自己的喜好构建模型:https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#using-the-firebaserecycleradapter

答案 1 :(得分:0)

一些建议:您是否介意更改设置方法名称? 从np.array([32,24])Set_State,我也有类似的问题请尊重Java命名conventions

还在自定义模型中添加set方法

第二

setState

开始倾听:

 FirebaseRecyclerOptions<User> options =
                new FirebaseRecyclerOptions.Builder<User>()
                        .setQuery(query, User.class)
                        .build();

@Override protected void onStart() { super.onStart(); adapter.startListening(); } 调用会删除事件侦听器和适配器中的所有数据

stopListening()

Here你有一个非常好的解释例子