I am not able to display Images using Picasso ImageLoader - android

I am working on recyclerview and cardview to load some images using Picasso Library.
I am pasting my code here.
This is my DivineDestinationDistricWiseActivity.
public class DivineDestinationsDistrictWise extends Fragment {
RecyclerView all_divine_dest_places_list;
List<Places> placesList;
GridLayoutManager glm;
public DivineDestinationsDistrictWise() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_divine_destinations_district_wise, container, false);
all_divine_dest_places_list = (RecyclerView) rootView.findViewById(R.id.all_divine_places_rv_id);
glm = new GridLayoutManager(getActivity() , 2);
all_divine_dest_places_list.setLayoutManager(glm);
all_divine_dest_places_list.setHasFixedSize(true);
intializeData();
initializeAdapter();
return rootView;
}
private void intializeData() {
placesList = new ArrayList<>();
placesList.add(new Places("Divine Destinations", R.drawable.hyd_balakampeta_ellamma));
placesList.add(new Places("Adventure Journeys", R.drawable.hyd_birla_mandir));
placesList.add(new Places("boatings", R.drawable.hyd_jagannatha_temple));
placesList.add(new Places("Heritage Spots", R.drawable.hyd_mecca_masjid));
placesList.add(new Places("Shopping", R.drawable.hyd_peddamma_temple));
placesList.add(new Places("Nature Discovery", R.drawable.hyd_peddamma_temple));
placesList.add(new Places("Wild Life", R.drawable.wild_life_main));
}
private void initializeAdapter()
{
DDDWAdapter adapter =new DDDWAdapter(placesList);
// places_type_list_recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
all_divine_dest_places_list.setAdapter(adapter);
}
}
This is my adapter class.
public class DDDWAdapter extends RecyclerView.Adapter<DDDWAdapter.DivineDestViewHolder> {
CardView cv;
Context context;
public class DivineDestViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView personName;
ImageView photo_city;
public DivineDestViewHolder(View itemView) {
super(itemView);
cv=(CardView)itemView.findViewById(R.id.cardview_places_id);
personName=(TextView)itemView.findViewById(R.id.places_list_id_tv);
photo_city=(ImageView)itemView.findViewById(R.id.image_places_list_id);
context = itemView.getContext();
photo_city.setOnClickListener(this);
}
#Override
public void onClick(View v) {
}
}
List<Places> place;
DDDWAdapter(List<Places> place)
{
this.place=place;
}
#Override
public DivineDestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.places_type_list, parent, false);
DivineDestViewHolder pvh = new DivineDestViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(DivineDestViewHolder holder, int position) {
holder.personName.setText(place.get(position).city_name);
holder.photo_city.setImageResource(place.get(position).photo);
//here i am trying to load images with Picasso Library but not getting Images.
Picasso.with(context).load(R.id.image_places_list_id).into(holder.photo_city);
}
#Override
public int getItemCount() {
return place.size();
}
}
This is my Places class.
public class Places {
String city_name;
int photo;
Places(String city_name, int photo)
{
this.city_name = city_name;
this.photo=photo;
}
}
I have tried by inserting like below if I use like this I am getting images. But When I scroll the screen Images are scrolling with little jerky behaviour.
Picasso.with(context).load(place.get(position).photo).into(holder.photo_city);
Please help me out from this... Thanks in advance.....

