Image url passing null value to another Intent - java

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;

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
}
});

Android: RecyclerView.SetAdapter causes an application crash with no obvious error

We have been working on implementing a RecyclerView in our project for a while now, and although the backend seems to work just fine, as soon as I call the recyclerView.setAdapter it causes the program to crash. I have spent several hours poring over this program, and nothing yet, although I have suspicions that the answer lies in the adapter. Can anyone shed some light on this?
Create and set adapter:
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView = findViewById(R.id.navRV);
mRecyclerView.setHasFixedSize(false);
mRecyclerView.setLayoutManager(mLayoutManager);
Date todaysDate = new Date(System.currentTimeMillis());
AnnouncementList.add("Test Announcement 1", "This is some random text in the body" +
". It really needs to show up correctly!!", todaysDate);
mAdapter = new TwitterAdapter(mContext);
mRecyclerView.setAdapter(mAdapter);
And this is the adapter:
package com.company.application;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Twitter Adapter:
* This adapter is the interface between the data and the view
*/
public class TwitterAdapter extends RecyclerView.Adapter<TwitterAdapter.ViewHolder> {
//Use this as the tag for logging in this activity
final private String LOGTAG = "TWITTER_ADAPTER";
private Context mContext;
public TwitterAdapter(Context mContext) {
this.mContext = mContext;
}
#NonNull
#Override
public TwitterAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.list_layout, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull TwitterAdapter.ViewHolder holder, int position) {
//Set the text and the title
holder.textViewBody.setText((String)AnnouncementList.getList().get(position).getBody());
holder.textViewTitle.setText((String)AnnouncementList.getList().get(position).getTitle());
//If we have an image, insert it so we can draw it
if(AnnouncementList.getList().get(position).getImage() != null)
holder.imageView.setImageDrawable((Drawable)AnnouncementList.getList().get(position).getImage());
else
holder.imageView.setImageDrawable(null);
}
#Override
public int getItemCount() {
Log.d(LOGTAG, "getItemCount returns " + AnnouncementList.size());
return AnnouncementList.size();
}
/**
* Holds the view so that the RecyclerView can process it
*/
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public RelativeLayout mRelativeLayout;
ImageView imageView;
TextView textViewTitle, textViewBody, textViewDate;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textViewTitle = itemView.findViewById(R.id.textViewTitle);
textViewBody = itemView.findViewById(R.id.textViewBody);
// textViewDate = itemView.findViewById(R.id.textViewDate);
}
}
}
You need to pass the the arraylist to adapter like this
mAdapter = new TwitterAdapter(mContext, AnnouncementList);
Then in the adapter change the constructor like this
private Context mContext;
private ArrayList<ModelClass> mAnnouncementList = new ArrayList();
public TwitterAdapter(Context mContext,ArrayList<ModelClass> mAnnouncementList) {
this.mContext = mContext;
this.mAnnouncementList= mAnnouncementList;
}
and then access variables like this
holder.textViewBody.setText(mAnnouncementList.getList().get(position).getBody());
holder.textViewTitle.setText(mAnnouncementList.getList().get(position).getTitle());
dont forget to return the size like below
#Override
public int getItemCount() {
Log.d(LOGTAG, "getItemCount returns " + mAnnouncementList.size());
return mAnnouncementList.size();
}

Word list in a RecyclerView and the favourite section of my app doesn't work properly

I developed a vocabulary app where users can select their favorite words to a favourite section. I have two problems.
First, my code looks okay but when I choose a word from the list it doesn't show in the favorite section immediately. However, when I re-open the application it appears there.
The second problem is with the words which repeat in list with any run.
Here's my code :
Adapter list
package farmani.com.essentialwordsforielts.mainPage;
import android.content.Context;
import android.content.Intent;
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.LinearLayout;
import android.widget.TextView;
import farmani.com.essentialwordsforielts.R;
import farmani.com.essentialwordsforielts.innerpage.ActivityInnerPage;
public class AdapterList extends RecyclerView.Adapter<ViewHolder> {
Context context;
LayoutInflater inflater;
TextView title;
ImageView avatar;
LinearLayout cardAdapter;
public AdapterList(Context context){
this.context = context;
inflater = LayoutInflater.from(context);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.adapter_card_view, parent, false);
title = (TextView) view.findViewById(R.id.title1);
avatar = (ImageView) view.findViewById(R.id.avatar);
cardAdapter = (LinearLayout) view.findViewById(R.id.card_adapter);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.title.setText(MainActivity.list.get(position).getWord());
String img = MainActivity.list.get(position).getImg();
int id = MainActivity.context.getResources().getIdentifier(img, "drawable", MainActivity.context.getPackageName());
holder.avatar.setImageResource(id);
holder.cardAdapter.setOnClickListener(clickListener);
holder.cardAdapter.setId(position);
}
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = view.getId();
Intent intent = new Intent (MainActivity.context, ActivityInnerPage.class);
intent.putExtra("name", "list");
intent.putExtra("id", position + "");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainActivity.context.startActivity(intent);
}
};
#Override
public int getItemCount() {
return MainActivity.list.size();
}
}
Adapter favorite
package farmani.com.essentialwordsforielts.mainPage;
import android.content.Context;
import android.content.Intent;
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.LinearLayout;
import android.widget.TextView;
import farmani.com.essentialwordsforielts.R;
import farmani.com.essentialwordsforielts.innerpage.ActivityInnerPage;
public class AdapterFav extends RecyclerView.Adapter<ViewHolder> {
Context context;
LayoutInflater inflater;
TextView title;
ImageView avatar;
LinearLayout cardAdapter;
public AdapterFav(Context context){
this.context = context;
inflater = LayoutInflater.from(context);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.adapter_card_view, parent, false);
title = (TextView) view.findViewById(R.id.title1);
avatar = (ImageView) view.findViewById(R.id.avatar);
cardAdapter = (LinearLayout) view.findViewById(R.id.card_adapter);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.title.setText(MainActivity.favorite.get(position).getWord());
String img = MainActivity.favorite.get(position).getImg();
int id = MainActivity.context.getResources().getIdentifier(img, "drawable", MainActivity.context.getPackageName());
holder.avatar.setImageResource(id);
holder.cardAdapter.setOnClickListener(clickListener);
holder.cardAdapter.setId(position);
}
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = view.getId();
Intent intent = new Intent (MainActivity.context, ActivityInnerPage.class);
intent.putExtra("name", "favorite");
intent.putExtra("id", position + "");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainActivity.context.startActivity(intent);
}
};
#Override
public int getItemCount() {
return MainActivity.favorite.size();
}
}
When you update your data set call adapater.notifyChange();

