Is there a reason this Picasso line is not loading my images? - java

I am building a simple blog feature and using picasso to load images into recyclerview in android studio but the images don't load, Please can someone help me understand why. See the related code section below:
public class BlogRecyclerViewAdapter extends RecyclerView.Adapter {
private Context context;
private List blogList;
// Constructor
public BlogRecyclerViewAdapter(Context context, List<Blog> blogList) {
this.context = context;
this.blogList = blogList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.post_row, parent, false);
return new ViewHolder(view, context);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
// Now bind the views and the data
Blog blog = blogList.get(position);
String imageUrl = null;
holder.title.setText(blog.getTitle());
holder.desc.setText(blog.getDesc());
DateFormat dateFormat = DateFormat.getDateInstance();
String formattedDate = dateFormat.format(new Date(Long.valueOf(blog.getTimeStamp())).getTime());
holder.timeStamp.setText(formattedDate);
imageUrl = blog.getImage();
//TODO: Use Picasso library to load image
Picasso.get().load(imageUrl).into(holder.image);
}
#Override
public int getItemCount() {
return blogList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView desc;
public TextView timeStamp;
public ImageView image;
String userId;
public ViewHolder(#NonNull View view, Context ctx) {
super(view);
context = ctx;
title = (TextView) view.findViewById(R.id.postTitleList);
desc = (TextView) view.findViewById(R.id.postTextList);
image = (ImageView) view.findViewById(R.id.postImageList);
timeStamp = (TextView) view.findViewById(R.id.timeStampList);
userId = null;
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// We can go to the next activity
}
});
}
}
}

Related

How to update the recycler without changing the list

My Recyclerview is filled with a list containing links to images. But the images can change (let's say the user can draw something on them), but the link to the image itself does not change. I need to make it so that when the image changes, it is also drawn again in the recycler.
notifyDataSetChanged() does not work in this case, apparently because it reacts to changes in the sheet, and in fact there are no changes in the sheet itself, the links are the same in it. But the images are changed by links.
Maybe somehow you can push the recycler so that it is updated or draws items again?
My Activity:
public class MyDrawing extends Fragment {
private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;
private View root;
private TextView text;
public MyDrawing() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_my_drawing, container, false);
recyclerView = root.findViewById(R.id.recyclerViewMyDrawing);
text = root.findViewById(R.id.text);
List<CacheImageModel> cacheImageModels = getCacheImageByState();
recyclerViewAdapter = new RecyclerViewAdapter(cacheImageModels, getContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
recyclerView.setAdapter(recyclerViewAdapter);
recyclerViewAdapter.notifyDataSetChanged();
return root;
}
My Adapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<CacheImageModel> cacheImageModels;
private Context context;
private String imgUrl;
private int imageKey;
private String category;
private String nameImage;
private ProgressBar progressBarItem;
public RecyclerViewAdapter(List<CacheImageModel> cacheImageModels, Context context) {
this.cacheImageModels = cacheImageModels;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_view_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
imgUrl = cacheImageModels.get(position).getImageCacheUrl();
nameImage = cacheImageModels.get(position).getName();
category = cacheImageModels.get(position).getCategory();
imageKey = cacheImageModels.get(position).getImageKey();
holder.progressBarItem.setVisibility(View.VISIBLE);
holder.relativeLayout.setLayoutParams(new FrameLayout.LayoutParams(MyApp.getScreenWidth(context) / 2,
MyApp.getScreenWidth(context) / 2));
Glide.with(context)
.load(imgUrl)
.listener(new RequestListener<Drawable>() {
#Override
public boolean onLoadFailed(#Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
holder.progressBarItem.setVisibility(View.GONE);
return false;
}
})
.into(holder.image);
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int posit = holder.getAdapterPosition();
if (posit < 0 || posit >= cacheImageModels.size()) {
Log.d("POSITION", "Number postition " + posit + " its error");
} else {
String urlImagePosition = cacheImageModels.get(posit).getImageCacheUrl();
String namePosition = cacheImageModels.get(posit).getName();
String categoryPosition = cacheImageModels.get(posit).getCategory();
int imageKeyPosition = cacheImageModels.get(posit).getImageKey();
Uri uri = ContentUris.withAppendedId(MCDataContract.CONTENT_URI, imageKeyPosition);
Intent intent = new Intent(v.getContext(),
ColoringActivity.class);
intent.putExtra("urlImagePosition", urlImagePosition);
intent.putExtra("nameImage", namePosition);
intent.putExtra("keyPosition", imageKeyPosition);
intent.putExtra("categoryPosition", categoryPosition);
intent.setData(uri);
v.getContext().startActivity(intent);
}
}
});
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
#Override
public int getItemCount() {
return cacheImageModels.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private ImageView image;
private ProgressBar progressBarItem;
private RelativeLayout relativeLayout;
private CardView cardView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.imageView);
progressBarItem = itemView.findViewById(R.id.progressBarItem);
relativeLayout = itemView.findViewById(R.id.parentItem);
cardView = itemView.findViewById(R.id.cardView);
}
}
}
notifyDataSetChanged() is most likely working as planned. You can set a breakpoint in your view holder binding code to check this.
It is more likely that Glide is pulling the cached versions of the images. You will need a way to get Glide to ignore what's in cache and reload from the URL.
This post addresses the same issue and may be of help to you.

