Reputation: 431
I am trying to get my data from my database to show in a RecyclerView
. However, it does not seem to be working. My code DOES succeed in populating the correct number of row items, however the data is not retrieved.
ACTIVITY:
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;
}
}
Again, clearly something is working as the RecyclerView
is populated with the correct number of row items. However, no data is retrieved. The toast returns null when trying to get the usernames.
Any help is greatly appreciated, thanks.
Upvotes: 2
Views: 746
Reputation: 12005
In your model class, you've changed the getter and setter default (generated) names, and Firebase uses them to serialize/deserialize objects. So your model should be:
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;
}
}
By using setUsername
(and the other variations) firebase will try to find an attributed called username
but won't find it, the same with message
. That's why you had the correct number of objects (Firebase was able to initialize your object because you've correctly provided an empty constructor) but they didn't have anything in them (the attributes were null) because the setters and getters didn't match the attributes names.
Your attributes are user
and msg
, so the setters and getters should reflect that: setUser
, getUser
, setMsg
, getMsg
.
Upvotes: 3