Ребята, я пытаюсь создать приложение, подобное Instagram. Меня поразило получение данных из базы данных и хранилища Firebase. RecyclerView ничего не показывает. Помогите мне это исправить. все данные хранятся в базе данных Firebase.
Страница адаптера Recyclerview на странице фрагмента
ProgressBar proBar;
private FirebaseAuth.AuthStateListener authStateListener;
Context context;
String cuurentUser;
FirebaseAuth firebaseAuth;
FirebaseRecyclerAdapter adapter;
RecyclerView recyclerView;
List<Post> postingList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_reviews, container, false);
PostAdapter postAdapter = new PostAdapter(context, postingList);
recyclerView = view.findViewById(R.id.recycView);
proBar = view.findViewById(R.id.proBar);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(postAdapter);
postAdapter.notifyDataSetChanged();
firebaseAuth = FirebaseAuth.getInstance();
cuurentUser = firebaseAuth.getCurrentUser().getUid();
getDetails();
return view;
}
private void getDetails() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Query query = FirebaseDatabase.getInstance().getReference().child("Posts").child(cuurentUser);
FirebaseRecyclerOptions<Post> options = new FirebaseRecyclerOptions.Builder<Post>()
.setQuery(query, new SnapshotParser<Post>() {
@NonNull
@Override
public Post parseSnapshot(@NonNull DataSnapshot snapshot) {
return new Post(snapshot.child("id").getValue().toString(),
snapshot.child("profileImg").getValue().toString(),
snapshot.child("username").getValue().toString(),
snapshot.child("postImg").getValue().toString(),
snapshot.child("description").getValue().toString(),
snapshot.child("time").getValue().toString(),
snapshot.child("date").getValue().toString(),
snapshot.child("title").getValue().toString()
);
}
})
.build();
adapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(options) {
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.post_item, parent, false);
//PostViewHolder viewHolder = new PostViewHolder(view);
return new PostViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull final PostViewHolder holder, final int position, @NonNull final Post model) {
// String uid = getRef(position).getKey();
holder.uname.setText(model.getUsername());
holder.descrip.setText(model.getDescription());
holder.ptime.setText(model.getTime());
holder.pdate.setText(model.getDate());
holder.ptitle.setText(model.getTitle());
Glide.with(getContext()).load(model.getProfileImg()).into(holder.profImg);
Glide.with(getContext()).load(model.getPostImg()).into(holder.postImage);
holder.linear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
}
};
recyclerView.setAdapter(adapter);
}
}, 2000);
}
@Override
public void onStart() {
super.onStart();
if(postingList != null)
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
public static class PostViewHolder extends RecyclerView.ViewHolder {
TextView uname, fname, descrip, ptime, pdate, ptitle;
CircleImageView profImg;
ImageView postImage;
View mview;
LinearLayout linear;
public PostViewHolder(@NonNull View itemView) {
super(itemView);
mview = itemView;
linear = itemView.findViewById(R.id.linearRoot);
uname = itemView.findViewById(R.id.username);
fname = itemView.findViewById(R.id.fullname);
descrip = itemView.findViewById(R.id.description);
ptime = itemView.findViewById(R.id.ptime);
pdate = itemView.findViewById(R.id.pdate);
ptitle = itemView.findViewById(R.id.ptitle);
profImg = itemView.findViewById(R.id.profImg);
postImage = itemView.findViewById(R.id.postImg);
}
}
страница модели как Post. java
public class Post implements Parcelable {
public Post() {
}
private String uid;
private String postImg;
private String description;
private String username;
private String date;
private String time;
private String profileImg;
private String title;
public Post(String uid, String postImg, String description, String username, String date, String time, String profileImg, String title) {
this.uid = uid;
this.postImg = postImg;
this.description = description;
this.username = username;
this.date = date;
this.time = time;
this.profileImg = profileImg;
this.title = title;
}
protected Post(Parcel in) {
uid = in.readString();
postImg = in.readString();
description = in.readString();
username = in.readString();
date = in.readString();
time = in.readString();
profileImg = in.readString();
title = in.readString();
}
public static final Creator<Post> CREATOR = new Creator<Post>() {
@Override
public Post createFromParcel(Parcel in) {
return new Post(in);
}
@Override
public Post[] newArray(int size) {
return new Post[size];
}
};
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getPostImg() {
return postImg;
}
public void setPostImg(String postImg) {
this.postImg = postImg;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getProfileImg() {
return profileImg;
}
public void setProfileImg(String profileImg) {
this.profileImg = profileImg;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(uid);
dest.writeString(postImg);
dest.writeString(description);
dest.writeString(username);
dest.writeString(date);
dest.writeString(time);
dest.writeString(profileImg);
dest.writeString(title);
}
}
xml страница с recyclerView
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TabLayout.ReviewsFragment">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycView"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/proBar"/>
</FrameLayout>
Другая xml страница для отображения деталей - это post_item. xml