first generate the setter getter of the Places Class
public class Places {
String city_name;
int photo;
public SampleClass(String city_name, int photo) {
this.city_name = city_name;
this.photo = photo;
}
public String getCity_name() {
return city_name;
}
public void setCity_name(String city_name) {
this.city_name = city_name;
}
public int getPhoto() {
return photo;
}
public void setPhoto(int photo) {
this.photo = photo;
}
}
and then initialize your adapter and send also a context to it like
DDDWAdapter adapter =new DDDWAdapter(getActivity,placesList);
and then your adapter look like this
public class DDDWAdapter extends RecyclerView.Adapter<DDDWAdapter.DivineDestViewHolder> {
CardView cv;
Context context;
List<Places> place;
public class DivineDestViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener{
TextView personName;
ImageView photo_city;
public DivineDestViewHolder(View itemView) {
super(itemView);
cv=(CardView)itemView.findViewById(R.id.cardview_places_id);
personName=(TextView)itemView.findViewById(R.id.places_list_id_tv);
photo_city=(ImageView)itemView.findViewById(R.id.image_places_list_id);
context = itemView.getContext();
photo_city.setOnClickListener(this);
}
#Override
public void onClick(View v) {
}
}
DDDWAdapter(Context context,List<Places> place)
{
this.context=context;
this.place=place;
}
#Override
public DivineDestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.places_type_list, parent, false);
DivineDestViewHolder pvh = new DivineDestViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(DivineDestViewHolder holder, int position) {
holder.personName.setText(place.get(position).city_name());
//here i am trying to load images with Picasso Library but not getting Images.
Picasso.with(context).load(place.get(position).getPhoto()).into(holder.photo_city);
}
#Override
public int getItemCount() {
return place.size();
}

You are setting image resource directly to your imageView and then latter you are assigning R.id instead of R.drawble to load via Picasso
#Override
public void onBindViewHolder(DivineDestViewHolder holder, int position) {
holder.personName.setText(place.get(position).city_name);
holder.photo_city.setImageResource(place.get(position).photo);
//here i am trying to load images with Picasso Library but not getting Images.
Picasso.with(context).load(R.id.image_places_list_id).into(holder.photo_city);
}
You need to load images like this
#Override
public void onBindViewHolder(DivineDestViewHolder holder, int position) {
holder.personName.setText(place.get(position).city_name);
//here is how you should be loading image
Picasso.with(context).load(place.get(position).photo).into(holder.photo_city);
}

Related

fragment recyclerview is giving this error E/RecyclerView: No adapter attached; skipping layout

I,ve tried everything and nothing is working... Any solution please. if i use listview it works perfectly but on recycler view i'm getting an error.
the code is below
home fragment
recyclerViewOne = view.findViewById(R.id.recyclerViewOne);
collectionReference = firebaseFirestore.collection("Trending songs");
recyclerViewOne.setHasFixedSize(true);
recyclerViewOne.setLayoutManager(new
LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
mUpload = new ArrayList<>();
gridViewHolder = new GridViewHolder(getActivity(), mUpload, new
GridViewHolder.ItemClickListeners() {
#Override
public void onClicke(GridModel model, int post) {
((DashboardActivity) getActivity()).method();
((DashboardActivity) getActivity()).playO(post);
}
});
recyclerViewOne.setAdapter(gridViewHolder);
i need some help please
According to the naming, you are trying the set a ViewHolder as an adapter to the recycler. If that is the case, then you should implement it the correct way: a custom Adapter that extends RecyclerView.Adapter and that add that adapter to the recycler view
this is the GridViewHolder
public class GridViewHolder extends RecyclerView.Adapter<GridViewHolder.SongsAdapterViewHolder> {
Context context;
List<GridModel> gridModels;
private ItemClickListeners listeners;
private int selected;
#NonNull
#Override
public SongsAdapterViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.grid_item, parent,false);
return new SongsAdapterViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull SongsAdapterViewHolder holder, int position) {
GridModel gridModel = gridModels.get(position);
holder.mDesc.setText(gridModel.getmDesc());
holder.mTitle.setText(gridModel.getmTitle());
String duration = Utility.convertion(Long.parseLong(gridModel.getDuration()));
Glide.with(context).load(gridModel.imageurl).into(holder.imageurl);
holder.bind(gridModel, listeners);
}
public GridViewHolder(Context context, List<GridModel> gridModels, ItemClickListeners listeners) {
this.context = context;
this.gridModels = gridModels;
this.listeners = listeners;
}
#Override
public int getItemCount() {
return gridModels.size();
}
public class SongsAdapterViewHolder extends RecyclerView.ViewHolder{
TextView mTitle, mDesc;
ImageView imageurl;
public SongsAdapterViewHolder(#NonNull View itemView) {
super(itemView);
mTitle = itemView.findViewById(R.id.gridTitle);
mDesc= itemView.findViewById(R.id.gridMusician);
imageurl = itemView.findViewById(R.id.gridImageView);
}
public void bind(final GridModel gridModel, final ItemClickListeners listeners) {
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listeners.onClicke(gridModel, getAdapterPosition());
}
});
}
}
public interface ItemClickListeners {
void onClicke(GridModel model, int post);
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
}

