我正在尝试构建一个应用程序,用户可以在其中发布带有图片和描述的内容,这些内容分别包含在CardView
的{{1}}中。所以我有三个问题
1。如果将RecyclerView
用作子项,然后我将其发布的数据替换为旧数据,而不是添加新数据(如果该帖子有多个具有相同ID的子代,则我的卡应显示但ID每次都被替换)
id(userId)
的{{1}}甚至被生成
尽管我进行了空检查。我用来存储孩子的帖子的早期版本是NullPointerException
,但是替换了帖子,而不是添加新的帖子,因此我使用了随机的String生成器,并尝试从中获取信息。
HomeFragment.java
getDesc()
PostAdapter.java
id(userId)
AddPost.java
public class HomeFragment extends Fragment {
private RecyclerView rv;
private TextView username,date,desc;
private CircleImageView postImage;
private ImageView postBigImage;
private CardView postCard;
private List<PostActivity> postItems;
private DatabaseReference dr;
private StorageReference sr;
private FirebaseUser user;
//FirebaseRecyclerAdapter<PostActivity, userViewHolder> fra;
private PostAdapter pa;
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
sr = FirebaseStorage.getInstance().getReference();
user = FirebaseAuth.getInstance().getCurrentUser();
String id = user.getUid();
AddPost ap = new AddPost();
dr =
FirebaseDatabase.getInstance().getReference().child("Posts")
.child(ap.getUni());
postItems = new ArrayList<>();
pa = new PostAdapter(postItems);
rv =view.findViewById(R.id.rvPostItems);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
rv.setAdapter(pa);
dr.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
//postItems.add(ds.getValue(PostActivity.class));
PostActivity pa =dataSnapshot.getValue(PostActivity.class);
postItems.add(pa);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return view;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
post_items.xml
package com.pappu5.navigation;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
public List<PostActivity> postItems;
public PostAdapter(List<PostActivity> postItems) {
this.postItems = postItems;
}
@NonNull
@Override
public PostAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_items,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final PostAdapter.ViewHolder holder, int position) {
String descTe = postItems.get(position).getDesc();
if(descTe!=null) {
holder.setDesc(descTe);
}else{
String def = "This is NULL";
holder.setDesc(def);
}
}
@Override
public int getItemCount() {
return postItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView desc;
private View mView;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setDesc(String descText){
desc = mView.findViewById(R.id.postDesc);
desc.setText(descText);
}
}
}
答案 0 :(得分:1)
要修复NullPointerException,请检查postItems.get(position)的空值,而不是postItems.get(position).getDesc(),即
@Override
public void onBindViewHolder(@NonNull final PostAdapter.ViewHolder holder, int position) {
if(postItems.get(position) != null) {
String descTe = postItems.get(position).getDesc();
holder.setDesc(descTe);
} else {
String def = "This is NULL";
holder.setDesc(def);
}
}
此外,您正在使用空白PostItems列表(即
)初始化PostAdapter。postItems = new ArrayList<>();
pa = new PostAdapter(postItems);
,稍后在ValueEventListener的onDataChange()方法中添加项目。您必须为新的更新的List postItems更新PostAdapter并通知适配器。这样。
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
//postItems.add(ds.getValue(PostActivity.class));
PostActivity pa =dataSnapshot.getValue(PostActivity.class);
postItems.add(pa);
}
pa.notifyDataSetChanged();
}
对于未显示的卡,可以共享post_items.xml文件的代码。
答案 1 :(得分:0)
(代表问题作者发布了答案)。
我解决了所有问题,方法是删除整个PostAdapter
类,然后在FireebaseRecyclerAdapter
本身中实现HomeFragment
!我的卡现在显示出魅力!感谢所有在这里回答我并帮助过我的人。