我想使用Firebase Realtime Database创建一个聊天应用程序,其中包含RecyclerView和每个用户的名字的CardView。问题是,当数据库中只有3个人时,它仅向我显示Cardview。 我在此链接中附加了一张数据库图片:
UsersFragment:
.Select(z => z.Value)
UserAdapter。在我中使用了User类:
public class UsersFragment extends Fragment {
private RecyclerView recyclerView;
private UserAdapter userAdapter;
private List<User> mUsers;
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate ( R.layout.fragment_users3, container, false );
recyclerView=view.findViewById (R.id.recycler_view );
recyclerView.setLayoutManager ( new LinearLayoutManager ( getContext () ) );
mUsers=new ArrayList<> ( );
readUsers();
return view;
}
private void readUsers () {
final FirebaseUser firebaseUser= FirebaseAuth.getInstance ().getCurrentUser ();
DatabaseReference referenc= FirebaseDatabase.getInstance ().getReference ("users");
referenc.addValueEventListener ( new ValueEventListener () {
@Override
public void onDataChange (@NonNull DataSnapshot dataSnapshot) {
mUsers.clear ();
for(DataSnapshot snapshot:dataSnapshot.getChildren ()){
User user=snapshot.getValue (User.class);
assert user!=null;
assert firebaseUser!=null;
if(!firebaseUser.getUid ().equals ( user.getUid () )){
mUsers.add ( user );
}
}
userAdapter=new UserAdapter ( getContext (),mUsers );
recyclerView.setAdapter ( userAdapter );
}
@Override
public void onCancelled (@NonNull DatabaseError databaseError) {
}
} );
}
}
存储名字的用户:
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
private Context mContext;
private List<User> mUsers;
public UserAdapter(Context mContext,List<User> mUsers){
this.mUsers=mUsers;
this.mContext=mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder (@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from ( mContext ).inflate ( R.layout.users_template ,parent,false);
return new UserAdapter.ViewHolder ( view );
}
@Override
public void onBindViewHolder (@NonNull ViewHolder holder, int position) {
User user=mUsers.get ( position );
holder.username.setText ( user.getFirstName () );
}
@Override
public int getItemCount () {
return mUsers.size ();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView username;
public ViewHolder(View itemView){
super(itemView);
username=itemView.findViewById ( R.id.username1 );
}
}
}
还有我的TabLayout和ViewPager所在的活动
public class User {
public String firstName;
public String secondName;
public String uid;
public String email;
public User(){
}
public User (String firstName, String secondName, String uid, String email) {
this.firstName = firstName;
this.secondName = secondName;
this.uid=uid;
this.email=email;
}
public User (String firstname, String secondname) {
}
public String getFirstName () {
return firstName;
}
public String getSecondName () {
return secondName;
}
public String getUid () {
return uid;
}
public String getEmail () {
return email;
}
public void setFirstName (String firstName) {
this.firstName = firstName;
}
public void setSecondName (String secondName) {
this.secondName = secondName;
}
public void setUid (String uid) {
this.uid = uid;
}
public void setEmail (String email) {
this.email = email;
}
}
在调试模式下,它显示给我:
public class UsersActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_users );
TabLayout tablayout=findViewById ( R.id.tab_layout );
ViewPager viewPager=findViewById ( R.id.view_pager );
ViewPagerAdapter viewPagerAdapter=new ViewPagerAdapter ( getSupportFragmentManager () );
viewPagerAdapter.addFragment ( new ChatFragment (),"Chats" );
viewPagerAdapter.addFragment ( new UsersFragment (),"Users" );
viewPager.setAdapter (viewPagerAdapter );
tablayout.setupWithViewPager (viewPager );
}
class ViewPagerAdapter extends FragmentPagerAdapter{
private ArrayList<Fragment> fragments;
private ArrayList<String> titles;
ViewPagerAdapter(FragmentManager fn){
super(fn);
this.fragments=new ArrayList<> ( );
this.titles=new ArrayList<> ( );
}
@NonNull
@Override
public Fragment getItem (int position) {
return fragments.get ( position );
}
@Override
public int getCount () {
return fragments.size ();
}
public void addFragment(Fragment fragment,String title){
fragments.add ( fragment );
titles.add ( title );
}
@Nullable
@Override
public CharSequence getPageTitle (int position) {
return titles.get ( position );
}
}
}
我希望向我展示我在数据库中拥有的所有用户。
答案 0 :(得分:0)
将Firebase数据库中的密钥更改为firstName,secondName,email,而不是FirstName,SecondName和E-mail。如果在模型类中找不到正确的键及其getter和setter,则无法映射firebase中的数据。
答案 1 :(得分:0)
您在数据库中的JSON中具有属性FirstName
。在User
类中,有一个方法getFirstName()
,该方法对应于属性firstName
(根据Firebase使用的JavaBean约定)。由于情况不同,因此属性不同。
要使JSON和类匹配,可以将JSON更改为使用JavaBean属性名称:
{
"users": {
"uid1": {
"email": "...",
"firstName": "...",
"secondName": "..."
}
}
}
或者,您可以使用PropertyName
属性使Java类与当前JSON匹配:
public class User {
private String firstName;
private String secondName;
private String uid;
private String email;
public User(){
}
public User (String firstName, String secondName, String uid, String email) {
this.firstName = firstName;
this.secondName = secondName;
this.uid=uid;
this.email=email;
}
public User (String firstname, String secondname) {
}
@PropertyName("FirstName")
public String getFirstName () {
return firstName;
}
@PropertyName("SecondName")
public String getSecondName () {
return secondName;
}
@PropertyName("Uid")
public String getUid () {
return uid;
}
@PropertyName("E-mail")
public String getEmail () {
return email;
}
@PropertyName("FirstName")
public void setFirstName (String firstName) {
this.firstName = firstName;
}
@PropertyName("SecondName")
public void setSecondName (String secondName) {
this.secondName = secondName;
}
@PropertyName("Uid")
public void setUid (String uid) {
this.uid = uid;
}
@PropertyName("E-mail")
public void setEmail (String email) {
this.email = email;
}
}
也请在这里查看我的答案:Specifying serialized names in java class for firestore document