Can we make and use two lists of same model class in Adapter?

I am working on Retrofit to show list of images and i am using List to show images which is working fine.But the problem is now I want to show name and age from the same model class.
My Question is I want to use another List to show the name and age in the same adapter and set to same Recyclerview. Is this Possible?
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.VideoInfoHolder> {
private Context context;
private List<String> imgList= Collections.emptyList();
private ArrayList<MyModel> myModel;
public MyAdapter(Context context) {
this.context = context;
}
#Override
public VideoInfoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_view, parent, false);
return new VideoInfoHolder(itemView);
}
#Override
public void onBindViewHolder(final VideoInfoHolder holder, final int position) {
Picasso.with(this).load(imgList.getImage()).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
//do what ever you want with your bitmap
imgView.setImageBitmap(loadedImage);///imgView is use to set the image in it
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
myModel= new ArrayList<>();
for (int i = 0; i < myModel.size(); i++) {
holder.tvName.setText(myModel.get(i).getName());
holder.tvAge.setText(myModel.get(position).getAge());
}
}
#Override
public int getItemCount() {
return imgList.size();
}
public class MyInfoHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public AppCompatImageView imgView;
public AppCompatTextView tvName, tvAgee;
public VideoInfoHolder(View itemView) {
super(itemView);
tvName = (AppCompatTextView) itemView.findViewById(R.id.tv_name);
tvAge = (AppCompatTextView) itemView.findViewById(R.id.tv_age);
}
}
public void notifyDataChange(List<String> myList) {
if (!imgList.isEmpty()) {
imgList.clear();
}
imgList= myList;
notifyDataSetChanged();
}
}
You should create your own model with image name and age.After that in first parsing just set image and use in adapter. In second time use name and age in same adapter model. In last notify adapter.
Thanks.

recyclerview in view pager won't load

