【发布时间】:2017-05-25 21:35:49
【问题描述】:
我正在尝试显示位于My Database tree 中的所有子“名称”。我使用了@Alex Mamo Unable to get All child data in Firebase Database using a Map?给出的这个答案 我想将所有数据显示到 recyclerView 中。但由于某些原因,我只能在我的屏幕手机上看到所有名字(Eva、smith、公主),而我只能看到一个“公主”,它在我的 recyclerView 布局中显示了 3 次(公主、公主、公主)。在玩弄了亚历克斯的答案之后,我发现这对我来说非常有效,
DatabaseReference yourRef = FirebaseDatabase.getInstance().getReference().child("Users");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
String name = dataSnapshot1.child("name").getValue().toString();
displayName.setText(name);// display on the screen
Toast.makeText(context, name, Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);
}
因此,由于此答案在 Toast 中正确地为我提供了所有名称,但在我的屏幕上却没有,因此我认为我的 FirebaseRecyclerAdpater 有问题。到目前为止,这是我的代码,
FirebaseRecyclerAdapter<UsersPostClass, UsersViewHolder> userList = new FirebaseRecyclerAdapter<UsersPostClass, UsersViewHolder>(
UsersPostClass.class,
R.layout.custom,
UsersViewHolder.class,
mDatabaseRef
) {
@Override
protected void populateViewHolder( UsersViewHolder viewHolder, UsersPostClass model, int position) {
viewHolder.setUsernameList(getApplicationContext());
}
};
mRecyclerView.setAdapter(userList);
}
private static class UsersViewHolder extends RecyclerView.ViewHolder {
View mView;
public UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setUsernameList(final Context context) {
final TextView displayName = (TextView) mView.findViewById(R.id.listViewUsername);
DatabaseReference yourRef = FirebaseDatabase.getInstance().getReference().child("Users");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
String name = dataSnapshot1.child("name").getValue().toString();
displayName.setText(name);
Toast.makeText(context, name, Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
yourRef.addListenerForSingleValueEvent(eventListener);
}
}
欢迎任何帮助或提示。 编辑:添加我的布局 这是我的 RecyclerView 布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="My Players List"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textSize="20sp"
android:layout_marginBottom="10dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
最后是我想要显示所有名称的自定义布局
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="@android:color/transparent"
card_view:cardElevation="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
<TextView
android:id="@+id/listViewUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center_vertical"
android:text="Username"
android:textColor="#aaa"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
只是为了了解我在说什么,这是我应该得到的东西(公主、公主、公主)的截图(伊娃、史密斯、公主)。 My ScreenShot 如果您仔细观察,您会看到 Toast 正确返回“Eva”和其他 2 个名称,但屏幕上没有。
【问题讨论】:
-
您能提供给我们您的 .XML 文件吗?
-
@AlexMamo 添加了请再看一遍
-
尝试使用
LinearLayout更改您的Custom Layout和RelativeLayout,并将layout_width和layout_height设置为wrap_content,如下所示:android:layout_width="wrap_content"和android:layout_height="wrap_content"。有用吗? -
不。顺便说一句,我添加了一张图片,只是为了让您直观地了解我在说什么。感谢您尝试提供帮助
-
尝试在您的
FirebaseRecyclerAdapter中将UsersPostClass更改为String类。
标签: android firebase firebase-realtime-database firebaseui