firebase recycler adapter doesnt load images but show data - android

I am beginner in android studio I am trying to load data from firebase Database into recyclerview but I have a problem in below code that RecyclerView doesn't load images but show data..and android studio doesn't show any error and I also search in google but I did not find any solution..
Now below is my code...Thanku in andvance
Adapter class
private Context mContext;
private List<Upload> mUpload;
public ImageAdapter(Context context, List<Upload> uploads) {
mContext = context;
mUpload = uploads;
}
//this method will create views
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(mContext).inflate(R.layout.image_item ,parent,false);
return new ImageViewHolder(v);
}
//this method is used for databinding that binds our data with views
#Override
public void onBindViewHolder(#NonNull ImageViewHolder holder, int position) {
Upload uploadCurrent=mUpload.get(position);
holder.textViewName.setText(uploadCurrent.getmName());
Picasso.with(mContext)
.load(uploadCurrent.getmImageUrl())
.fit()
.centerCrop()
.into(holder.imageView);
}
#Override
public int getItemCount() {
return mUpload.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder{
public TextView textViewName;
public ImageView imageView;
public ImageViewHolder(View itemView) {
super(itemView);
textViewName=itemView.findViewById(R.id.text_view_name);
imageView=itemView.findViewById(R.id.image_view_upload);
}
}
Upload calss
public class Upload {
private String mName;
private String mImageUrl;
public Upload () {}
public Upload(String name, String imageUrl) {
if(name.trim().equals(""))
{
name="No name";
}
this.mName = name;
this.mImageUrl = imageUrl;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
public String getmImageUrl() {
return mImageUrl;
}
public void setmImageUrl(String mImageUrl) {
this.mImageUrl = mImageUrl;
}
}
This is MainAvtivity/ImageActivity
public class ImagesActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ImageAdapter mAdapter;
private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_images);
mRecyclerView=findViewById(R.id.recyler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mUploads=new ArrayList<>();
mDatabaseRef= FirebaseDatabase.getInstance().getReference("uploads");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren())
{
Upload upload=postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter=new ImageAdapter(ImagesActivity.this,mUploads);
mRecyclerView.setAdapter(mAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ImagesActivity.this,databaseError.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
}

I am unable to comment but did you check that List which is inside Adapter has Photo URL?
change
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren())
{
Upload upload=postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter=new ImageAdapter(ImagesActivity.this,mUploads);
mRecyclerView.setAdapter(mAdapter);
}
into
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren())
{
Upload upload=postSnapshot.getValue(Upload.class);
mAdapter.list.add(upload);
mAdapter.notifyDataSetChanged();
}
}
Adapter class
private Context mContext;
ArrayList<Upload> list= new ArrayList<>();
public mAdapter(Context context ) {
mContext = context;
}
MAinActivity
mAdapter=new ImageAdapter(ImagesActivity.this);
mRecyclerView.setAdapter(mAdapter);
The logic behind this is that Adapterclass has a Arraylist. When you obtain data from background Thread on onDataChange. You add it to list directly and notify adapter that dataset has changed

Related

Images showing slow from firebase realtime database

I am creating an Android app which is based on wallpaper. I have different categories in my app that shows images and after clicking on particular image it will show full image. The problem is when I click on any categories the images are shown are very slow. I am retrieving images from firebase realtime database. I am showing images in grid layout in two columns. I have also compress all the images then to the process of showing images is very slow.
This is one of the category code.
public class AnimalWallpaper extends AppCompatActivity {
private RecyclerView recyclerView;
private ProgressBar progressBar;
private DatabaseReference reference;
private ArrayList<String> list;
private WallpaperAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animal_wallpaper);
reference = FirebaseDatabase.getInstance().getReference().child("animal");
recyclerView = findViewById(R.id.recyclerViewAnimal);
progressBar = findViewById(R.id.progressBarAnimal);
getData();
}
private void getData() {
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull #NotNull DataSnapshot snapshot) {
progressBar.setVisibility(View.GONE);
list = new ArrayList<>();
for (DataSnapshot shot : snapshot.getChildren()) {
String data = shot.getValue().toString();
list.add(data);
}
recyclerView.setLayoutManager(new GridLayoutManager(AnimalWallpaper.this, 2));
adapter = new WallpaperAdapter(list, AnimalWallpaper.this);
recyclerView.setAdapter(adapter);
progressBar.setVisibility(View.GONE);
}
#Override
public void onCancelled(#NonNull #NotNull DatabaseError error) {
progressBar.setVisibility(View.GONE);
Toast.makeText(AnimalWallpaper.this, "Error : "+error.getMessage() , Toast.LENGTH_SHORT).show();
}
});
}
}
This is wallpaperadapter class code.
public class WallpaperAdapter extends RecyclerView.Adapter<WallpaperAdapter.WallpaperViewHolder> {
private ArrayList<String> list;
private Context context;
public WallpaperAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
#NonNull
#org.jetbrains.annotations.NotNull
#Override
public WallpaperViewHolder onCreateViewHolder(#NonNull #org.jetbrains.annotations.NotNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_image_layout, parent, false );
return new WallpaperViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull #org.jetbrains.annotations.NotNull WallpaperAdapter.WallpaperViewHolder holder, int position) {
Glide.with(context).load(list.get(position)).into(holder.imageView);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, FullImageActivity.class);
intent.putExtra("images", list.get(position));
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return list.size();
}
public class WallpaperViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public WallpaperViewHolder(#NonNull #NotNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.item_image);
}
}
}