How to get the name of the running app, not the package name? [duplicate]

This question already has answers here:
How to get installed application name in android?
(2 answers)
Closed 4 years ago.
by using private List appProcessInfos;
am getting the total package name example "com.example.app" but what i need is the app name.
Another thing is that am getting the system app also which is not need, only the user running app details i need.
Thanks in advance any help or suggestion would be greatly appreciated.
public class ShowAppAdapter extends RecyclerView.Adapter<ShowAppAdapter.ShowAppAdapterViewHolder> {
private List<ActivityManager.RunningAppProcessInfo> appProcessInfos;
public ShowAppAdapter(List<ActivityManager.RunningAppProcessInfo> appProcessInfos){
this.appProcessInfos = appProcessInfos;
}
#NonNull
#Override
public ShowAppAdapterViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.appshow_layout, parent,false);
return new ShowAppAdapterViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ShowAppAdapterViewHolder holder, int position) {
String title = String.valueOf(appProcessInfos.get(position).processName);
holder.textView.setText(title);
}
#Override
public int getItemCount() {
return appProcessInfos.size();
}
public class ShowAppAdapterViewHolder extends RecyclerView.ViewHolder {
TextView textView;
ImageView imageView;
public ShowAppAdapterViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
imageView = itemView.findViewById(R.id.imageView);
}
}
}
this is my Adapter class.
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
PackageManager pm = this.getPackageManager();
List<String> appList=new ArrayList<>();
List<ActivityManager.RunningAppProcessInfo> allTasks = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo allTask : allTasks) {
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
allTask.processName, PackageManager.GET_META_DATA));
appList.add(c.toString());
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
Now pass appList into your adapter like below :
Make your Adapter like below
public class ShowAppAdapter extends RecyclerView.Adapter<ShowAppAdapter.ShowAppAdapterViewHolder> {
private List<String> appProcessInfos;
public ShowAppAdapter(List<String> appProcessInfos){
this.appProcessInfos = appProcessInfos;
}
#NonNull
#Override
public ShowAppAdapterViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.appshow_layout, parent,false);
return new ShowAppAdapterViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ShowAppAdapterViewHolder holder, int position) {
String title = appProcessInfos.get(position);
holder.textView.setText(title);
}
#Override
public int getItemCount() {
return appProcessInfos.size();
}
public class ShowAppAdapterViewHolder extends RecyclerView.ViewHolder {
TextView textView;
ImageView imageView;
public ShowAppAdapterViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
imageView = itemView.findViewById(R.id.imageView);
}
}
}

Usage time of all (individual) applications in android [Usage]

I tried executing the code below, but this gives the "Last Time Used" of the applications.
public class UsageListAdapter extends RecyclerView.Adapter<UsageListAdapter.ViewHolder> {
private List<CustomUsageStats> mCustomUsageStatsList = new ArrayList<>();
private DateFormat mDateFormat = new SimpleDateFormat();
/**
* Provide a reference to the type of views that you are using (custom ViewHolder)
*/
public static class ViewHolder extends RecyclerView.ViewHolder {
private final TextView mPackageName;
private final TextView mLastTimeUsed;
private final ImageView mAppIcon;
public ViewHolder(View v) {
super(v);
mPackageName = (TextView) v.findViewById(R.id.textview_package_name);
mLastTimeUsed = (TextView) v.findViewById(R.id.textview_last_time_used);
mAppIcon = (ImageView) v.findViewById(R.id.app_icon);
}
public TextView getLastTimeUsed() {
return mLastTimeUsed;
}
public TextView getPackageName() {
return mPackageName;
}
public ImageView getAppIcon() {
return mAppIcon;
}
}
public UsageListAdapter() {
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.usage_row, viewGroup, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
viewHolder.getPackageName().setText(
mCustomUsageStatsList.get(position).usageStats.getPackageName());
long lastTimeUsed = mCustomUsageStatsList.get(position).usageStats.getLastTimeUsed();
viewHolder.getLastTimeUsed().setText(mDateFormat.format(new Date(lastTimeUsed)));
viewHolder.getAppIcon().setImageDrawable(mCustomUsageStatsList.get(position).appIcon);
}
#Override
public int getItemCount() {
return mCustomUsageStatsList.size();
}
public void setCustomUsageStatsList(List<CustomUsageStats> customUsageStats) {
mCustomUsageStatsList = customUsageStats;
}
}
So can anyone modify this and get the time used of each application (in foreground/background) and display it.

