从recyclerview和firebase数据库中删除显示错误的项目

问题描述 投票:0回答:1

当我单击删除按钮时,它将删除该孩子。删除子项后,其显示错误。有什么解决方案?我尝试了各种选择,但相同的问题一次又一次地显示。谁能帮我解决这个问题?

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {

    public Context mContext;
    private List <Post>mPost;
    private FirebaseUser firebaseUser;
    ProgressBar progressBar;

    public PostAdapter(Context mContext, List<Post> mPost) {
        this.mContext = mContext;
        this.mPost = mPost;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.post_item,parent,false);


        return new PostAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {

        firebaseUser= FirebaseAuth.getInstance().getCurrentUser();
        final Post post=mPost.get(position);

        try {
            Glide.with(mContext.getApplicationContext()).load(post.getPostimage()).into(holder.post_image);

        }catch (Exception e)
        {

        }



        //holder.time.setText(post.getTime());
        Get_Time_ago getTimeAgo=new Get_Time_ago();

        long lastTime=Long.parseLong(String.valueOf(post.timestamp));
        String last_seen_time=getTimeAgo.getTimeAgo(lastTime,mContext);
       holder. time.setText(last_seen_time);
                   /*SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                   String time=sfd.format(new Date(lastseenonline));
                   mLast_seenview.setText(time);*/


        if(post.getDescription().equals("")){
            holder.description.setVisibility(View.GONE);

        }else {
            holder.description.setVisibility(View.VISIBLE);
            holder.description.setText(post.getDescription());

        }

        publisherInfo(holder.image_profile,holder.username,holder.blood,holder.publisher,post.getPublisher());


        if(post.getPublisher().equals(firebaseUser.getUid())){
            holder.delete.setVisibility(View.VISIBLE);


        }else {
            holder.delete.setVisibility(View.GONE);

        }


        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
                    alertDialog.setTitle("Confirm Delete?");
                    alertDialog.setMessage("Do you want to delete your post?");
                    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            FirebaseDatabase.getInstance().getReference("posts").child(post.getPostid()).removeValue();
                            notify();


                        }
                    });
                    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    alertDialog.show();
                }

        });


    }

    @Override
    public int getItemCount() {
        return mPost.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView image_profile,post_image,like,dislike,comment,save,delete;
        public TextView username,blood,likes,dislikes,comments, description,publisher,time;




        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            image_profile=itemView.findViewById(R.id.image_profile);
            post_image=itemView.findViewById(R.id.post_image);
            like=itemView.findViewById(R.id.like);
            comment=itemView.findViewById(R.id.comment);
            save=itemView.findViewById(R.id.save);
            delete=itemView.findViewById(R.id.delete);

            username=itemView.findViewById(R.id.username);
            blood=itemView.findViewById(R.id.blood);
            time=itemView.findViewById(R.id.time);


            likes=itemView.findViewById(R.id.likes);
            dislikes=itemView.findViewById(R.id.dislikes);
            dislike=itemView.findViewById(R.id.dislike);
            comments=itemView.findViewById(R.id.comments);
            description=itemView.findViewById(R.id.description);
            publisher=itemView.findViewById(R.id.publisher);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);


        }
    }




    private void publisherInfo (final ImageView image_profile, final TextView username,final  TextView blood, final TextView publisher, final String userId ){
        DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Users").child(userId);
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                try {
                    User user=dataSnapshot.getValue(User.class);
                    Glide.with(mContext.getApplicationContext()).load(user.getImageurl()).into(image_profile);

                    username.setText(user.getUsername());
                    blood.setText(user.getBlood());

                    publisher.setText(user.getUsername());

                }catch (Exception e){

                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

}

错误

java.lang.NullPointerException: Attempt to read from field 'long com.example.mfree.Model.Post.timestamp' on a null object reference
        at com.example.mfree.Adapter.PostAdapter.onBindViewHolder(PostAdapter.java:116)
        at com.example.mfree.Adapter.PostAdapter.onBindViewHolder(PostAdapter.java:51)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3540)
        at android.view.View.measure(View.java:23169)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:23169)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:716)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2718)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1572)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1855)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
        at android.view.Choreographer.doCallbacks(Choreographer.java:761)
2020-05-29 06:45:07.599 9312-9312/com.example.mfree E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:696)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
java firebase firebase-realtime-database android-recyclerview adapter
1个回答
0
投票

setPositiveButton onClick

代替此:

......
......
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

dialog.cancel();
FirebaseDatabase.getInstance().getReference("posts").child(post.getPostid()).removeValue();
notify();
............

尝试一下:

......
......
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

dialog.cancel();
FirebaseDatabase.getInstance().getReference("posts").child(post.getPostid()).removeValue();
mPost.remove(position);
notifyDataSetChanged();
............
© www.soinside.com 2019 - 2024. All rights reserved.