I am developing an android app with multiple tabs using viewpager. However, one of the tabs which contain recyclerview just keep on loading non stop as shown in the attached screenshot. The recyclerview contains a list of images. I am able to load imageview and textview in other tabs successfully, only this tab which contain recyclerview has issue.
Adapter:
public class MasterListAdapter extends RecyclerView.Adapter<MasterListAdapter.Holder> {
private Context mContext;
private List<Integer> mImageIds;
private LayoutInflater inflater;
#BindView(R.id.images_list_view) ImageView imageView;
public MasterListAdapter(Context context, List<Integer> imageIds) {
mContext = context;
mImageIds = imageIds;
inflater = LayoutInflater.from(context);
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
return new Holder(inflater.inflate(R.layout.list_view, parent, false));
}
#Override
public void onBindViewHolder(Holder holder, int position) {
Glide.with(mContext).load(mImageIds.get(position)).apply(RequestOptions.fitCenterTransform()).into(imageView);
}
#Override
public int getItemCount() {
return mImageIds.size();
}
public class Holder extends ViewHolder {
public Holder(View itemView) {
super(itemView);
}
}
}
Fragment:
public class ListFragment extends BaseFragment {
#BindView(R.id.recyclerView)
RecyclerView recyclerView;
#Override
int getLayoutId() {
return R.layout.fragment_list;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
init();
}
private void init() {
recyclerView.setAdapter(new MasterListAdapter(getContext(),ImageAssets.getImages()));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
}
ImageAssets:
public class ImageAssets {
private static final List<Integer> images = new ArrayList<Integer>() {{
add(R.drawable.user);
//add(R.drawable.img2);
//add(R.drawable.img3);
//add(R.drawable.img4);
//add(R.drawable.img5);
//add(R.drawable.img6);
//add(R.drawable.img7);
//add(R.drawable.img8);
//add(R.drawable.img9);
//add(R.drawable.img10);
//add(R.drawable.img11);
//add(R.drawable.img12);
//add(R.drawable.img13);
}};
public static List<Integer> getImages() {
return images;
}
}
Viewpager:
static class ViewPagerAdapter extends FragmentPagerAdapter {
ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public Fragment getItem(int position) {
return Page.values()[position].getFragment();
}
#Override
public CharSequence getPageTitle(int position) {
return Page.values()[position].getTitle();
}
#Override
public int getCount() {
return Page.values().length;
}
}
enum Page {
First("View") {
#Override
Fragment getFragment() {return new ListFragment();}
};
private String title;
Page(String title) {
this.title = title;
}
String getTitle() {
return title;
}
abstract Fragment getFragment();
}
list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:foreground="?attr/selectableItemBackground"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/images_list_view"
android:clipToPadding="false"
android:src="#drawable/user">
</ImageView>
</android.support.v7.widget.CardView>
Update:
Hi all, thank you for all your value responses to my post. I have figured out the cause of the infinite loading issue. It was because I imported ListFragment in MainActivity accidentally which has the exact same name as my fragment class. Removing the import solved the issue.
Instead of
#BindView(R.id.images_list_view) ImageView imageView;
try this :-
public class Holder extends ViewHolder {
public ImageView imageView;
public Holder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.images_list_view);
}
and in onBind
#Override
public void onBindViewHolder(Holder holder, int position) {
Glide.with(mContext)
.load(mImageIds.get(position))
.apply(RequestOptions.fitCenterTransform())
.into(holder.imageView);
}
As you are calling a static reference your ImageAssets constructor is never getting called.. so get the correct list of images try this in your fragment :-
private void init() {
ImageAssets imageAssets = new ImageAssets(); // add this line to get the images
recyclerView.setAdapter(new MasterListAdapter(getContext(),ImageAssets.getImages()));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
Make constructor in ImageAssets class and initialize images there !!
Found your problem. You forgot to bind your view inside Holder class.
public class MasterListAdapter extends RecyclerView.Adapter<MasterListAdapter.Holder> {
private Context mContext;
private List<Integer> mImageIds;
private LayoutInflater inflater;
public MasterListAdapter(Context context, List<Integer> imageIds) {
mContext = context;
mImageIds = imageIds;
inflater = LayoutInflater.from(context);
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
return new Holder(inflater.inflate(R.layout.list_view, parent, false));
}
#Override
public void onBindViewHolder(Holder holder, int position) {
Glide.with(mContext).load(mImageIds.get(position)).apply(RequestOptions.fitCenterTransform()).into(holder.imageView);
}
#Override
public int getItemCount() {
return mImageIds.size();
}
public class Holder extends ViewHolder {
#BindView(R.id.images_list_view) ImageView imageView;
public Holder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
Fragment
public class ListFragment extends BaseFragment {
#BindView(R.id.recyclerView)
RecyclerView recyclerView;
#Override
int getLayoutId() {
return R.layout.fragment_list;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
init();
}
private void init() {
recyclerView.setAdapter(new MasterListAdapter(getContext(),ImageAssets.getImages()));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
}

Getting blank RecyclerView inside ConstraintLayout

I'm trying to use CardView , inside the CardView i placed UI widgets 2 TextViews and 2 ImageViews. My RecyclerView is used to display a list of Cardviews
Here's my Java Class model:
public class Masar {
public String masarName;
public String masarDesc;
public int imgl;
public Masar(String masarName, String masarDesc) {
this.masarName = masarName;
this.masarDesc = masarDesc;
}
public Masar(String masarName, String masarDesc, int imgl) {
this.masarName = masarName;
this.masarDesc = masarDesc;
this.imgl = imgl;
}
public String getMasarName() {
return masarName;
}
public String getMasarDesc() {
return masarDesc;
}
public int getImgl() {
return imgl;
}
}
And here's my Adapter class:
public class MasarAdapter extends RecyclerView.Adapter<MasarAdapter.MyViewHolder> {
private Context mContext;
private List<Masar> masrList;
public MasarAdapter(Context mContext, List<Masar> masrList) {
this.mContext = mContext;
this.masrList = masrList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.masar_items, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Masar masar=masrList.get(position);
holder.masarName.setText(masar.getMasarName());
holder.masarDesc.setText(masar.getMasarDesc());
}
#Override
public int getItemCount() {
return 0;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView masarName, masarDesc;
public ImageView masarImg;
public MyViewHolder(View itemView) {
super(itemView);
masarName = (TextView) itemView.findViewById(R.id.masarName);
masarDesc = (TextView) itemView.findViewById(R.id.masarDesc);
masarImg = (ImageView) itemView.findViewById(R.id.imageView4);
}
}
}
I've tried several times to find out why i'm getting blank result .. Could you please help to solve my problem ?
You're returning 0 in your getItemCount() method.
#Override
public int getItemCount() {
return 0;
}
It should be:
#Override
public int getItemCount() {
return masrList.size();
}

Images got changed in recyclerview adapter click

I have used RecyclerView to display data which contains an Image and a TextView as rawitem.
My Adapter class is as below :
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.RecyclerViewHolders> {
private List<MyModel> itemList;
private Context context;
private DisplayImageOptions displayImageOptions;
public MyAdapter(Activity context, List<MyModel> itemList) {
this.itemList = itemList;
this.context = context;
}
#Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.singleview_grid_location, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
#Override
public void onBindViewHolder(final RecyclerViewHolders holder, final int position) {
holder.rl_main.setTag(position);
final MyModel mainModel = itemList.get((int) holder.rl_main.getTag());
if (mainModel.is_selected()) {
holder.rl_main.setBackgroundResource(R.drawable.border_theme);
holder.iv_selection.setVisibility(View.VISIBLE);
} else {
holder.rl_main.setBackgroundColor(Color.parseColor("#FFFFFF"));
holder.iv_selection.setVisibility(View.GONE);
}
holder.txt_location_name.setText(mainModel.getLocationName());
if (mainModel.getImagePath() != null && !mainModel.getImagePath().equals("")) {
Constant.getImageLoader().displayImage(mainModel.getImagePath(),
holder.iv_location_photo, Constant.getInstanceImageOptions(R.mipmap.popup_placeholder), new SimpleImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view2) {
}
#Override
public void onLoadingFailed(String imageUri, View view2, FailReason failReason) {
holder.iv_location_photo.setImageResource(R.mipmap.popup_placeholder);
}
#Override
public void onLoadingComplete(String imageUri, View view2, Bitmap loadedImage) {
}
});
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mainModel.is_selected()) {
mainModel.setIs_selected(false);
} else {
mainModel.setIs_selected(true);
}
itemList.set(position, mainModel);
notifyDataSetChanged();
}
});
}
#Override
public int getItemCount() {
return this.itemList.size();
}
public class RecyclerViewHolders extends RecyclerView.ViewHolder {
public TextView txt_location_name;
public ImageView iv_location_photo;
public CardView card_view;
public ImageView iv_selection;
public RelativeLayout rl_main;
public RecyclerViewHolders(View itemView) {
super(itemView);
txt_location_name = (TextView) itemView.findViewById(R.id.txt_location_name);
iv_location_photo = (ImageView) itemView.findViewById(R.id.iv_location_photo);
card_view = (CardView) itemView.findViewById(R.id.card_view);
iv_selection = (ImageView) itemView.findViewById(R.id.iv_selection);
rl_main = (RelativeLayout) itemView.findViewById(R.id.rl_main);
}
}
}
The issue is that, When I am clicking on view of Recyclerview, The images got changed. Ya, The textview and my selection remains same and works accurate. The issue is image got changed for views.
What might be the issue ? Please, checkout. Thanks.
What is the point of doing this ??
holder.rl_main.setTag(position);
final MyModel mainModel = itemList.get((int) holder.rl_main.getTag());
You need to get the List position from the position parameter itself that onBindView provides you.
Next You should write onClickListener on Class itself and not implement inside the onBindView.Bad Practice and can create some problems.
Override Method. " getItemViewType(int position) ".
After Modifying the model.
You should do notify item added/removed/changed/inserted based on the need and the provide position which you can get from getAdapterPosition.

Categories

Resources