i'm create recycler view which fetching data from web server by retrofit , my problem when i want fetching image by picasso i have error message
java.lang.IllegalArgumentException: Context must not be null.
i don't know why context is null i'm add the picasso in onBindViewHolder
my adapter
public class myAdapter extends RecyclerView.Adapter<myAdapter.ViewHolder> {
private List<Listitem>listitems;
private Context context;
public myAdapter(List<Listitem> listitems, Context context) {
this.listitems = listitems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Listitem listitem = listitems.get(position);
holder.textTitile.setText(listitem.getTitel());
holder.textDesc.setText(listitem.getDesc());
Picasso.with(context)
.load(listitem.getImageurl())
.placeholder(R.color.colorAccent)
.into(holder.movieThumbnail);
}
#Override
public int getItemCount() {
return listitems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView textTitile ;
public TextView textDesc;
public ImageView movieThumbnail;
public ViewHolder(View itemView) {
super(itemView);
textTitile=(TextView) itemView.findViewById(R.id.Titell);
textDesc=(TextView)itemView.findViewById(R.id.desc);
movieThumbnail = (ImageView) itemView.findViewById(R.id.movie_thumbnail);
}
}
}
You Context is null, Any way you can use view context also ex : holder.itemView.getContext()
You can check You are sending not null or you can use blow approach.
So you can change as
Picasso.with(holder.itemView.getContext())
.load(listitem.getImageurl())
.placeholder(R.color.colorAccent)
.into(holder.movieThumbnail);
Related
RecyclerView items not visible
I am using a string array name item test and pass it to String array list, I used the same idea before and was working correctly
listItemsTest = new ArrayList<>(Arrays.asList(itemsTest));
recyclerView = findViewById(R.id.RecycleViewTest);
recycleViewAdapterTest = new RecycleViewAdapterTest(this,listItemsTest);
recyclerView.setAdapter(recycleViewAdapterTest);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
My Adapter
public class RecycleViewAdapterTest extends
RecyclerView.Adapter<RecycleViewAdapterTest.MyViewHolder> {
private ArrayList<String> ListTest;
Context context;
public RecycleViewAdapterTest(Context context, ArrayList<String> listTest) {
this.ListTest = listTest;
this.context = context;
}
#NonNull
#Override
public RecycleViewAdapterTest.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_list,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull RecycleViewAdapterTest.MyViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return ListTest.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
LinearLayout linearLayout;
TextView textView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
linearLayout = itemView.findViewById(R.id.LLTest);
textView = itemView.findViewById(R.id.textViewTest);
}
}
#Override
public void onBindViewHolder(#NonNull RecycleViewAdapterTest.MyViewHolder holder, int position) {
String value = this.ListTest.get(position);
holder.textView.setText(value);// Try to make the textView public
}
because you need to set list value to viewholder's textview in OnBindViewHolder. :(
I have a recyclerview in ShoppingCartActivity and a button in ProductDetailActivity. I want to send some data to ShoppingCartActivity when a button clicked in ProductDetailActivity and display them in recyclerview item that creates dynamically in ShoppingCartActivity.
I added addData method to recyclerview Adapter but it doesn't work and app crashes.
shop_btn = findViewById(R.id.add_to_shopping_cart);
shop_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent addToCart = new Intent(ProductDetailActivity.this, ShoppingCartActivity.class);
startActivity(addToCart);
}
});
//HERE IS MY RECYCLERVIEW ADAPTER
public class RecyclerViewAdapter_ShoppingCart extends RecyclerView.Adapter<RecyclerViewAdapter_ShoppingCart.MyViewHolder> {
private Context context;
List<CartItems> cartItemsList;
public RecyclerViewAdapter_ShoppingCart(Context context, List<CartItems> cartItemsList) {
this.context = context;
this.cartItemsList = cartItemsList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.card_cart, parent, false);
return new MyViewHolder(view);
}
#NonNull
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, final int position) {
holder.cart_title.setText(cartItemsList.get(position).getTitle());
holder.cart_price.setText(cartItemsList.get(position).getPrice());
Picasso.get().load(cartItemsList.get(position).getImageUrl()).into(holder.cart_img);
}
#Override
public int getItemCount() {
return cartItemsList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView cart_title, cart_price;
ImageView cart_img;
public MyViewHolder(View itemView) {
super(itemView);
cart_title = itemView.findViewById(R.id.cart_title);
cart_price = itemView.findViewById(R.id.cart_price);
//cart_delete = itemView.findViewById(R.id.cart_delete);
cart_img = itemView.findViewById(R.id.cart_image);
}
}
public void addItem(List<CartItems> cartItems) {
CartItems newValue = new CartItems();
newValue.setTitle("123");
newValue.setPrice("123");
newValue.setImageUrl("");
cartItems.add(newValue);
notifyDataSetChanged();
}
}
My goal is adding recyclerview items dynamically to ShoppingCartActivity when a button clicked in ProductDetailActivity but app crashes.
Which is the code to obtain a Horizontal RecyclerView of images? I already have the code for horizontal RecyclerView of Strings. I need to have the same but of Images.
I have to create a RecyclerView of 10 horizontal scrollable imageView.
This is my actual code of 10 horizontal TextViews:
public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.HorizontalViewHolder> {
private String[] items;
public HorizontalAdapter(String[] items) {
this.items = items;
}
#Override
public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_layout_recycler_view_top_10, parent, false);
return new HorizontalViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalViewHolder holder, int position) {
holder.text.setText(items[position]);
}
#Override
public int getItemCount() {
return items.length;
}
public class HorizontalViewHolder extends RecyclerView.ViewHolder {
TextView text;
public HorizontalViewHolder(View itemView) {
super(itemView);
text = itemView.findViewById(R.id.text_view_top_10_ricette_titolo_ricetta);
} }
}
and I manage it on a Fragment in this way:
RecyclerView list = (RecyclerView) view.findViewById(R.id.recycler_view_ricette_categorie_primi_top_10);
list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
list.setAdapter(new HorizontalAdapter(new String[]{"Android","Programming","Java","RecyclerView","Layout"}));
How I have to modify this?
Try this make below changes in your HorizontalAdapter
public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.HorizontalViewHolder> {
private int[] items;
public HorizontalAdapter(String[] int) {
this.items = items;
}
#Override
public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_layout_recycler_view_top_10, parent, false);
return new HorizontalViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalViewHolder holder, int position) {
holder.image.setImageResource(items[position]);
}
#Override
public int getItemCount() {
return items.length;
}
public class HorizontalViewHolder extends RecyclerView.ViewHolder {
ImageView image;
public HorizontalViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.imageviewid);
} }
}
code for RecyclerView
RecyclerView list = (RecyclerView) view.findViewById(R.id.recycler_view_ricette_categorie_primi_top_10);
list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
list.setAdapter(new HorizontalAdapter(new int[]{R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher}));
NOTE : Your layout (item_layout_recycler_view_top_10) needs to contain a ImageView instead of a TextView
Instead of a recyclerView full of textViews (R.layout.item_layout_recycler_view_top_10) you want images?
Just create another layout which contains an ImageView instead of a TextView in it and use it accordingly to your current solution.
String array change to image url array or drawable image array create and send to adapter and set into imageview.
I'm trying to delete an item from my RecyclerView when the user clicks on it. However, even though the delete method in the Adapter is public I get the error 'Cannot resolve method delete(int)' when trying to call 'delete' method from the onClick in MyViewHolder.
What gives? It's public so why can't I call it?
GroceryItemAdapter
public class GroceryItemAdapter extends RecyclerView.Adapter<MyViewHolder> {
private LayoutInflater inflater;
private Context context;
List<MetaData> data = Collections.emptyList();
public GroceryItemAdapter(Context context, List<MetaData> data) {
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
}
public void delete(int position) {
data.remove(position);
notifyItemRemoved(position);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.grocery_item_row, parent, false);
MyViewHolder holder = new MyViewHolder(view);
Log.i("GroceryHero", "onCreateViewHolder called");
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
MetaData current = data.get(position);
holder.title.setText(current.title);
holder.icon.setImageResource(current.iconid);
Log.i("GroceryHero","onBindViewHolder called " + position);
}
#Override
public int getItemCount() {
return data.size();
}
}
MyViewHolder:
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title;
ImageView icon;
public MyViewHolder(View itemView) {
super(itemView);
Log.i("GroceryHero", "MyViewHolder called");
title = (TextView) itemView.findViewById(R.id.grocery_text);
icon = (ImageView) itemView.findViewById(R.id.grocery_icon);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Item clicked at " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
delete(getAdapterPosition()); // ERROR: Cannot resolve method
}
}
You're trying to call delete from the view holder class. But delete is a method of the adapter class. You can may be pass an instance of adapter to the view holder in onCreateViewHolder and store it as a private field.
GroceryItemAdapter mAdapter;
public MyViewHolder(View itemView, GroceryItemAdapter adapter) {
mAdapter = adapter;
...
}
// now you can call
mAdapter.delete(getAdapterPosition());
hi i have a problem friends i am passing the text and the images from the Json using volley library and recycler view with horizontal orientation everything working fine but i am getting more gap in between the images because of that navigation drawer also effecting this the image of the home screen
Here is navigation drawer
This adapter featured products
public class FeaturedAdapter extends RecyclerView.Adapter<ViewHolder> {
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
private Context context;
private List<Data> fpdata;
public FeaturedAdapter(Context context, List<Data> fpdata) {
this.context = context;
this.fpdata = fpdata;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View fpds = LayoutInflater.from(parent.getContext()).inflate(R.layout.singlefeatured, parent,false);
ViewHolder viewHolder = new ViewHolder(fpds);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Data feature=fpdata.get(position);
holder.netfp.setImageUrl(feature.getFpimage(),imageLoader);
holder.title.setText(feature.getTitle());
holder.price.setText(feature.getPrice());
}
#Override
public int getItemCount() {
return fpdata.size();
}
}