Android点击CardView跳转并加载Firebase对应分类数据问题求助
Hey Tomislav, let's get this click-to-category feature working! You're already halfway there with your Firestore RecyclerView setup—we just need to add click handling to your adapter and pass the right data to your new Activity. Here's how to do it step by step:
Step 1: Add Click Listener to Your RecyclerView Items
First, we'll add an OnClickListener to each item in your FirmeAdapter's FirmeHolder. This lets us detect when a user taps a CardView (or item layout) and trigger the navigation.
Modify your FirmeHolder constructor like this:
class FirmeHolder extends RecyclerView.ViewHolder { ImageButton imageButton; TextView naziv; public FirmeHolder(@NonNull final View itemView) { super(itemView); naziv = itemView.findViewById(R.id.naziv); imageButton = itemView.findViewById(R.id.logo); // Add item click listener here itemView.setOnClickListener(v -> { // Get the position of the clicked item int position = getAdapterPosition(); // Make sure we're not dealing with an invalid position if (position != RecyclerView.NO_POSITION) { // Fetch the corresponding Firme object for the clicked item Firme clickedCategory = getItem(position); // Create an Intent to navigate to your new Activity Intent intent = new Intent(itemView.getContext(), BeautyIzbornik.class); // Pass the category data (we'll use naziv as the identifier here) intent.putExtra("SELECTED_CATEGORY", clickedCategory.getNaziv()); // Start the new Activity itemView.getContext().startActivity(intent); } }); } public void setLogo(String logo) { Picasso.get().load(logo).into(imageButton); } }
Step 2: Modify Your New Activity to Load Category-Specific Data
Now, update your BeautyIzbornik Activity to receive the category data from the Intent, then query Firestore for all items under that category.
First, create an entity class for your items (let's call it Item) with fields matching your Firestore collection, then make an adapter (ItemAdapter) similar to your FirmeAdapter. Then implement the Activity:
public class BeautyIzbornik extends AppCompatActivity { private FirebaseFirestore db = FirebaseFirestore.getInstance(); private CollectionReference itemsCollection = db.collection("your_items_collection"); // Replace with your actual items collection name private ItemAdapter itemAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_beauty_izbornik); // Get the category name passed from MainActivity String selectedCategory = getIntent().getStringExtra("SELECTED_CATEGORY"); // Set up RecyclerView with category-specific query setUpRecyclerView(selectedCategory); } private void setUpRecyclerView(String categoryName) { // Query Firestore for items where the category matches the selected one // Adjust the field name ("category") to match what's in your Firestore items Query categoryQuery = itemsCollection.whereEqualTo("category", categoryName) .orderBy("item_name", Query.Direction.ASCENDING); FirestoreRecyclerOptions<Item> options = new FirestoreRecyclerOptions.Builder<Item>() .setQuery(categoryQuery, Item.class) .build(); itemAdapter = new ItemAdapter(options); RecyclerView recyclerView = findViewById(R.id.recycler_view_items); // Replace with your RecyclerView ID recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(itemAdapter); } @Override protected void onStart() { super.onStart(); itemAdapter.startListening(); } @Override protected void onStop() { super.onStop(); itemAdapter.stopListening(); } }
Important Notes to Check
- Register Your Activity: Make sure
BeautyIzbornikis added to yourAndroidManifest.xml(Android Studio usually does this automatically when you create the Activity, but double-check). - Firestore Collection Structure: Ensure your items collection has a field (like
category) that matches thenazivvalue from yourkategorijecollection—this is how we filter items by category. - Item Entity & Adapter: Create the
Itemclass andItemAdapterjust like yourFirmeandFirmeAdapter, but with fields that match your items in Firestore (e.g., item name, description, image URL, etc.).
That should do it! When you tap a CardView in MainActivity, it'll send the category name to BeautyIzbornik, which will load and display all items under that category.
内容的提问来源于stack exchange,提问作者Tomislav Jurišić