Using ParseRecyclerQueryAdapter

Please I would like someone to help point me in the right direction with using the ParseRecyclerQueryAdapter. I'm trying to retrieve articles with my current adapter class that implements the standard RecyclerView. my implementation of pagination is not working properly soIi would like a demonstration from anyone that has used the ParseRecyclerQueryAdapter to achieve the following. By translating it from me so I get the implementation.
public class ArticleRVAdapter extends RecyclerView.Adapter {
List<Article> articleData = Collections.emptyList();
private LayoutInflater inflater;
private Context context;
public ArticleRVAdapter(Context context, List<Article> data){
inflater=LayoutInflater.from(context);
this.articleData= data;
this.context = context;
}
#Override
public ArticleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.article_item, parent, false);
ArticleViewHolder viewHolder = new ArticleViewHolder(view);
return viewHolder;
}
#Override
public int getItemCount() {
return articleData.size();
}
public class ArticleViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
CardView cardView;
TextView title;
TextView body;
Date dateCreated;
TextView author;
ImageView headImage;
String objectId;
public ArticleViewHolder(final View itemView) {
super(itemView);
itemView.setClickable(true);
itemView.setLongClickable(true);
itemView.setOnClickListener(this);
cardView = (CardView) itemView.findViewById(R.id.card_article);
title = (TextView) itemView.findViewById(R.id.text_title);
body = (TextView) itemView.findViewById(R.id.text_article_body);
//dateCreated = itemView.findViewById(R.id.text_article_body);
author = (TextView) itemView.findViewById(R.id.text_author);
headImage = (ImageView) itemView.findViewById(R.id.imageView_thumb);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), ArticleInformationActivity.class);
intent.putExtra("article_id", articleData.get(getLayoutPosition()).getObjectId());
intent.putExtra("article_title", articleData.get(getLayoutPosition()).getArticleTitle());
intent.putExtra("article_body", articleData.get(getLayoutPosition()).getArticleBody());
intent.putExtra("article_author", articleData.get(getLayoutPosition()).getArticleAuthor());
intent.putExtra("article_date", articleData.get(getLayoutPosition()).getDateCreated());
v.getContext().startActivity(intent);
}
}
After a lot of testing, I got the solution in my project:
Fragment (or Activity):
private RecyclerView mRecyclerView;
private MyQueryAdapter myQueryAdapter;
private SwipeRefreshLayout swipeRefreshLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_fragment, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.rv);
myQueryAdapter = new MyQueryAdapter(true);
mRecyclerView.setAdapter(myQueryAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
myQueryAdapter.addOnQueryLoadListener(new ParseRecyclerQueryAdapter.OnQueryLoadListener<MY_MODEL>() {
#Override
public void onLoaded(List<MY_MODEL> objects, Exception e) {
if (swipeRefreshLayout.isRefreshing()) {
swipeRefreshLayout.setRefreshing(false);
}
}
#Override
public void onLoading() {
if (!swipeRefreshLayout.isRefreshing()) {
swipeRefreshLayout.setRefreshing(true);
}
}
});
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
myQueryAdapter.loadObjects();
}
});
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onResume() {
super.onResume();
myQueryAdapter.loadObjects();
}
Adapter
public class MyQueryAdapter extends ParseRecyclerQueryAdapter<MY_MODEL, MyQueryAdapter.MyViewHolder>{
public MyQueryAdapter(boolean hasStableIds) {
super(MY_MODEL.class, hasStableIds);
}
public MarcacionesAdapter(ParseQueryAdapter.QueryFactory<Marcaciones> factory, boolean hasStableIds) {
super(factory, hasStableIds);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(v);
return myViewHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
MY_MODEL model = getItem(position);
holder.name.setText(model.getName());
holder.year.setText(model.getYear());
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView name;
TextView year;
MyViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.card_view);
name = (TextView)itemView.findViewById(R.id.name);
year = (TextView)itemView.findViewById(R.id.year);
}
}
}
If you want to configure a custom query, you can do it in this way:
factory =
new ParseQueryAdapter.QueryFactory<MY_MODEL>() {
public ParseQuery< MY_MODEL > create() {
ParseQuery<MY_MODEL> query = MY_MODEL.getQuery();
query.include("user");
//Filter by date
query.whereGreaterThan("createdAt", midnight);
query.whereLessThan("createdAt", now);
query.orderByDescending("createdAt");
return query;
}
};
adapter = new MyQueryAdapter(factory, true);

