Zuhairy_Harry

Load Firebase Data

Aug 6th, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. public class DisplayAllData extends RecyclerView.Adapter<DisplayAllData.ItemViewHolder>{
  2. private List<User> mUserLsit=new ArrayList<>();
  3. private Context mContext;
  4.  
  5. @Override
  6. public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  7. View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,parent,false);
  8. return new ItemViewHolder(view);
  9. }
  10.  
  11. public DisplayAllData(Context mContext,List<User> mUserLsit) {
  12. this.mContext=mContext;
  13. this.mUserLsit = mUserLsit;
  14. }
  15.  
  16. @Override
  17. public void onBindViewHolder(ItemViewHolder holder, int position) {
  18. User user=mUserLsit.get(position);
  19. holder.mTvName.setText(user.name);
  20. holder.mTvEmail.setText(user.email);
  21. holder.mTvPwd.setText(user.pwd);
  22. }
  23.  
  24. @Override
  25. public int getItemCount() {
  26. return mUserLsit.size();
  27. }
  28.  
  29. public class ItemViewHolder extends RecyclerView.ViewHolder {
  30. TextView mTvName,mTvEmail,mTvPwd;
  31. public ItemViewHolder(View itemView) {
  32. super(itemView);
  33. mTvEmail=itemView.findViewById(R.id.rlTvEmail);
  34. mTvName=itemView.findViewById(R.id.rlTvName);
  35. mTvPwd=itemView.findViewById(R.id.rlTvPwd);
  36.  
  37. }
  38. }
  39. }
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. public class DisplayActivity extends AppCompatActivity {
  61. private RecyclerView mRvData;
  62. private DisplayAllData allDataAdapter;
  63. private DatabaseReference mDatabase;
  64. private TextView mTvEmpty;
  65. private FirebaseDatabase mFirebaseInstance;
  66. private List<User> mUserList = new ArrayList<>();
  67.  
  68. @Override
  69. protected void onCreate(@Nullable Bundle savedInstanceState) {
  70. super.onCreate(savedInstanceState);
  71. setContentView(R.layout.display_data);
  72. initView();
  73. }
  74.  
  75. private void initView() {
  76. mFirebaseInstance = FirebaseDatabase.getInstance();
  77. mDatabase = mFirebaseInstance.getReference("usersDb/UserTable");
  78. mRvData = findViewById(R.id.rvData);
  79. mTvEmpty = findViewById(R.id.dlTvEmpty);
  80. mRvData.setLayoutManager(new LinearLayoutManager(this));
  81. mDatabase.addValueEventListener(new ValueEventListener() {
  82. @Override
  83. public void onDataChange(DataSnapshot dataSnapshot) {
  84. mUserList.clear();
  85. for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
  86. User user = dataSnapshot1.getValue(User.class);
  87. mUserList.add(user);
  88. }
  89. allDataAdapter = new DisplayAllData(DisplayActivity.this, mUserList);
  90. mRvData.setAdapter(allDataAdapter);
  91. allDataAdapter.notifyDataSetChanged();
  92. if (mUserList.isEmpty())
  93. mTvEmpty.setVisibility(View.VISIBLE);
  94. else
  95. mTvEmpty.setVisibility(View.GONE);
  96. }
  97.  
  98.  
  99. @Override
  100. public void onCancelled(DatabaseError databaseError) {
  101.  
  102. }
  103. });
  104. }
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
Add Comment
Please, Sign In to add comment