There is a database with categories, each category has a list of values inside. How can I make it so that when a category is selected, a list of items that fall into the selected category is displayed? Now my category names are displayed using RecyclerView.
reference = FirebaseDatabase.getInstance().getReference().child("products"); reference.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { list = new ArrayList<ProductModel>(); for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){ // Iterable<DataSnapshot> ProductChild = dataSnapshot1.getChildren(); ProductModel p = new ProductModel(); p.setProductName(dataSnapshot1.getKey()); list.add(p); } adapter = new ProductAdapter(ProductSelectionActivity.this,list); recyclerView.setAdapter(adapter); } For example, a person chooses the category "Dairy products". And I need to display a list of products included in the selected category.
Ie I need to handle the press and somehow compare? Please tell me, because newbie, just started learning Android and the first time I work with Firebase.
Abstract class processing click on the category:
public abstract class RecyclerClickListener implements RecyclerView.OnItemTouchListener{ private GestureDetector gestureDetector;// используется, чтобы понять, произошел клик private GestureDetector.OnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent e) { return true; } }; public RecyclerClickListener(Context context) { gestureDetector = new GestureDetector(context, gestureListener); } @Override public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { if (gestureDetector.onTouchEvent(e)) { View clickedChild = rv.findChildViewUnder(e.getX(), e.getY()); // findChildViewUnder используется для определения нажатого элемента if (clickedChild != null && !clickedChild.dispatchTouchEvent(e)) { int clickedPosition = rv.getChildAdapterPosition(clickedChild); // getChildAdapterPosition - определение позиции if (clickedPosition != RecyclerView.NO_POSITION) { onItemClick(rv, clickedChild, clickedPosition); return true; } } } return false; } @Override public void onTouchEvent(RecyclerView rv, MotionEvent e) { } public abstract void onItemClick(RecyclerView recyclerView, View itemView, int position);} At the moment I have a list with categories of products: Dairy products, fish, meat, etc.
public class ProductSelectionActivity extends AppCompatActivity { private RecyclerView recyclerView; private ArrayList<ProductModel>list; private ProductAdapter adapter; private DatabaseReference reference; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_product_selection); recyclerView = (RecyclerView) findViewById(R.id.product_list); LinearLayoutManager llm = new LinearLayoutManager(this); llm.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(llm); recyclerView.setHasFixedSize(true); recyclerView.addOnItemTouchListener(new RecyclerClickListener(this) { @Override public void onItemClick(RecyclerView recyclerView, View itemView, final int position) { Toast.makeText(ProductSelectionActivity.this, "Click" , Toast.LENGTH_SHORT).show(); } @Override public void onRequestDisallowInterceptTouchEvent(boolean b) { } }); reference = FirebaseDatabase.getInstance().getReference().child("products"); reference.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { list = new ArrayList<ProductModel>(); for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){ ProductModel p = new ProductModel(); p.setProductName(dataSnapshot1.getKey()); list.add(p); } adapter = new ProductAdapter(ProductSelectionActivity.this,list); recyclerView.setAdapter(adapter); } @Override public void onCancelled(@NonNull DatabaseError databaseError) { Toast.makeText(ProductSelectionActivity.this, "Ooops", Toast.LENGTH_SHORT).show(); } }); }} And now I need to click on the category to open a list with products in this category. How to do it?
