java.lang.IllegalArgumentException: Context must not be null - java

I am using adapter to show images. In Adapter, How to get context when using Picasso?
I tried different solutions but I couldn't solve. What should I do when using picasso in Adapter? Context is a big problem for developers I see on the internet.
package gc.x;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import gc.ovidos_challenge.R;
import gc.ovidos_challenge.models.Image;
import java.net.URL;
import java.util.List;
public class ImagesAdapter extends RecyclerView.Adapter<ImagesAdapter.MyViewHolder> {
private List<Image> imagesList;
private Context context;
public ImagesAdapter(Context context) {
this.context = context;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public ImageView imageview;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.textview);
imageview = (ImageView) view.findViewById(R.id.imageurl);
}
}
public ImagesAdapter(List<Image> imagesList) {
this.imagesList = imagesList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_image, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Image image = imagesList.get(position);
holder.title.setText(image.title);
Picasso.with(this.context).load(image.url).into(holder.imageview);
// holder.imageview.setImageBitmap(image.getBitmapFromURL());
}
#Override
public int getItemCount() {
return imagesList.size();
}
}

you have created two constructors one for context and one for your data. use one constructor to initialize both of them
like this
public ImagesAdapter(Context context,List<Image> imagesList) {
this.context = context;
this.imagesList=imagesList;
}

Related

Is it possible change the fragment when listview click

I would like to change the fragment when listview item clicked under Bottom navigation activity
But I have not idea how to write the OnClickListener
Anyone can provide some hints or tell me what wrong in this program?
Here is the program
And thank you for spend the time to view my program
Thank you very much
package com.example.campus.ui.campus;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.c.MainActivity;
import com.example.c.R;
import com.example.c.database.campus.CampusData;
import com.example.c.ui.campusInformation.CampusInformationActivity;
import com.example.c.ui.campusInformation.CampusInformationFragment;
import java.util.ArrayList;
import java.util.List;
public class CampusListLayoutAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<CampusData> campusList = new ArrayList<CampusData>();
private int resourceViewID;
private Context context;
private Context mContext;
static class ViewHolder{
LinearLayout llCampusCard;
TextView tvCampusName;
TextView tvCampusAddress;
ImageView ivCampusImage;
}
public CampusListLayoutAdapter(Context c, List<CampusData> campusList){
context = c;
layoutInflater = LayoutInflater.from(c);
this.campusList = campusList;
}
#Override
public int getCount() {
return campusList.size();
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
mContext = viewGroup.getContext();
ViewHolder holder = new ViewHolder();
view = layoutInflater.inflate(R.layout.listview_campus, null);
if(view != null){
holder.tvCampusName = view.findViewById(R.id.tvCampusName);
holder.tvCampusAddress = view.findViewById(R.id.tvCampusAddress);
holder.ivCampusImage = view.findViewById(R.id.ivCampusImage);
holder.tvCampusName.setText(campusList.get(i).name);
holder.tvCampusAddress.setText(campusList.get(i).address);
String image = campusList.get(i).image;
resourceViewID = context.getResources().getIdentifier(image, "drawable", context.getPackageName());
holder.ivCampusImage.setImageResource(resourceViewID);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return view;
}else {
return null;
}
}
}
First of all, create an interface in your adapter class.
public interface Callbacks {
void onItemClick(YourObject object, int position); // arguments as per the requirement
}
change your constructor of adapter as
Callbacks callback;
public CampusListLayoutAdapter(Context c, List<CampusData> campusList, Callbacks callback){
.....
this.callback = callback;
}
Now in your onClick() use..
callback.onItemClick(yourClickedItem, position)
then pass an anonymous or simply implement interface in your activity.
adapter = new CampusListLayoutAdapter(context, list, new CampusListLayoutAdapter.Callbacks() {
#Override
public void onItemClick(Alert_bean alert, int position) {
// do what you want here in activity like changing fragment or view updates
}
});

Cannot resolve method setText() in imageView

I'm trying to do a simple project with Recyclerview and Cardview in Android Studio following this tutorial.
But I'm getting an error in onBindViewHolder function line 32 saying it can't resolve the method setText() in imageView and I don't know why since mTitle is not an ImageView and it is a TextView:
package com.example.demoproject;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import android.content.Context;
public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
Context c;
ArrayList<Model> models; // this array list creates a list of arrays which parameters define in my model class
public MyAdapter(Context c, ArrayList<Model> models) {
this.c = c;
this.models = models;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row,null); // this line inflate my row
return new MyHolder(view); // this will return my view to holder class
}
#Override
public void onBindViewHolder(#NonNull MyHolder myHolder, int i) {
myHolder.mTitle.setText(models.get(i).getTitle()); // here i is position
}
#Override
public int getItemCount() {
return 0;
}
}
MyHolder.java :
package com.example.demoproject.
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class MyHolder extends RecyclerView.ViewHolder {
ImageView mImaeView, mTitle;
public MyHolder(#NonNull View itemView) {
super(itemView);
this.mImaeView = itemView.findViewById(R.id.imageIv);
this.mTitle = itemView.findViewById(R.id.titleTv);
}
}
Your mTile is not a TextView it is a ImageView. Change it to this:
ImageView mImaeView;
TextView mTitle;
And make sure that itemView.findViewById(R.id.titleTv); returns TextView. Check in Your xml file (recycler view item) that You made it eveything well and titleTv is a TextView
because setText() method is set on textview not in imageView