java.lang.IllegalArgumentException: Context must not be null

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;
}

Void android.widget.imageview.setimageresource(int)' on a Null Reference(fatal error message in the log cat)

Please am having issue with this null error message java.lang.NullPointerException (no error message). I really need assistance on how and where to add the appropriate code so that the application would not crash. Here is the JAVA file that i am having issues with. I am trying to create a recycler view and jpg images from my drawable folder
HorizontalListAdapter.java
import android.app.Activity;
import android.graphics.Picture;
import android.os.Bundle;
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.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class HorizontalListAdapter extends RecyclerView.Adapter<HorizontalListAdapter.ViewHolder>
{
private Activity activity;
int[] images= {R.drawable.vb1,R.drawable.pota,R.drawable.vb1,
R.drawable.pota,R.drawable.vb1,R.drawable.download1,R.drawable.pota};
String[] food_items={"prawan","awadhi_lucknow_biryani","eggwraps","chips","mayonnaise","companin","mixvegwrap"};
public HorizontalListAdapter(Activity activity)
{
this.activity = activity;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_horizontal_list, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalListAdapter.ViewHolder viewHolder, final int position) {
viewHolder.imageView.setImageResource(images[position]);
viewHolder.txtview.setText(food_items[position].toUpperCase());
viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity, "Position clicked: " + position, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return images.length;
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout linearLayout;
private ImageView imageView;
private TextView txtview;
public ViewHolder(View view) {
super(view);
imageView = (ImageView) view.findViewById(R.id.imageview);
txtview = (TextView) view.findViewById(R.id.txtview);
linearLayout = (LinearLayout) view.findViewById(R.id.layout);
}
}
}
The second java class VerticalListAdapter.java
import android.app.Activity;
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.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class VerticalListAdapter extends RecyclerView.Adapter<VerticalListAdapter.ViewHolder> {
private Activity activity;
public VerticalListAdapter(Activity activity) {
this.activity = activity;
}
int[] images= {R.drawable.images,R.drawable.images,R.drawable.images,
R.drawable.images,R.drawable.images,R.drawable.images,R.drawable.images};
String[] food_items={"prawan","awadhi_lucknow_biryani","eggwraps","chips","mayonnaise","companin","mixvegwrap"};
String[] cost={"Rs 200","Rs 300","Rs 150","R 320","Rs 450","Rs 120","Rs 380"};
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_recycler_view, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
// if ((position + 1) % 2 == 0) {
viewHolder.imageView.setImageResource(images[position]);
viewHolder.txtview.setText(food_items[position].toUpperCase());
viewHolder.txtCost.setText("Cost Per Person "+cost[position]);
// } else {
// viewHolder.imageView.setImageResource(R.drawable.awadhi_lucknow_biryani);
//}
viewHolder.container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity, "Position: " + position, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return images.length;
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView txtview;
private TextView txtCost;
private RelativeLayout container;
public ViewHolder(View view) {
super(view);
imageView = (ImageView) view.findViewById(R.id.image);
txtview = (TextView) view.findViewById(R.id.text);
txtCost= (TextView) view.findViewById(R.id.textView);
container = (RelativeLayout) view.findViewById(R.id.container);
}
}
}
Please i need assistance with this i guess it is the int[]images causing the error i may be wrong please correct me with the right code i would use to replace and make the code work.
changeimageview accessibility form private to any other modifier & try again

Categories