Picasso Library Not Working In RecyclerView in android

So im having this issue in my adapter to were when i go to set the Picasso method to convert my image url it will not allow me to pass the context no matter how i do it. i Have tried this and The class name .this neither seem to work. Not sure what or why this is happening. Here is the adapter class that im having issues with.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ContentViewHolder> {
public content[] mDataset;
public MyAdapter(content[] data) {
mDataset = data;
}
#Override
public ContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.test, parent, false);
ContentViewHolder viewHolder = new ContentViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ContentViewHolder holder, int position) {
holder.bindContent(mDataset[position]);
}
#Override
public int getItemCount() {
return mDataset.length;
}
public class ContentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView mUrl;
public TextView mTitle;
public TextView mDate;
public TextView mAuthor;
public ImageView mThumbnail;
public ContentViewHolder(View itemView) {
super(itemView);
mUrl= (TextView) itemView.findViewById(R.id.url);
mTitle = (TextView) itemView.findViewById(R.id.title);
mDate = (TextView) itemView.findViewById(R.id.date);
mAuthor = (TextView) itemView.findViewById(R.id.author);
mThumbnail =(ImageView)itemView.findViewById(R.id.thumbnail);
}
public void bindContent(content bloginfo) {
mUrl.setText(bloginfo.getUrl());
mTitle.setText(bloginfo.getTitle());
mDate.setText(bloginfo.getDate());
mAuthor.setText(bloginfo.getAuthor());
Picasso.with(context).load(bloginfo.getThumbnail()).into(mThumbnail);
}
#Override
public void onClick(View view) {
}
}
}
Ok so i got this to work here is the working adapter code below. Thank you all for your help.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ContentViewHolder> {
public content[] mDataset;
private Activity activityContext;
public MyAdapter(Activity context,content[] data) {
mDataset = data;
activityContext = context;
}
#Override
public ContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.test, parent, false);
ContentViewHolder viewHolder = new ContentViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ContentViewHolder holder, int position) {
holder.bindContent(mDataset[position]);
}
#Override
public int getItemCount() {
return mDataset.length;
}
public class ContentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView mUrl;
public TextView mTitle;
public TextView mDate;
public TextView mAuthor;
public ImageView mThumbnail;
public ContentViewHolder(View itemView) {
super(itemView);
mUrl= (TextView) itemView.findViewById(R.id.url);
mTitle = (TextView) itemView.findViewById(R.id.title);
mDate = (TextView) itemView.findViewById(R.id.date);
mAuthor = (TextView) itemView.findViewById(R.id.author);
mThumbnail =(ImageView)itemView.findViewById(R.id.thumbnail);
}
public void bindContent(content bloginfo) {
mUrl.setText(bloginfo.getUrl());
mTitle.setText(bloginfo.getTitle());
mDate.setText(bloginfo.getDate());
mAuthor.setText(bloginfo.getAuthor());
Picasso.with(activityContext).load(bloginfo.getThumbnail()).into(mThumbnail);
}
#Override
public void onClick(View view) {
}
}
}
This
Picasso.with(MyAdapter.this)
should be
Picasso.with(context)
Use Activity Context. This can be passed to the constructor of adapter class from activity.
http://developer.android.com/reference/android/content/Context.html
http://square.github.io/picasso/
Samples available on github
https://github.com/square/picasso/blob/master/picasso-sample/src/main/java/com/example/picasso/PicassoSampleAdapter.java
In your adapter you can make:
private Activity activityContext;
then in Adapter contructor:
public MyAdapter(Activity context, content[] data) {
mDataset = data;
activityContext = context;
}
Finally call:
Picasso.with(activityContext).load(bloginfo.getThumbnail()).into(mThumbnail);
If you create your Adapter in your Activity you need to create it using:
new myAdapter(this, YOURDATA), if you create it in Fragment, you need to use new myAdapter(getActivity(), YOURDATA).
Picasso.with(mThumbnail.getContext()).load(bloginfo.getThumbnail()).into(mThumbnail);

Categories

Resources