【发布时间】:2017-08-13 20:48:07
【问题描述】:
我正在尝试创建一个基本应用程序,在该应用程序中,我将消息发送到 Firebase 中的实时数据库并在列表视图中接收它们。(它是 Whatsapp 的翻版。)我发送用户的用户名和消息通过推送消息到数据库。但是,由于随机生成的密钥,我无法读取它们并从中创建列表视图。 有没有办法接收参考聊天的所有孩子并将它们放在列表视图中?
ps。我不能使用 .getKey 函数,因为消息将来自不同的设备。
pps。 可以提供额外的代码或详细信息。
代码:
public class MessageBoard extends AppCompatActivity {
private static final String TAG = "MessageBoard";
private FirebaseAuth mAuth= FirebaseAuth.getInstance();
private FirebaseDatabase database = FirebaseDatabase.getInstance();
private FirebaseUser user = mAuth.getCurrentUser();
private ArrayList<String> chatlist;
private ArrayAdapter<String> arrayAdapter;
private DatabaseReference ref;
private DatabaseReference Users;
private String username;
private String email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_board);
final Button SignOut = (Button) findViewById(R.id.SignOut);
final Button SendMessage = (Button) findViewById(R.id.SendMessage);
final EditText MessageText = (EditText) findViewById(R.id.Message_Text);
final ListView listView = (ListView) findViewById(R.id.Listview);
final TextView UserNameDis = (TextView) findViewById(R.id.UserNameDisplay);
if(user != null){
email = user.getEmail();
}
chatlist = new ArrayList<String>();
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, chatlist);
listView.setAdapter(arrayAdapter);
ref = database.getReference("chat");
//-----------------------------------------------------------------------------------------------------------------------------------------
//to login screen
if(user == null){
startActivity(new Intent(MessageBoard.this, Login.class));
Log.d(TAG, "Login");
}else{
Toast.makeText(MessageBoard.this, "Login Succesfull.",
Toast.LENGTH_SHORT).show();
}
//-----------------------------------------------------------------------------------------------------------------------------------------
//to username screen
if(user.getDisplayName() == null){
startActivity(new Intent(MessageBoard.this, Username.class));
Log.d(TAG, "Username");
}else{
Toast.makeText(MessageBoard.this, "Username Known.",
Toast.LENGTH_SHORT).show();
}
//-----------------------------------------------------------------------------------------------------------------------------------------
//set username welcome text
username = user.getDisplayName();
String Welcome = "Welcome " + username;
UserNameDis.setText(Welcome);
//-----------------------------------------------------------------------------------------------------------------------------------------
//sign out button
SignOut.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(MessageBoard.this,Login.class));
}
});
//-----------------------------------------------------------------------------------------------------------------------------------------
// chat data update
ref.orderByKey();
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//where I think the receiving code should be
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//-----------------------------------------------------------------------------------------------------------------------------------------
//message send
SendMessage.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
String push = ref.push().getKey();
ref.child(push).child("message").setValue(username);
ref.child(push).child("username").setValue(username);
}
});
//-----------------------------------------------------------------------------------------------------------------------------------------
}
}
【问题讨论】:
标签: android firebase firebase-realtime-database