I was trying to load data from firebase into my recycler view. There are two values that I am retrieving first is Name and second is the ImageUrl. When i run my app the value from the name is displaying on the recyclerview but the image is still blank. Somehow picasso is failing to load the image into the CircleImageView.
Below is my ViewHolderclass
public static class CategoryViewHolder extends RecyclerView.ViewHolder {
View mView;
CircleImageView categoryImageView;
TextView categoryName;
public CategoryViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setDisplayName(String name){
categoryName = (TextView) mView.findViewById(R.id.Category_name);
categoryName.setText(name);
}
public void setUserImage(String thumb_image, Context ctx){
categoryImageView = (CircleImageView)
mView.findViewById(R.id.Category_pic);
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.
default_avatar)
.into( categoryImageView);
// Picasso.with(ctx) .load(thumb_image) .into(categoryImageView);
}
}
this is my model class
public class CategoryItems {
private String Name;
private String ImageUrl;
public CategoryItems(String title, String imageURL) {
this.Name = title;
this.ImageUrl = imageURL;
}
publ ic CategoryItems(){
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImageUrl() {
return ImageUrl;
}
public void setImageUrl(String imageUrl) {
ImageUrl = imageUrl;
}
}
this is my FirebaseREcyclerViewAdapter
public void onStart() {
super.onStart();
startListening();
}
public void startListening(){
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("Catlist")
.limitToLast(500);
FirebaseRecyclerOptions<CategoryItems> options =
new FirebaseRecyclerOptions.Builder<CategoryItems>()
.setQuery(query, CategoryItems.class)
.build();
FirebaseRecyclerAdapter adapter = new
FirebaseRecyclerAdapter<CategoryItems, CategoryViewHolder>(options) {
#Override
public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.category_items, parent, false);
return new CategoryViewHolder(view);
}
#Override
protected void onBindViewHolder(final CategoryViewHolder holder, int
position, final CategoryItems model) {
holder.setDisplayName(model.getName());
holder.setUserImage(model.getImageUrl(), getContext());
// holder.setUserStatus(model.getStatus());
Log.d(model.getImageUrl().toString(), "onBindViewHolder: ");
}
};
mUsersList.setAdapter(adapter);
adapter.startListening();
}
my recycler is displaying names only and i tried to log the imageurls and its displaying the url fine
Try enabling logging in Picasso to look at errors and warning, it will definitely show some error in your case which you can resolve.
Picasso
.with(context)
.setLoggingEnabled(true)
....
For more information check out this link
Related
I have created an application and data of user is loaded via firebase recycler adapter. It have data of users and each user have a link in their profile which redirect to a webpage. Everything is working fine the user is redirected to the webpage via the default browser. My question is how can I restrict the app to open the links/urls within itself. I don't want the links to be opened in the default browser. Below is my code
MyViewholder
class MyViewHolder extends RecyclerView.ViewHolder {
TextView geglink, userName, userAbout;
View v;
public MyViewHolder(#NonNull #NotNull View itemView) {
super(itemView);
geglink = (TextView) itemView.findViewById(R.id.rcvGigLink);
userName = (TextView) itemView.findViewById(R.id.rcvName);
userAbout = (TextView) itemView.findViewById(R.id.rcvAbout);
v = itemView;
DashboardActivity
RecyclerView recyclerView;
FirebaseRecyclerOptions<ModelClass> options;
FirebaseRecyclerAdapter<ModelClass, MyViewHolder> adapter;
DatabaseReference dRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
dRef = FirebaseDatabase.getInstance().getReference().child("Users");
recyclerView = findViewById(R.id.dashboardRCV);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
LoadData();
}
private void LoadData() {
options = new FirebaseRecyclerOptions.Builder<ModelClass>()
.setQuery(dRef, ModelClass.class)
.build();
adapter = new FirebaseRecyclerAdapter<ModelClass, MyViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull MyViewHolder holder, int position, #NonNull ModelClass model) {
holder.geglink.setText(model.getGig());
holder.userName.setText(model.getName());
holder.userAbout.setText(model.getAbout());
holder.v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = model.getGig();
if (!url.startsWith("http://") && !url.startsWith("https://")){
url = "https://" + url;
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
}
});
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_layout, parent, false);
return new MyViewHolder(view);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
}
}
ModelClass
public class ModelClass {
String gig, name, about;
public String getGig() {
return gig;
}
public void setGig(String gig) {
this.gig = gig;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
public ModelClass(String gig, String name, String about) {
this.gig = gig;
this.name = name;
this.about = about;
}
public ModelClass() {
}
}
Thanks in advance
HISTOROBJECT.CLASS
public class HistoryObject{
private String mName;
private String mImageUrl;
public HistoryObject(){
}
public HistoryObject(String Name, String Image) {
this.user = Name;
this.msg = Image;
}
private String user, msg;
public String getMsg() {
return msg;
}
public void setMsg(String Name) {
this.msg = Name;
}
public String getUser() {
return user;
}
public void setUser(String Image) {
this.user = Image;
}
}
HISTORYADAPTER.CLASS
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ImageViewHolder> {
private Context mContext;
private List<HistoryObject> mUploads;
public HistoryAdapter (Context cx, List<HistoryObject> uploads){
mContext = cx;
mUploads = uploads;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.listofitems, parent, false);
return new ImageViewHolder(v); }
#Override
public void onBindViewHolder(#NonNull ImageViewHolder holder, int position) {
HistoryObject historyObject = mUploads.get(position);
holder.textView.setText(historyObject.getUser());
Picasso.get().load(historyObject.getMsg())
.placeholder(R.mipmap.ic_launcher)
.fit()
.centerCrop()
.into(holder.spapic);
}
#Override
public int getItemCount() {
return mUploads.size() ;
}
public class ImageViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ImageView spapic;
public ImageViewHolder(View itemView) {
super(itemView);
textView =(TextView)itemView.findViewById(R.id.nameofspa);
spapic =(ImageView) itemView.findViewById(R.id.spapic);
}
}
}
HOMEPAGE.CLASS
mDatabaseRef = FirebaseDatabase.getInstance().getReference("BeautyCenters");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
// Log.d("SPAA", "VALUE IS " +dataSnapshot) ;
Log.d("SPAA", "postsnap IS " +postSnapshot) ;
HistoryObject upload = postSnapshot.getValue(HistoryObject.class);
mUploads.add(upload);
}
mHistoryAdapter = new HistoryAdapter(HomePage.this, mUploads);
mHistoryRecyclerView.setAdapter(mHistoryAdapter);
mHistoryAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Iam able to retrieve data and can see all list in logcat but it is not displaying in recyclerview. I also want to retrieve the images from the firebase. iam able to show the adapter but the text is not showing in that adapter instead it is blank and also the picture
I have faced this problem before.
Try moving:
mHistoryRecyclerView.setAdapter(mHistoryAdapter);
mHistoryAdapter.notifyDataSetChanged();
out of the listener class to the bottom of onCreate.
If that doesn't work make sure you have this code added:
#Override
public void onStart() {
super.onStart();
if (mAdapter != null) {
mAdapter.startListening();
}
}
If it is still not working then provide a link of your project and I will play around with it.
I am trying to retrieve some data from Firebase to populate into my RecycleView but every time I attempt this my RecycleView appears empty.
I have already created my adapter and set up my Recycleview and attempted to load the data from Firebase but nothing shows up. When I try to log the information it comes back as null. Some help would be appreciated.
This is how my database is set up
Post
-Ljl__LUiOJ2YMAK5NHI
desc: "Product info"
id: "sH8LzoaH9UaXahlmssixTpvQy8q2"
image: "content://media/external/images/media/323211"
//Retrieves information stored inside Post node...
public void fetchUserInfo() {
postRef = FirebaseDatabase.getInstance().getReference().child("Post");
postRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
value = ds.getValue(Post.class);
postList.add(value);
}
adapter = new Adapter(Shop_Activity.this, postList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.i("Error", databaseError.toString());
}
});
}
Adapter :
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
Context context;
ArrayList<Post> userPost;
public Adapter(Context context, ArrayList<Post> userPost){
this.context = context;
this.userPost = userPost;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.shop_layout_design,viewGroup, false));
}
//this is where you set the value for the ui elements
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
viewHolder.title.setText(userPost.get(i).getDescription());
viewHolder.description.setText(userPost.get(i).getUserID());
Picasso.get().load(userPost.get(i).getPostImage()).into(viewHolder.postImage);
}
#Override
public int getItemCount() {
return userPost.size();
}
//links up ui elements
class ViewHolder extends RecyclerView.ViewHolder{
TextView title;
TextView description;
ImageView postImage;
public ViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.post_title);
description = itemView.findViewById(R.id.post_desc);
postImage = itemView.findViewById(R.id.post_image);
}
}
}
Post class
public class Post {
private String desc;
private String id;
private String image;
public Post(){
}
public void setdesc(String desc){
this.desc = desc;
}
public void setimage(String image){
this.image = image;
}
public void setid(String id){
this.id = id;
}
public String getdesc() {
return desc;
}
public String getimage(){
return image;
}
public String getid(){
return id;
}
}
adapter.notifyDataSetChanged();
add this after setting the adapter
I'm trying to show Blog Posts on my MainActivity (Home page). But Homepage Nothing shows I mean Homepage is empty.
Firebase database structure:
MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private RecyclerView mBlogList;
private DatabaseReference mDataRef;
private FirebaseRecyclerAdapter<BlogInfo, ViewHolder> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: started!");
mDataRef = FirebaseDatabase.getInstance().getReference().child("BlogApp").child("Posts");
mBlogList = findViewById(R.id.blogListId);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<BlogInfo> options = new FirebaseRecyclerOptions.Builder<BlogInfo>()
.setQuery(mDataRef, BlogInfo.class).build();
adapter = new FirebaseRecyclerAdapter<BlogInfo, ViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull BlogInfo model) {
String imgUrl = model.getImage();
Picasso.get().load(imgUrl).into(holder.post_image);
holder.post_title.setText(model.getTitle());
holder.post_desc.setText(model.getDescription());
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder: initialized!");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_blog_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
};
mBlogList.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
Log.d(TAG, "onStart: init");
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
Log.d(TAG, "onStop: stop Blog list");
}
// ViewHolder Class
public static class ViewHolder extends RecyclerView.ViewHolder{
private static final String TAG = "ViewHolder";
TextView post_title;
TextView post_desc;
ImageView post_image;
ViewHolder(View itemView) {
super(itemView);
Log.d(TAG, "ViewHolder: Initialized!");
post_title = itemView.findViewById(R.id.postTitleId);
post_desc = itemView.findViewById(R.id.postDescId);
post_image = itemView.findViewById(R.id.postImageId);
}
}
}
BlogInfo.class:
public class BlogInfo {
private String description;
private String image;
private String title;
public BlogInfo() {}
public BlogInfo(String title, String description, String image) {
this.title = title;
this.description = description;
this.image = image;
}
public String getTitle() {return title;}
public void setTitle(String title) {this.title = title;}
public String getDescription() {return description;}
public void setDescription(String description) {this.description = description;}
public String getImage() {return image;}
public void setImage(String image) {this.image = image;}
}
This is my first time I'm trying to use FirebaseRecyclerAdapter to retrieve data from Firebase Database. But I don't know where the problem is. I tried to debug also but the compiler won't enter into onCreateViewHolder or onBindViewHolder function. But I'm totally following FirebaseUI for Realtime Database reference.
To solve this, you need to remove the following line of code:
mBlogList.setHasFixedSize(true);
This is needed only when you want your RecyclerView to have a fixed size. In your case is not necessary.
I am able to retrieve text data successfully from firebase, but i got some difficulty in doing the same for images. I am using a recycler view and below is my recycler adapter:
public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.MyHolder>{
List<Listdata> listdata;
public RecyclerviewAdapter(List<Listdata> listdata) {
this.listdata = listdata;
}
#Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.myview,parent,false);
MyHolder myHolder = new MyHolder(view);
return myHolder;
}
public void onBindViewHolder(MyHolder holder, int position) {
Listdata data = listdata.get(position);
holder.vname.setText(data.getName());
holder.vemail.setText(data.getEmail());
holder.vaddress.setText(data.getAddress());
}
#Override
public int getItemCount() {
return listdata.size();
}
class MyHolder extends RecyclerView.ViewHolder{
TextView vname , vaddress,vemail;
ImageView thumbnail;
public MyHolder(View itemView) {
super(itemView);
vname = (TextView) itemView.findViewById(R.id.vname);
thumbnail = (ImageView) itemView.findViewById(thumbnail);
vemail = (TextView) itemView.findViewById(R.id.vemail);
vaddress = (TextView) itemView.findViewById(R.id.vaddress);
}
}
}
and here is activity code that initialises the process:
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
list = new ArrayList<>();
// StringBuffer stringbuffer = new StringBuffer();
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
Userdetails userdetails = dataSnapshot1.getValue(Userdetails.class);
Listdata listdata = new Listdata();
String name=userdetails.getName();
String email=userdetails.getEmail();
String address=userdetails.getAddress();
listdata.setName(name);
listdata.setEmail(email);
listdata.setAddress(address);
list.add(listdata);
// Toast.makeText(MainActivity.this,""+name,Toast.LENGTH_LONG).show();
}
RecyclerviewAdapter recycler = new RecyclerviewAdapter(list);
RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(InvestmentDrawer.this);
recyclerview.setLayoutManager(layoutmanager);
recyclerview.setItemAnimator( new DefaultItemAnimator());
recyclerview.setAdapter(recycler);
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
// Log.w(TAG, "Failed to read value.", error.toException());
}
});
Glide.with(this)
.load(imageData)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(ivImgGlide);
}
I would like to use the url entered in the database to load the images dynamically. Using glide or picasso. Below is the Listdata class
public class Listdata {
public String name;
public String address;
public String email;
public String imageurl;
public String getImageUrl() {
return imageurl;
}
public void SetImageUrl(String imageurl) {
this.imageurl = imageurl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
You can use Picasso or glide inside your onBindViewHolder
public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.MyHolder>{
List<Listdata> listdata;
Context context;
public RecyclerviewAdapter(List<Listdata> listdata,Context context) {
this.listdata = listdata;
this.context = context;
}
#Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.myview,parent,false);
MyHolder myHolder = new MyHolder(view);
return myHolder;
}
public void onBindViewHolder(MyHolder holder, int position) {
Listdata data = listdata.get(position);
holder.vname.setText(data.getName());
holder.vemail.setText(data.getEmail());
holder.vaddress.setText(data.getAddress());
Glide.with(context)
.load(data.getImageUrl())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.thumbnail);
//with Picasso
Picasso.with(context)
.load(data.getImageUrl())
.into(holder.thumbnail);
// you need to define a method getImageUrl() to get the image url in Listdata
}
#Override
public int getItemCount() {
return listdata.size();
}
class MyHolder extends RecyclerView.ViewHolder{
TextView vname , vaddress,vemail;
ImageView thumbnail;
public MyHolder(View itemView) {
super(itemView);
vname = (TextView) itemView.findViewById(R.id.vname);
thumbnail = (ImageView) itemView.findViewById(thumbnail);
vemail = (TextView) itemView.findViewById(R.id.vemail);
vaddress = (TextView) itemView.findViewById(R.id.vaddress);
}
}
}
Remember you need to pass context from your parent activity into your adapter
In you onDataChanged you need to set the Image url using SetImageUrl()
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
list = new ArrayList<>();
// StringBuffer stringbuffer = new StringBuffer();
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
Userdetails userdetails = dataSnapshot1.getValue(Userdetails.class);
Listdata listdata = new Listdata();
String name=userdetails.getName();
String email=userdetails.getEmail();
String address=userdetails.getAddress();
String imageUrl = userdetails.getImageUrl();
listdata.setName(name);
listdata.setEmail(email);
listdata.setAddress(address);
listdata.SetImageUrl(imageUrl);
list.add(listdata);
// Toast.makeText(MainActivity.this,""+name,Toast.LENGTH_LONG).show();
}
RecyclerviewAdapter recycler = new RecyclerviewAdapter(list,getApplicationContext());
RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(InvestmentDrawer.this);
recyclerview.setLayoutManager(layoutmanager);
recyclerview.setItemAnimator( new DefaultItemAnimator());
recyclerview.setAdapter(recycler);
}