Is this errormessage (uses or overrides a deprecated API) related to my NullPointer Exception

I am facing a warning like this one:
... uses or overrides a deprecated API. Recompile with -Xlint:deprecation for details does this warning
This is my first time to see it. I face some null issues in my app so i wonder if this warning has a relation with my issues.
This is my adapter that I face the warning in
but the logcat tells me that i face nulls in other pages too so are all connected to this warning or not. thanks in advance
```
package com.example.quiz.Adabter;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.quiz.Common.Common;
import com.example.quiz.Model.CurrentQuestion;
import com.example.quiz.R;
import java.util.List;
public class ResultGridAdapter extends RecyclerView.Adapter<ResultGridAdapter.MyViewHolder> {
Context context;
List<CurrentQuestion> currentQuestionList;
public ResultGridAdapter(Context context, List<CurrentQuestion> currentQuestionList) {
this.context = context;
this.currentQuestionList = currentQuestionList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(context).inflate(R.layout.layout_result_item, viewGroup, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
Drawable img;
myViewHolder.btn_question.setText(new StringBuilder("Question").append(currentQuestionList.get(i)
.getQuestionIndex() + 1));
if (currentQuestionList.get(i).getType() == Common.ANSWER_TYPE.RIGHT_ANSWER) {
myViewHolder.btn_question.setBackgroundColor(Color.parseColor("#ff99cc00"));
img = context.getResources().getDrawable(R.drawable.ic_check_white_24dp);
myViewHolder.btn_question.setCompoundDrawables(null, null, null, img);
} else if (currentQuestionList.get(i).getType() == Common.ANSWER_TYPE.WRONG_ANSWER) {
myViewHolder.btn_question.setBackgroundColor(Color.parseColor("#ffcc0000"));
img = context.getResources().getDrawable(R.drawable.ic_clear_white_24dp);
myViewHolder.btn_question.setCompoundDrawables(null, null, null, img);
} else {
img = context.getResources().getDrawable(R.drawable.ic_error_outline_white_24dp);
myViewHolder.btn_question.setCompoundDrawables(null, null, null, img);
}
}
#Override
public int getItemCount() {
return currentQuestionList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
Button btn_question;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
btn_question = (Button) itemView.findViewById(R.id.btn_question);
btn_question.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LocalBroadcastManager.getInstance(context)
.sendBroadcast(new Intent(Common.KEY_BACK_FROM_RESULT).putExtra(Common.KEY_BACK_FROM_RESULT
, currentQuestionList.get(getAdapterPosition()).getQuestionIndex()));
}
});
}
}
}

Is it good to samplesized image when binding in recyclerview ViewHolder?

I want to build an android application and some images in it with recyclerView. I wonder if it good to use Bitmapfactory.option to reduce the image load time by the system when binding the view in recyclerView adapter ? or it will take more memory ?
here is my recyclerView adapter
package net.renotekno.rifqi.cardviewtraining.Adapter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import net.renotekno.rifqi.cardviewtraining.Data.Data;
import net.renotekno.rifqi.cardviewtraining.MainActivity;
import net.renotekno.rifqi.cardviewtraining.R;
import butterknife.BindView;
import butterknife.ButterKnife;
public class LocationRecycler extends RecyclerView.Adapter<LocationRecycler.ViewHolderLocation> {
private final MainActivity mContext;
public LocationRecycler(MainActivity mainActivity) {
mContext = mainActivity;
}
#Override
public ViewHolderLocation onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false);
return new ViewHolderLocation(view);
}
#Override
public void onBindViewHolder(ViewHolderLocation holder, int position) {
holder.bindItem(position, mContext);
}
#Override
public int getItemCount() {
return Data.locations.length;
}
public static class ViewHolderLocation extends RecyclerView.ViewHolder {
#BindView(R.id.placeTitle) TextView mPlaceTitle;
#BindView(R.id.countryTitle) TextView mCountryTitle;
#BindView(R.id.countryImage) ImageView mCountryImage;
public ViewHolderLocation(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
public void bindItem(int position, MainActivity context){
int imageToDisplay = Data.locations[position].getImageID();
Bitmap imageBitmap = getSimplesizedImage(imageToDisplay, context);
mPlaceTitle.setText(Data.locations[position].getPlaceTitle());
mCountryTitle.setText(Data.locations[position].getCountryLocation());
mCountryImage.setImageBitmap(imageBitmap);
}
private Bitmap getSimplesizedImage(int imageToDisplay, MainActivity context) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = 4;
return BitmapFactory.decodeResource(context.getResources() , imageToDisplay, options);
}
}
}

Image url passing null value to another Intent

I have implemented RecyclerView in my app and have setup a onClickListener on Recycler View. The text fields are passing successfully, however I also have a image in the recyclerview, which is being brought through picasso. So I try to retrieve the image url which is stored in another class, but I'm getting a empty value.
The code is:
package xyz.xyz;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.squareup.picasso.Picasso;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
public class FlatSearchCardAdapter extends RecyclerView.Adapter<FlatSearchCardAdapter.ViewHolder> {
private Context context;
private ImageLoader imageLoader;
List<FlatCardItemReturn> flatlistCard;
public FlatSearchCardAdapter(List<FlatCardItemReturn> flatlistCard, Context context){
super();
//Getting all the superheroes
this.flatlistCard = flatlistCard;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.flatrenter_list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
FlatCardItemReturn flatcarditemreturn = flatlistCard.get(position);
Picasso.with(context).load(flatcarditemreturn.getImageUrl()).placeholder(R.drawable.loadingpicasso).error(R.drawable.errorpicasso).resize(100, 100).into(holder.imageView);
holder.textViewId.setText(flatcarditemreturn.getId());
holder.textViewType.setText(flatcarditemreturn.getType());
holder.textViewFurnishing.setText(flatcarditemreturn.getFurnishing());
holder.textViewArea.setText(flatcarditemreturn.getArea());
holder.textViewPrice.setText(flatcarditemreturn.getPrice());
holder.textViewNumberofrooms.setText(flatcarditemreturn.getNumberofrooms());
}
#Override
public int getItemCount() {
return flatlistCard.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView imageView;
public TextView textViewType;
public TextView textViewPrice;
public TextView textViewFurnishing;
public TextView textViewArea;
public TextView textViewNumberofrooms;
public TextView textViewId;
public FlatCardItemReturn flatcarditemreturn = new FlatCardItemReturn();
public ViewHolder(View itemView) {
super(itemView);
textViewId = (TextView) itemView.findViewById(R.id.id);
imageView = (ImageView) itemView.findViewById(R.id.flatphoto);
textViewType = (TextView) itemView.findViewById(R.id.type);
textViewFurnishing = (TextView) itemView.findViewById(R.id.furnishing);
textViewArea = (TextView) itemView.findViewById(R.id.area);
textViewPrice = (TextView) itemView.findViewById(R.id.price);
textViewNumberofrooms = (TextView) itemView.findViewById(R.id.numberofrooms);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(), flatrenterpostview.class);
i.putExtra("idvalue", textViewId.getText().toString());
i.putExtra("typevalue", textViewType.getText().toString());
i.putExtra("furnishingvalue", textViewFurnishing.getText().toString());
i.putExtra("areavalue", textViewArea.getText().toString());
i.putExtra("pricevalue", textViewPrice.getText().toString());
i.putExtra("numberofroomsvalue", textViewNumberofrooms.getText().toString());
i.putExtra("imageurl", flatcarditemreturn.getImageUrl());
v.getContext().startActivity(i);
}
});
}
}
}
I'm pretty confused on how to set the position in that, so as to get the currect value. Will be grateful if anyone can help me out.
Just to make it clear, i.putExtra("imageurl", flatcarditemreturn.getImageUrl()); is giving nothing. I expect the url stored in my class. The url is stored perfectly, I checked.
Just to make it clear, i.putExtra("imageurl", flatcarditemreturn.getImageUrl()); is giving nothing. I expect the url stored in my class
Then why do you create a new FlatCardItemReturn in your ViewHolder? This line:
public FlatCardItemReturn flatcarditemreturn = new FlatCardItemReturn();
You'll want to set the correct data item to the ViewHolder, rather that instantiating a new 'empty' one. You can easily accomplish this by adding a method to the ViewHolder:
public void setData(FlatCardItemReturn item) {
flatcarditemreturn = item;
}
Now, just call this from onBindViewHolder():
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
FlatCardItemReturn flatcarditemreturn = flatlistCard.get(position);
// other stuff omitted
holder.setData(flatcarditemreturn);
}
You can now also change:
public FlatCardItemReturn flatcarditemreturn = new FlatCardItemReturn();
into:
public FlatCardItemReturn flatcarditemreturn;

Categories