首页 > 解决方案 > 没有编译错误,但是当运行代码单击评论按钮时,它不会导致我想要的活动

问题描述

我做了下面的代码,使用适配器来显示列表,并使用一个按钮来向列表项添加评论,它在单击评论按钮时显示 logcat“ViewPostIme 指针 0 和 ViewPostIme 指针 1”。

编译后它没有显示任何错误,我尝试通过谷歌使用许多参考,但没有任何效果。

我的基本想法是调用带有关联列表项的评论按钮来执行评论活动。

适配器类

public class PostsAdapter extends FirestoreRecyclerAdapter<PostsModel, PostsAdapter.PostsHolder> {
    private OnItemClickListener listener;
    private View.OnClickListener buttonListener;


    private String id;

    private static final String TAG = "DocSnippets";


    public PostsAdapter(@NonNull FirestoreRecyclerOptions<PostsModel> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull PostsHolder holder, int position, @NonNull PostsModel model) {
        //retrieve the fields here
        holder.textViewDescription.setText(model.getPostContent());
        holder.textViewPriority.setText(String.valueOf(model.getSpinnerC()));
        holder.textViewPriority.setText(String.valueOf(model.getTimestamp()));
    }

    @NonNull
    @Override
    public PostsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_list_layout,
                parent, false);
        return new PostsHolder(v);
    }

    public void deleteItem(int position) {
        getSnapshots().getSnapshot(position).getReference().delete();
    }

    public void setOnClickListener(OnClickListener postKey) {
    }


    class PostsHolder extends RecyclerView.ViewHolder {

        //first declare here the elements to be displayed in the cardview.
        TextView textViewTitle;
        TextView textViewDescription;
        TextView textViewPriority;

        Button commentsbutton;

        public PostsHolder(final View itemView) {
            super(itemView);
            textViewTitle = itemView.findViewById(R.id.post_etPostTitle);
            textViewDescription = itemView.findViewById(R.id.post_description);
            textViewPriority = itemView.findViewById(R.id.post_time);
             commentsbutton = itemView.findViewById(R.id.commenting_button);


            commentsbutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION && buttonListener != null) {
                        buttonListener.onClick(itemView);

                    }

                }
            });
        }
    }

    public interface OnClickListener
    {
        void OnClickListener(DocumentSnapshot documentSnapshot, int position);

    }
    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.buttonListener = onClickListener;
    }


    public interface OnItemClickListener {
        void onItemClick(DocumentSnapshot documentSnapshot, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}

片段类

public class HomeFragment extends Fragment {


    RelativeLayout mParent;
    //FloatingActionButton addButton;

    private static final String TAG = "DocSnippets";

    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference PostsRef = db.collection("posts");

    private PostsAdapter adapter;
    private FirestoreRecyclerOptions options;
    private FirebaseAuth mAuth;
    private String mUserId, id;

    private Button commentsbutton;

    RecyclerView recyclerView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //just change the fragment_dashboard
        //with the fragment you want to inflate
        //like if the class is HomeFragment it should have R.layout.home_fragment
        //if it is DashboardFragment it should have R.layout.fragment_dashboard
       View view = inflater.inflate(R.layout.fragment_home, container, false);
        final FragmentActivity c = getActivity();
        LinearLayoutManager layoutManager = new LinearLayoutManager(c);

        Query query = PostsRef.orderBy("timestamp", Query.Direction.DESCENDING);
        FirestoreRecyclerOptions<PostsModel> options = new FirestoreRecyclerOptions.Builder<PostsModel>()
                .setQuery(query, PostsModel.class)
                .build();
        adapter = new PostsAdapter(options);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
             recyclerView.setLayoutManager(new LinearLayoutManager(c));
        recyclerView.setAdapter(adapter);
            commentsbutton = (Button) view.findViewById(R.id.commenting_button);

         mParent =view.findViewById(R.id.relative_home);
       mAuth = FirebaseAuth.getInstance();
        mUserId = mAuth.getUid();



        adapter.setOnItemClickListener(new PostsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
                PostsModel note = documentSnapshot.toObject(PostsModel.class);
                 id = documentSnapshot.getId();
                String path = documentSnapshot.getReference().getPath();
                Log.d(TAG, "String post Id is: " + id);


            }
        });

        adapter.setOnClickListener(new PostsAdapter.OnClickListener() {
            @Override
            public void OnClickListener(DocumentSnapshot documentSnapshot, int position) {

                PostsModel note = documentSnapshot.toObject(PostsModel.class);
                id = documentSnapshot.getId();
                String path = documentSnapshot.getReference().getPath();
                Log.d(TAG, "String post Id is: " + id);
                Intent toCommentActivity = new Intent(getContext(), CommentActivity.class);

                 toCommentActivity.putExtra("PostKey", id);

                getContext().startActivity(toCommentActivity);


            }
        });







        return view;



    }


    private String getTime(long timestamp){
        long ts = timestamp*1000;
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
        String time = sdf.format(new Date(ts));
        return time;

    }

    @Override
    public void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
    }

}

标签: javaandroidfirebasegoogle-cloud-firestoreadapter

解决方案


推荐阅读