我正在使用FirebaseAdapter
,并尝试将其填充到RecyclerView
。当我调试应用程序时,它正在从数据库中检索数据,但它不会将视图填充到RecyclerView
中。我尝试了许多解决方案,但找不到任何有用的方法。请帮我解决这个问题.logcat中没有任何错误。几天前一切正常,突然消失了。
这是我的活动
public class PostsList extends AppCompatActivity {
RecyclerView recyclerView;
DatabaseReference userReference,userReference1;
LinearLayoutManager mLayoutManager;
TextView title;
ImageView new_post_btn,sign_out_btn;
String un="";
FirebaseRecyclerAdapter<PostsModel,PostsList.ViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_posts_list);
mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
sign_out_btn=findViewById(R.id.button2);
new_post_btn=findViewById(R.id.button1);
title=findViewById(R.id.activity_title);
sign_out_btn.setImageResource(R.drawable.sign_out);
new_post_btn.setImageResource(R.drawable.add);
Toolbar toolbar=findViewById(R.id.toolbar_posts_list);
setSupportActionBar(toolbar);
recyclerView = findViewById(R.id.recyclerView);
userReference= FirebaseDatabase.getInstance().getReferenceFromUrl("my reference url here").child("Posts");
userReference1= FirebaseDatabase.getInstance().getReferenceFromUrl("my reference url here").child("AccountPics");
SharedPreferences prefs = getSharedPreferences("USERNAME", MODE_PRIVATE);
String restoredText = prefs.getString("email", null);
if (restoredText != null) {
un= prefs.getString("email", null);
}
userReference1.child(un).child("name").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
name=dataSnapshot.getValue(String.class);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(),databaseError.getMessage(),Toast.LENGTH_SHORT).show();
}
});
title.setText("Dashboard");
sign_out_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(PostsList.this)
.setMessage("Do you really want to Logout?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(PostsList.this,MainActivity.class));
finish();
}})
.setNegativeButton(android.R.string.no, null).show();
}
});
new_post_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(PostsList.this,MainActivity.class);
intent.putExtra("userID",un);
startActivity(new Intent(PostsList.this,PostNewNews.class));
finish();
}
});
}
String name="";
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
@Override
protected void onStop(){
super.onStop();
adapter.stopListening();
}
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<PostsModel> options=new FirebaseRecyclerOptions.Builder<PostsModel>()
.setQuery(userReference,PostsModel.class)
.build();
adapter=new FirebaseRecyclerAdapter<PostsModel, PostsList.ViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final PostsList.ViewHolder holder, int position, @NonNull PostsModel model) {
holder.delete_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String key = getRef(holder.getAdapterPosition()).getKey();
userReference.child(key).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(),holder.post_title.getText().toString()+" Deleted Successfully",Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
});
holder.post_title.setText(model.getTitle());
holder.date.setText(model.getPost_date());
Picasso.get().load(model.getImage()).into(holder.image);
}
@NonNull
@Override
public PostsList.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.posts_list_layout,viewGroup,false);
return new ViewHolder(view);
}
};
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(adapter);
adapter.startListening();
adapter.notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView date, post_title,user_title;
ImageView image;
ImageButton delete_btn;
private final LinearLayout layout;
final LinearLayout.LayoutParams params;
private void Layout_hide() {
params.height = 0;
itemView.setLayoutParams(params); //This One.
//layout.setLayoutParams(params); //Or This one.
}
ViewHolder(@NonNull View itemView) {
super(itemView);
layout = itemView.findViewById(R.id.show_item_layout);
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
delete_btn=itemView.findViewById(R.id.delete_btn);
post_title=itemView.findViewById(R.id.post_title);
date=itemView.findViewById(R.id.day);
image=itemView.findViewById(R.id.profile_image);
}
}
}
答案 0 :(得分:0)
我发现了错误。它是XML。我在RecyclerView
中有LinearLayout
,后来又删除了LinearLayout
。由于height
具有0dp
方向,因此LinearLayout
被设置为vertical
。我更改了height
属性,它现在运行良好。谢谢大家的合作。