我正在尝试从我的数据库中获取数据以显示在RecyclerView
中。但是,它似乎没有起作用。我的代码成功填充了正确数量的行项,但是没有检索到数据。
活动:
public class Chat extends AppCompatActivity {
String username;
public RecyclerView recyclerView;
private DatabaseReference databaseReference;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat);
context = this;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
// Set up the input
final EditText input = new EditText(this);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
username = input.getText().toString();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
String key = getIntent().getStringExtra("Key");
Toast.makeText(this, key, Toast.LENGTH_LONG).show();
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference roomRef = database.getReference("rooms/" + key);
databaseReference = database.getReference("rooms").child(key);
ImageButton sendText = findViewById(R.id.send);
final EditText msg = findViewById(R.id.msg);
sendText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseReference roomRef2 = roomRef.child(String.valueOf(System.currentTimeMillis()));
Map<String, String> strings = new HashMap<>();
strings.put("user", username);
strings.put("msg", msg.getText().toString());
roomRef2.setValue(strings);
}
});
//RECYCLER VIEW
recyclerView = findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerAdapter<Msg,MsgViewHolder> adapter = new FirebaseRecyclerAdapter<Msg, MsgViewHolder>(
Msg.class, R.layout.row, MsgViewHolder.class,databaseReference
) {
@Override
protected void populateViewHolder(MsgViewHolder viewHolder, Msg model, int position) {
viewHolder.setUser(model.getUsername());
viewHolder.setMsg(model.getMessage());
Toast.makeText(context, model.getUsername(), Toast.LENGTH_LONG).show();
}
};
recyclerView.setAdapter(adapter);
}
public static class MsgViewHolder extends RecyclerView.ViewHolder {
TextView username, msg_;
public MsgViewHolder(View itemView) {
super(itemView);
username = itemView.findViewById(R.id.userTxt);
msg_ = itemView.findViewById(R.id.msgTxt);
}
public void setUser(String username) {
this.username.setText(username);
}
public void setMsg(String message) {
this.msg_.setText(message);
}
}
}
MODEL:
public class Msg {
public Msg(){
}
public Msg(String user, String msg) {
this.user = user;
this.msg = msg;
}
private String user, msg;
public String getMessage() {
return msg;
}
public void setMessage(String message) {
this.msg = message;
}
public String getUsername() {
return user;
}
public void setUsername(String username) {
this.user = username;
}
}
同样,显然有些工作正常,因为RecyclerView
填充了正确数量的行项。但是,不会检索任何数据。尝试获取用户名时,toast返回null。
非常感谢任何帮助,谢谢。
答案 0 :(得分:3)
在您的模型类中,您已更改了getter和setter默认(生成)名称,Firebase使用它们来序列化/反序列化对象。所以你的模型应该是:
public class Msg {
public Msg(){
}
public Msg(String user, String msg) {
this.user = user;
this.msg = msg;
}
private String user, msg;
public String getMsg() {
return msg;
}
public void setMsg(String message) {
this.msg = message;
}
public String getUser() {
return user;
}
public void setUser(String username) {
this.user = username;
}
}
通过使用setUsername
(以及其他变体),firebase将尝试查找名为username
的归因,但未找到它,与message
相同。这就是为什么你有正确数量的对象(Firebase能够初始化你的对象,因为你已经正确地提供了一个空的构造函数)但是他们没有任何东西(属性为null)因为setter和getter与属性名称不匹配。
您的属性为user
和msg
,因此设置者和获取者应反映:setUser
,getUser
,setMsg
,getMsg