我正在一个项目中,我需要从Firebase实时数据库中检索多个用户的数据,并且每个用户都是经过身份验证的用户,即每个用户数据都存储在经过身份验证的密钥下。我想将数据检索到recyclerview中。
这是个人资料页面的代码
public class Profile extends AppCompatActivity {
DatabaseReference databaseReference;
ProgressDialog progressDialog;
List<User> list = new ArrayList<User>();
RecyclerView recyclerView;
Firebase firebase;
FirebaseUser firebaseUser;
RecyclerView.Adapter adapter ;
FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_detail);
mAuth = FirebaseAuth.getInstance();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(Search_Profile.this));
progressDialog = new ProgressDialog(Search_Profile.this);
progressDialog.setMessage("Loading Data from Firebase Database");
progressDialog.show();
databaseReference = FirebaseDatabase.getInstance().getReference().child(Dashboard.Database_Path);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
User user = dataSnapshot.getValue(User.class);
list.add(user);
}
adapter = new RecyclerViewAdapter(Search_Profile.this,list);
recyclerView.setAdapter(adapter);
progressDialog.dismiss();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
}
这是适配器的代码
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
private List<User> MainImageUploadInfoList;
RecyclerViewAdapter(Context context, List<User> TempList) {
this.MainImageUploadInfoList = TempList;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
User user = MainImageUploadInfoList.get(position);
holder.FirstNameTextView.setText(user.getFirst_name());
holder.LastNameTextView.setText(user.getLast_name());
}
@Override
public int getItemCount() {
return MainImageUploadInfoList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView FirstNameTextView;
public TextView LastNameTextView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
FirstNameTextView = (TextView) itemView.findViewById(R.id.textView1);
LastNameTextView = (TextView) itemView.findViewById(R.id.textView2);
}
}
}
这是用户代码
public class User {
private String first_name;
private String last_name;
private String date;
private String gender;
private String height;
private String city_state;
private String phone_no;
private String hobbies;
private String highest_education;
private String occupation;
private String income;
private String marital_status;
private String family_members;
private String fathers_name;
private String mothers_name;
private String fathers_occupation;
private String mothers_occupation;
private String siblings_names;
private String age;
private String height1;
private String marital_status1;
private String city_state1;
private String education1;
private String occupation1;
private String income1;
private String email_id;
private String user_id;
public User() {
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getCity_state() {
return city_state;
}
public void setCity_state(String city_state) {
this.city_state = city_state;
}
public String getPhone_no() {
return phone_no;
}
public void setPhone_no(String phone_no) {
this.phone_no = phone_no;
}
public String getHobbies() {
return hobbies;
}
public void setHobbies(String hobbies) {
this.hobbies = hobbies;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
public String getIncome() {
return income;
}
public void setIncome(String income) {
this.income = income;
}
public String getMarital_status() {
return marital_status;
}
public void setMarital_status(String marital_status) {
this.marital_status = marital_status;
}
public String getFamily_members() {
return family_members;
}
public void setFamily_members(String family_members) {
this.family_members = family_members;
}
public String getFathers_name() {
return fathers_name;
}
public void setFathers_name(String fathers_name) {
this.fathers_name = fathers_name;
}
public String getMothers_name() {
return mothers_name;
}
public void setMothers_name(String mothers_name) {
this.mothers_name = mothers_name;
}
public String getFathers_occupation() {
return fathers_occupation;
}
public void setFathers_occupation(String fathers_occupation) {
this.fathers_occupation = fathers_occupation;
}
public String getMothers_occupation() {
return mothers_occupation;
}
public void setMothers_occupation(String mothers_occupation) {
this.mothers_occupation = mothers_occupation;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getHeight1() {
return height1;
}
public void setHeight1(String height1) {
this.height1 = height1;
}
public String getMarital_status1() {
return marital_status1;
}
public void setMarital_status1(String marital_status1) {
this.marital_status1 = marital_status1;
}
public String getCity_state1() {
return city_state1;
}
public void setCity_state1(String city_state1) {
this.city_state1 = city_state1;
}
public String getEducation1() {
return education1;
}
public void setEducation1(String education1) {
this.education1 = education1;
}
public String getOccupation1() {
return occupation1;
}
public void setOccupation1(String occupation1) {
this.occupation1 = occupation1;
}
public String getIncome1() {
return income1;
}
public void setIncome1(String income1) {
this.income1 = income1;
}
public String getEmail_id() {
return email_id;
}
public void setEmail_id(String email_id) {
this.email_id = email_id;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getHighest_education() {
return highest_education;
}
public void setHighest_education(String highest_education) {
this.highest_education = highest_education;
}
public String getSiblings_names() {
return siblings_names;
}
public void setSiblings_names(String siblings_names) {
this.siblings_names = siblings_names;
}
}
These are the fields under the authenticated user
This is the link of last page for storing the data into firebase