How not to show user that is online in recyclerview

I am currently trying to create a messaging app.The users who have registered on to my app are showed in users tab but along with those users the user who is currently logged into my app also sees himself in users tab .I don't want user to see himself in users tab.
My code
UserAdapter.java
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
private Context mContext;
private List<User> mUsers;
private boolean ischat;
public UserAdapter(Context mContext, List<User> mUsers,boolean ischat) {
this.mContext = mContext;
this.mUsers = mUsers;
this.ischat=ischat;
}
#NonNull
#Override
public UserAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(mContext).inflate(R.layout.user_item,parent,false);
return new UserAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull UserAdapter.ViewHolder holder, int position) {
final User user=mUsers.get(position);
holder.username.setText(user.getFirst());
if (user.getImageURL().equals("default")){
holder.profile_image.setImageResource(R.mipmap.ic_launcher);
} else {
Glide.with(mContext).load(user.getImageURL()).into(holder.profile_image);
}
if (ischat){
if (user.getStatus().equals("online")){
holder.img_on.setVisibility(View.VISIBLE);
holder.img_off.setVisibility(View.GONE);
} else {
holder.img_on.setVisibility(View.GONE);
holder.img_off.setVisibility(View.VISIBLE);
}
} else {
holder.img_on.setVisibility(View.GONE);
holder.img_off.setVisibility(View.GONE);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(mContext, MessageActivity.class);
intent.putExtra("UserName",user.getFirst());
intent.putExtra("userid", user.getId());
mContext.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return mUsers.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView username;
public ImageView profile_image;
private ImageView img_on;
private ImageView img_off;
public ViewHolder(#NonNull View itemView) {
super(itemView);
username=itemView.findViewById(R.id.username);
profile_image=itemView.findViewById(R.id.profile_image);
img_on = itemView.findViewById(R.id.img_on);
img_off = itemView.findViewById(R.id.img_off);
}
}
}
UsersFragment.java
public class UsersFragment extends Fragment {
private RecyclerView recyclerView;
private UserAdapter mUserAdapter;
private List<User> mUsers;
String TAG = "MyTag";
public UsersFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_users, container, false);
recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mUsers = new ArrayList<>();
readUser();
return view;
}
private void readUser() {
final FirebaseUser firebaseUser=FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Users");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
mUsers.clear();
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
User user=snapshot.getValue(User.class);
mUsers.add(user);
}
mUserAdapter=new UserAdapter(getContext(),mUsers,false);
recyclerView.setAdapter(mUserAdapter);
mUserAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
Make condition to check if whether current login user is not not equals to the key node then it will add users in array
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
mUsers.clear();
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
if(!FirebaseAuth.getInstance().getCurrentUser().getUid().equals(snapshot.getId())){
User user=snapshot.getValue(User.class);
mUsers.add(user);
}
}
mUserAdapter=new UserAdapter(getContext(),mUsers,false);
recyclerView.setAdapter(mUserAdapter);
mUserAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

How to fetch Uploaded Images from Firebase Storage

