用户列表为空 [英] List of users is empty
问题描述
我想使用Firebase Realtime Database创建一个聊天应用程序,其中包含RecyclerView和每个用户的名字的CardView.问题是,当数据库中只有3个人时,它仅向我显示Cardview. 我在此链接中附有一张数据库图片:
I want to create a chat app using Firebase Realtime Database with a RecyclerView and a CardView with the first name for every user.The problem is that it's only show me a cardview when I have 3 people in the database. I attached in this link a picture with my database:
UsersFragment:
The UsersFragment:
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) {
}
} );
}
}
UserAdapter.在我中使用了User类:
The UserAdapter.In i used the User class:
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 );
}
}
}
存储名字的用户:
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;
}
}
还有我的TabLayout和ViewPager所在的活动
And the activity where my TabLayout and ViewPager is
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 );
}
}
}
在调试模式下,它显示给我:
In the debug mode it show me:
W/ClassMapper: No setter/field for FirstName found on class com.example.sportsbuddy.User (fields/setters are case sensitive!)
我希望向我展示我在数据库中拥有的所有用户.
I expect to show me all the users that I have in the database.
推荐答案
您在数据库中的JSON中具有属性FirstName
.在User
类中,您具有方法getFirstName()
,该方法对应于(根据Firebase使用的JavaBean约定)属性firstName
.由于情况不同,因此是不同的属性.
You have a property FirstName
in your JSON in the database. In your User
class, you have a method getFirstName()
, which corresponds (according to the JavaBean convention that Firebase uses) to a property firstName
. Since the case is different, that is a different property.
要使JSON和类匹配,可以将JSON更改为使用JavaBean属性名称:
To make the JSON and the class match, you can change the JSON to use JavaBean property names:
{
"users": {
"uid1": {
"email": "...",
"firstName": "...",
"secondName": "..."
}
}
}
或者,您可以使用PropertyName
属性使Java类与您当前的JSON相匹配:
Alternatively you can make the Java class match your current JSON by using the PropertyName
attribute:
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;
}
}
也请在此处查看我的答案:在Java中指定序列化名称Firestore文档的类
Also see my answer here: Specifying serialized names in java class for firestore document
这篇关于用户列表为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!