Android 是否可以从该“文件”中获取数据;“用户”;反对这个布局吗?
我正在制作这个简单的聊天应用程序,任何人都可以注册他的名字、电子邮件和通行证,然后登录并开始聊天 成功地从DB发送和接收消息,现在我遇到的问题是“如何在聊天版面中获取相应人员的用户名” 在注册过程中,我还将用户名和电子邮件保存到“用户”节点内的数据库中 这就是我迄今为止所尝试的: 这是我的数据库截图: 到目前为止,应用程序的外观如下: 以下是我的注册活动:Android 是否可以从该“文件”中获取数据;“用户”;反对这个布局吗?,android,firebase-realtime-database,Android,Firebase Realtime Database,我正在制作这个简单的聊天应用程序,任何人都可以注册他的名字、电子邮件和通行证,然后登录并开始聊天 成功地从DB发送和接收消息,现在我遇到的问题是“如何在聊天版面中获取相应人员的用户名” 在注册过程中,我还将用户名和电子邮件保存到“用户”节点内的数据库中 这就是我迄今为止所尝试的: 这是我的数据库截图: 到目前为止,应用程序的外观如下: 以下是我的注册活动: private void DOSignup() { final String get_name = name.g
private void DOSignup() {
final String get_name = name.getText().toString().trim();
final String get_email = email.getText().toString().trim();
String get_password = password.getText().toString().trim();
mAuth.createUserWithEmailAndPassword(get_email,get_password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser firebaseUser = mAuth.getCurrentUser();
Log.e("Current User", firebaseUser.toString());
// String name = firebaseUser.getDisplayName();
// Log.e("Name is :", name);
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
GetUserName getUserName = new GetUserName(get_name,get_email);
String id = databaseReference.push().getKey();
databaseReference.child(id).setValue(getUserName);
Toast.makeText(getApplicationContext(), "Registration successful!", Toast.LENGTH_LONG).show();
// progressBar.setVisibility(View.GONE);
Intent intent = new Intent(SignUpActivity.this, LoginActivity.class);
startActivity(intent);
}
else {
Toast.makeText(getApplicationContext(), "Registration failed! Please try again later", Toast.LENGTH_LONG).show();
// progressBar.setVisibility(View.GONE);
}
}
});
}
这是我用来显示消息的模型类:
public class ChatModel {
String messages;
public ChatModel() {
}
public ChatModel(String messages) {
this.messages = messages;
}
public String getMessages() {
return messages;
}
}
这是我的chat_layout.xml:
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="Lacie"
android:textSize="18sp" />
<TextView
android:id="@+id/dateTime"
android:layout_margin="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="07-10-2016 (21:50:21)" />
</RelativeLayout>
<TextView
android:id="@+id/message_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/l1"
android:background="@drawable/my_message"
android:elevation="2dp"
android:padding="10dp"
android:text="Placeholder message"
android:textColor="#fff"
android:textSize="13sp" />
请给出您的想法,我如何在聊天室布局中获得相应的用户名而不是“Lacie”。谢谢你试过什么?您需要能够读取数据。你还必须确保你已经为节点制定了规则。lacie甚至不在数据库中,对吗?您显示的屏幕截图只不过是一个视图。您显示了获取数据并将其存储到列表中,然后将列表放入适配器的0代码。。。你不知道这个。您的登录代码与获取数据无关。我也添加了适配器代码,顺便说一句,“Lacie”是硬编码的,请再次查看xml@soldForb,但我看不到您从firebase获取的代码?你有吗?另外,你显示的firebase代码看起来不像聊天节点,而是用户节点。你在这里表现得不够好。。。
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="Lacie"
android:textSize="18sp" />
<TextView
android:id="@+id/dateTime"
android:layout_margin="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="07-10-2016 (21:50:21)" />
</RelativeLayout>
<TextView
android:id="@+id/message_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/l1"
android:background="@drawable/my_message"
android:elevation="2dp"
android:padding="10dp"
android:text="Placeholder message"
android:textColor="#fff"
android:textSize="13sp" />
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_layout,
parent, false);
return new ViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull ChatModel model) {
ChatModel model1 = chatModelList.get(position);
GetUserName getUserName = new GetUserName();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
holder.messages.setText(model1.getMessages());
holder.userName.setText(databaseReference.getKey());
// holder.userName.setText(getUserName.getUsername());
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView messages,userName;
public ViewHolder(@NonNull View itemView) {
super(itemView);
messages = itemView.findViewById(R.id.message_body);
userName = itemView.findViewById(R.id.userName);