I've been following this tutorial link on youtube about how I can upload and retrieve images. Everything was going great until I got to the part where you show the images. My images are not showing. I don't get any erros and or warning messages I just get a blank page when I run my app. Below is my code. Thanks in advance.
//UsersAcivity class
private RecyclerView recyclerView;
private UploadAdapter uploadAdapter;
private DatabaseReference dbReference;
private List<UploadModel> uploads;
FirebaseStorage firebaseStorage;
StorageReference storageR;
private ImageView imageView;
FirebaseAuth firebaseAuth;
FirebaseUser firebaseUser;
FirebaseDatabase firebaseDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
overridePendingTransition(R.anim.slide_right, R.anim.slide_left);
uploads = new ArrayList<>();
mAdapter = new ImageAdapter(getApplicationContext(), uploads);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mAdapter);
uploadAdapter.notifyDataSetChanged();
dbReference = FirebaseDatabase.getInstance().getReference("Users");
String userid = FirebaseAuth.getInstance().getCurrentUser().getUid();
firebaseStorage = FirebaseStorage.getInstance();
storageR = firebaseStorage.getReference();
firebaseUser = firebaseAuth.getInstance().getCurrentUser();
firebaseDatabase.getInstance().getReference();
dbReference.orderByChild(userid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
UploadModel uploadModel = postsnapshot.getValue(Upload.class);
Uploads.add(uploadModel);
}
uploadAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(UsersActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
//UploadModel class
public class UploadModel {
private String image;
public UploadModel(){
//Empty constructor needed
}
public UploadModel (String imageUrl){
images=imageUrl;
}
public String getImageUrl() {
return image;
}
public void setImageUrl(String image) {
this.image = image;
}
}
UploadAdapter class
public class UploadAdapter extends RecyclerView.Adapter<UploadAdapter.ViewHolder>{
private Context ctext;
private List<Upload> list;
public UploadAdapter(Context context,List<Upload> uploads){
ctext = context;
list = uploads;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View V = LayoutInflater.from(ctext).inflate(R.layout.cardview, parent, false);
return new ViewHolder(V);
}
#Override
public void onBindViewHolder(#NonNull ImageViewHolder holder, int position) {
Upload uploadCurrent=list.get(position);
Picasso.get().load(uploadCurrent.getImageUrl()).fit().centerCrop().into(holder.imageView);
}
#Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public View view;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.imageview);
}
}
}
Your not getting any response from Firebase Realtime Database because of the path you provided.
dbReference.child("LoqCOxqzhpN3puCWwhYDtHJVXqg2").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
String imageUrl= (String) postsnapshot.child("ImageUrl").getValue();
Uploads.add(imageUrl);
}
uploadAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(UsersActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
Your orderByChild() finding id under the Users.

Recyclerview is not showing any data which i have in firebase. Iam able to retrieve the data

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.

Unable to retrieve data from firebase database due to async nature of firebase dabase

My ArrayList Seems to be empty when i try to retrieve data for first time due to async nature of firebase API,with second attempt to load data Its loaded properly,here is code related to the same.
Method which is used to retrieve data
AdvertisementRef.orderByChild("Category").equalTo(categoryS).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {
SaveAddInformation mModel = eventSnapshot.getValue(SaveAddInformation.class);
Log.d("DATA" ,""+ mModel);
adinfolist.add(mModel);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getMessage());
}
});
return adinfolist;
}
MainActivity
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setHasFixedSize(true);
advertismentref= FirebaseDatabase.getInstance().getReference("ADVERTISEMENT INFORMATION");
helper=new Firebase_helper(advertismentref);
adapter=new AdvertisementAdapter(helper.retrieve(/*catS*/),this);
// adapter = new AdvertisementAdapter(this, helper.retrieve());
rv.setAdapter(adapter);
ViewHolder and Adapter Classes
public class AdvertisementAdapter extends RecyclerView.Adapter <AdvertisementAdapter.AdvetisementViewHolder>{
ArrayList<SaveAddInformation>saveAddInformations=new ArrayList<SaveAddInformation>();
Context ctx;
public AdvertisementAdapter(ArrayList<SaveAddInformation> saveAddInformations,Context ctx) {
this.saveAddInformations = saveAddInformations;
this.ctx=ctx;
}
#Override
public AdvertisementAdapter.AdvetisementViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.design_row,parent,false);
AdvetisementViewHolder myViewholder=new AdvetisementViewHolder(v,ctx,saveAddInformations);
return myViewholder;
}
#Override
public void onBindViewHolder(AdvertisementAdapter.AdvetisementViewHolder holder, int position) {
SaveAddInformation save=saveAddInformations.get(position);
String title=save.getTitleS();
holder.titleTv.setText(title);
String rentamt=save.getRent_amount();
holder.rentamt.setText(rentamt);
String rentdays=save.getRentDaysSpin();
holder.rentdays.setText(rentdays);
String description=save.getProductDetails();
holder.desc.setText(description);
String imguri=save.getImageuri();
holder.setImage(getApplicationContext(), imguri);
}
#Override
public int getItemCount() {
return saveAddInformations.size();
}
public static class AdvetisementViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView titleTv,rentamt,rentdays,desc;
Context ctx;
ImageView adimg;
ArrayList<SaveAddInformation>saveAddInformations=new ArrayList<SaveAddInformation>();
public AdvetisementViewHolder(View itemView,Context ctx,ArrayList<SaveAddInformation>saveAddInformations) {
super(itemView);
this.saveAddInformations=saveAddInformations;
itemView.setOnClickListener(this);
this.ctx=ctx;
titleTv=(TextView)itemView.findViewById(R.id.Titletextv);
rentamt=(TextView)itemView.findViewById(R.id.rent_amount);
rentdays=(TextView)itemView.findViewById(R.id.days_rent);
desc=(TextView)itemView.findViewById(R.id.descTV);
}
public void setImage(Context applicationContext, String imguri) {
adimg=(ImageView)itemView.findViewById(R.id.ad_image);
// We Need TO pass Context
Picasso.with(applicationContext).load(imguri).into(adimg);
}
I have just moved retrieve method into main activity and called adapter.notifyDataSetChanged(); in onDataChange and onCancelled method and it worked!!!

Categories

Resources