I have an activity named Categories (categories of vehicles) that retrieves the data from firebase (ex. cars, motorcycle) and The layout is consist of recyclerview with cardview. What I want to happen is, when I click one of the categories, I want to retrieve the list of that chosen category to the next activity. My problem is, I don't know how to implement the onclick listener. here is the code for my CategoryActivty:
public class CategoryActivity extends AppCompatActivity {
DatabaseReference reference;
RecyclerView recyclerView;
ArrayList<Categories> list;
MyAdapter adapter;
BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
recyclerView = (RecyclerView) findViewById(R.id.myRecycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<Categories>();
reference = FirebaseDatabase.getInstance().getReference().child("Categories");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot:snapshot.getChildren())
{
Categories c= dataSnapshot.getValue(Categories.class);
list.add(c);
}
adapter = new MyAdapter(CategoryActivity.this,list);
recyclerView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
bottomNavigationView = findViewById(R.id.bottomNav);
bottomNavigationView.setSelectedItemId(R.id.home);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId())
{
case R.id.home:
break;
case R.id.notifications:
Intent intent1 = new Intent(HomeActivity.this,NotificationActivity.class);
startActivity(intent1);
break;
case R.id.info:
Intent intent2 = new Intent(HomeActivity.this,AboutActivity.class);
startActivity(intent2);
break;
}
return false;
}
});
}
}
This is for the Categories.java
public class Categories {
String name, image;
public Categories() {
}
public Categories(String name, String image) {
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
and this is for the Adapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
Context context;
ArrayList <Categories> list;
public MyAdapter (Context c, ArrayList <Categories> l)
{
context = c;
list = l;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.cardview1,parent,false));
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
holder.name.setText(list.get(position).getName());
Picasso.get().load(list.get(position).getImage()).into(holder.image);
}
@Override
public int getItemCount() {
return list.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView name;
ImageView image;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.categoryName);
image = (ImageView) itemView.findViewById(R.id.image1);
}
} }
please help me, it's my first time using android studio
Simple way is add onClickListener inside onBindViewHolder
holder.itemView.setOnClickListener {
// call listener or just start required activity with data
}
Because you can`t send non plain type object to Activity you must implements Categories from Parcelable and later use intent.putExtra().