【发布时间】:2018-06-14 23:26:33
【问题描述】:
我想将 onClickListener 添加到 recyclerview 中显示来自 firestore 的数据的项目。 OnClick 应该通过 intent 传递相应的文档 id。请帮忙
-
ProductFragment显示数据
public class ProductFragment extends Fragment { private static final String TAG = "ProductFragment"; private FirebaseFirestore firestoreDB; private RecyclerView productRecyclerView; public ProductFragment() { // 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_product, container, false); firestoreDB = FirebaseFirestore.getInstance(); productRecyclerView = (RecyclerView) view.findViewById(R.id.Product_RecyclerView); LinearLayoutManager recyclerLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext()); productRecyclerView.setLayoutManager(recyclerLayoutManager); DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(productRecyclerView.getContext(), recyclerLayoutManager.getOrientation()); productRecyclerView.addItemDecoration(dividerItemDecoration); getDocumentsFromCollection(); return view; } @Override public void onAttach(Context context) { super.onAttach(context); } public void getDocumentsFromCollection() { firestoreDB.collection("products").get() .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task) { if (task.isSuccessful()) { //List<Event> eventList = new ArrayList<>(); List<DocumentSnapshot> documents = task.getResult().getDocuments(); ProductAdapter recyclerViewAdapter = new ProductAdapter(documents, getActivity(), firestoreDB); recyclerViewAdapter.setOnItemClickListener(new ProductAdapter.ClickListener() { @Override public void onItemClick(int position, View v) { Log.d(TAG, "onItemClick position: " + position); // Go to the details page for the selected product } }); productRecyclerView.setAdapter(recyclerViewAdapter); } else { Log.d(TAG, "Error getting documents: ", task.getException()); } } }); firestoreDB.collection("products") .addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() { @Override public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) { } }); } } -
适配器
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder>{ public static ClickListener clickListener; public List<DocumentSnapshot> documents; private Context context; private FirebaseFirestore firestoreDB; public ProductAdapter(List<DocumentSnapshot> list, Context ctx, FirebaseFirestore firestore) { documents = list; context = ctx; firestoreDB = firestore; } @Override public int getItemCount() { return documents.size(); } @Override public ProductAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.list_layout, parent, false); ProductAdapter.ViewHolder viewHolder = new ProductAdapter.ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(ProductAdapter.ViewHolder holder, int position) { final int itemPos = position; final DocumentSnapshot snapshot = documents.get(position); holder.item_name.setText(snapshot.getString("Product")); holder.price.setText("Rs " + snapshot.getString("Cost") + "/" + snapshot.getString("Per")); } public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView item_name; public TextView price; public ViewHolder(View view) { super(view); itemView.setOnClickListener(this); item_name = view.findViewById(R.id.List_maintext); price = view.findViewById(R.id.List_subtext); } @Override public void onClick(View v) { clickListener.onItemClick(getAdapterPosition(), v); } } public void setOnItemClickListener(ClickListener clickListener) { ProductAdapter.clickListener = clickListener; } public interface ClickListener { void onItemClick(int position, View v); } }
我尝试了一些方法,但都崩溃了。 我需要显示所点击项目的详细视图
【问题讨论】:
标签: android firebase android-recyclerview google-cloud-firestore