If I click on RecyclerView
item I should get toast category id.
I've tried many times but I was not able to get a toast.
I'm using api. If I get this toast it will be very simple for me to see id.
Toast.makeText(v.getContext(), "Item is clicked", Toast.LENGTH_SHORT).show();
I don't get id from these I'm new Android Developer
adapterRecipes.setOnItemClickListener((v, obj, position) -> {
Intent intent = new Intent(getApplicationContext(), ActivityRecipeDetail.class);
intent.putExtra(Constant.EXTRA_OBJC, obj);
Toast.makeText(v.getContext(), "Item is clicked", Toast.LENGTH_SHORT).show();
startActivity(intent);
adsManager.showInterstitialAd();
});
adapter
package com.app.yourrecipesapp.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.app.yourrecipesapp.R;
import com.app.yourrecipesapp.config.AppConfig;
import com.app.yourrecipesapp.databases.prefs.SharedPref;
import com.app.yourrecipesapp.models.Category;
import com.balysv.materialripple.MaterialRippleLayout;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
public class AdapterCategory extends RecyclerView.Adapter<AdapterCategory.ViewHolder> {
private List<Category> items;
private Context context;
private OnItemClickListener mOnItemClickListener;
SharedPref sharedPref;
public interface OnItemClickListener {
void onItemClick(View view, Category obj, int position);
}
public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mOnItemClickListener = mItemClickListener;
}
// Provide a suitable constructor (depends on the kind of dataset)
public AdapterCategory(Context context, List<Category> items) {
this.items = items;
this.context = context;
this.sharedPref = new SharedPref(context);
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView category_name;
public TextView recipe_count;
public ImageView category_image;
public MaterialRippleLayout lyt_parent;
public ViewHolder(View v) {
super(v);
category_name = v.findViewById(R.id.category_name);
recipe_count = v.findViewById(R.id.video_count);
category_image = v.findViewById(R.id.category_image);
lyt_parent = v.findViewById(R.id.lyt_parent);
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_category, parent, false);
return new ViewHolder(v);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final Category c = items.get(position);
holder.category_name.setText(c.category_name);
if (AppConfig.ENABLE_RECIPE_COUNT_ON_CATEGORY) {
holder.recipe_count.setVisibility(View.VISIBLE);
holder.recipe_count.setText(c.recipes_count + " " + context.getResources().getString(R.string.recipes_count_text));
} else {
holder.recipe_count.setVisibility(View.GONE);
}
Picasso.get()
.load(sharedPref.getApiUrl() + "/upload/category/" + c.category_image)
.placeholder(R.drawable.ic_thumbnail)
.into(holder.category_image);
holder.lyt_parent.setOnClickListener(view -> {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(view, c, position);
}
});
}
public void setListData(List<Category> items){
this.items = items;
notifyDataSetChanged();
}
public void resetListData() {
this.items = new ArrayList<>();
notifyDataSetChanged();
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return items.size();
}
}
category.java
package com.app.yourrecipesapp.models;
import java.io.Serializable;
public class Category implements Serializable {
public int cid = -1;
public String category_name;
public String category_image;
public String recipes_count;
}