How to fetch Uploaded Images from Firebase Storage - android

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.

Related

android recylerview not displaying firebase data

I am trying to retrieve data from another firebase project, but the data is not displaying in the recylerview. I am not getting any errors but just a blank cardview. Can someone help me solve this issue that I am having. My code is below.
// This my code
public class ExampleFragment extends Fragment {
LinearLayout linearLayoutWithoutItems,linearLayoutNoConnection;
View rootView;
private RecyclerView recyclerView;
private Adapter1 aAdapter;
ImageButton menu_click;
ImageView imageView;
//Variables
NavigationView navigationView;
Toolbar toolbar;
Menu menu;
LinearLayout linearLayout;
TextView textView;
BottomNavigationView bottomNavigationView;
private DatabaseReference databaseReference;
private ArrayList<Model_Information> myUploads;
FirebaseStorage firebaseStorage;
StorageReference storageReference;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView =inflater.inflate(R.layout.fragment_example, container, false);
init();
return rootView;
}
public void init()
{
recyclerView = rootView.findViewById(R.id.WithItems_recyclerview);
linearLayoutWithoutItems = rootView.findViewById(R.id.WithoutItems);
linearLayoutNoConnection = rootView.findViewById(R.id.no_connection);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(),1);
recyclerView.setLayoutManager(layoutManager);
myUploads = new ArrayList<Model_Information>();
aAdapter = new Adapter1(getContext(), myUploads);
recyclerView.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
databaseReference = FirebaseDatabase.getInstance("https://liou-43081.firebaseio.com").getReference().child("Post");
if (InternetConnection.checkConnection(getContext())) {
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
//progressBar.setVisibility(View.GONE);
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
Model_Information upload=postsnapshot.getValue(Model_Information.class);
//myUploads.clear();
myUploads.add(upload);
aAdapter = new Adapter1(getContext(), myUploads);
recyclerView.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
recyclerView.invalidate();
}
linearLayoutWithoutItems.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
aAdapter.notifyDataSetChanged();
}else{
linearLayoutWithoutItems.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(getContext(), databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
} else {
linearLayoutNoConnection.setVisibility(View.VISIBLE);
linearLayoutWithoutItems.setVisibility(View.GONE);
recyclerView.setVisibility(View.GONE);
}
//Adapter1.class
public class Adapter1 extends RecyclerView.Adapter<Adapter1.ImageViewHolder>{
private Context mContext;
private ArrayList<Model_Information> users;
DatabaseReference databaseReference;
public Adapter1(Context context, ArrayList<Model_Information> uploads){
mContext = context;
users = uploads;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View V = LayoutInflater.from(mContext).inflate(R.layout.cardview1, parent, false);
return new ImageViewHolder(V);
}
#Override
public void onBindViewHolder(#NonNull final ImageViewHolder holder, final int position) {
//String uploadCurrent=users.get(position).getmImageUrl();
Glide.with(mContext).load(users.get(position).getmImageUrl()).thumbnail(0.05f).transition(DrawableTransitionOptions.withCrossFade()).fitCenter().centerInside().into(holder.imageView);
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(mContext,users.get(position).getCategory(), Toast.LENGTH_LONG).show();
Long l= Long.valueOf(1);
databaseReference = FirebaseDatabase.getInstance("https://louie-43081.firebaseio.com").getReference("Clicks_and_Views").child(users.get(position).id);
databaseReference.child("views").setValue(ServerValue.increment(l));
/*
Intent intent=new Intent(mContext,ViewActivity.class);
intent.putExtra("website",users.get(position).getWebsiteurl());
intent.putExtra("action",users.get(position).getAction());
intent.putExtra("image",users.get(position).getmImageUrl());
intent.putExtra("id",users.get(position).id);
mContext.startActivity(intent);
*/
}
});
}
#Override
public int getItemCount() {
return users.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder{
public ImageView imageView;
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.image);
}
}
You should not initialize adapter twice.
Your code block should look like this,
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
Model_Information upload=postsnapshot.getValue(Model_Information.class);
myUploads.add(upload);
aAdapter.notifyDataSetChanged();
recyclerView.invalidate();
}

how to retrieve data from Firebase Database from different nodes using Firebase Recycler View using onBindViewHolder

There are two nodes
Friends->From which I am getting date by using (Friends class)
User -> From which I am getting userName , profile Image
I am using bindViewHolder method. This code does not give me any error but when I run the app and click on friends my app crashed
public class FriendsActivity extends AppCompatActivity {
private RecyclerView myFriendsList;
//Firebase
private DatabaseReference friendsRef , usersRef;
private FirebaseAuth mAuth;
private String onlineUserId;
private String userName , profileImage ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friends);
//Firebae
mAuth = FirebaseAuth.getInstance();
onlineUserId = mAuth.getCurrentUser().getUid();
friendsRef = FirebaseDatabase.getInstance().getReference().child("Friends").child(onlineUserId);
usersRef = FirebaseDatabase.getInstance().getReference().child("Users");
myFriendsList = (RecyclerView)findViewById(R.id.friends_list);
myFriendsList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
myFriendsList.setLayoutManager(linearLayoutManager);
friendList();
}
private void friendList()
{
FirebaseRecyclerOptions<Friends> options =
new FirebaseRecyclerOptions.Builder<Friends>()
.setQuery(friendsRef, Friends.class)
.build();
FirebaseRecyclerAdapter<Friends , FriendsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Friends, FriendsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull FriendsViewHolder FriendsViewHolder, final int i, #NonNull Friends Friends)
{
final String postKey = getRef(i).getKey();
final String userIds = getRef(i).getKey();
FriendsViewHolder.date.setText(Friends.getDate());
usersRef.child(userIds).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
if(dataSnapshot.exists())
{
userName = dataSnapshot.child("fullName").getValue().toString();
profileImage = dataSnapshot.child("profile image").getValue().toString();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
FriendsViewHolder.fullname.setText(userName);
Picasso.get().load(profileImage).into(FriendsViewHolder.search_profile_image);
}
#NonNull
#Override
public FriendsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.all_user_display_layout , parent,false);
FriendsViewHolder viewHolder = new FriendsViewHolder(view);
return viewHolder;
}
};
myFriendsList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
}
#Override
protected void onStart()
{
super.onStart();
}
public static class FriendsViewHolder extends RecyclerView.ViewHolder
{
CircleImageView search_profile_image;
TextView date , fullname;
public FriendsViewHolder(#NonNull View itemView)
{
super(itemView);
search_profile_image = itemView.findViewById(R.id.all_users_profile_image);
fullname = itemView.findViewById(R.id.all_users_profile_fullname);
date= itemView.findViewById(R.id.all_users_status);
}
}
}

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 do I make recyclerView that retrives data from firebase, using getKey()?

I would like to know how can I do a recyclerView that shows data that I got from this database reference:
Salas= new ArrayList<String>();
DatabaseReference referenceSalas = FirebaseDatabase.getInstance().getReference("salas/");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("usuarios/");
FirebaseAuth autenticacao = FirebaseAuth.getInstance();
String emailUsu = autenticacao.getCurrentUser().getEmail();
reference.orderByChild("email").equalTo(emailUsu).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
nomeProf = datas.child("nome").getValue().toString();
referenceSalas.orderByChild("nomeProf").equalTo(nomeProf).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
Salas.add(datas.getKey());
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
I tried to do the RecyclerView like this:
public class salasFragment extends Fragment {
private RecyclerView mRecycleViewSalas;
private adapterSalas adapterSalas;
private ArrayList<salas> listaSalas= new ArrayList<>();
private LinearLayoutManager linearLayoutManager;
private TextView txtSalas, txtTeste;
private String nomeProf;
private String teste="", piru;
private DatabaseReference reference = FirebaseDatabase.getInstance().getReference("usuarios/");
private DatabaseReference referenceSalas = FirebaseDatabase.getInstance().getReference("salas/");
private ValueEventListener valueEventListenerSalas;
ArrayList<String> salasAula = new ArrayList<String>();
public salasFragment() {
// 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_salas, container, false);
final Context context = view.getContext();
txtSalas= view.findViewById(R.id.txtSalas);
txtTeste= view.findViewById(R.id.txtTeste);
mRecycleViewSalas= view.findViewById(R.id.recyclerSalas);
adapterSalas= new adapterSalas(listaSalas, context);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
mRecycleViewSalas.setLayoutManager(layoutManager);
mRecycleViewSalas.setHasFixedSize(true);
mRecycleViewSalas.setAdapter(adapterSalas);
return view; }
#Override
public void onStart() {
super.onStart();
recuperarSalas();
}
#Override
public void onStop() {
super.onStop();
reference.removeEventListener(valueEventListenerSalas);
}
public void recuperarSalas(){
FirebaseAuth autenticacao = FirebaseAuth.getInstance();
String emailUsu = autenticacao.getCurrentUser().getEmail();
valueEventListenerSalas = reference.orderByChild("email").equalTo(emailUsu).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
nomeProf = datas.child("nome").getValue().toString();
referenceSalas.orderByChild("nomeProf").equalTo(nomeProf).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
salas salas=datas.getKey(salas.class);
listaSalas.add(salas);
}
adapterSalas.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
}
my adapter:
public class adapterSalas extends RecyclerView.Adapter<adapterSalas.myViewHolder> {
private List<salas> Salas;
private Context context;
public adapterSalas(List<salas> listaSalas, Context c ) {
this.Salas= listaSalas;
this.context = c;
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
View itemLista = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_salas, parent, false);
return new myViewHolder(itemLista);
}
#Override
public void onBindViewHolder(#NonNull myViewHolder holder, int position) {
salas sala = Salas.get(position);
holder.btn1.setText(sala.getPrimeiro());
}
#Override
public int getItemCount() {
return Salas.size();
}
public class myViewHolder extends RecyclerView.ViewHolder{
Button btn1;
public myViewHolder(#NonNull View itemView) {
super(itemView);
btn1 = itemView.findViewById(R.id.btn1);
}
}
}
Although this line "salas salas=datas.getKey(salas.class);" does not work properly when I use "getKey", It only works when "getValue" is used. There is no way of me doing this project with "getValue" instead of "getKey". So there is any way that can make this recyclerView works properly with "getKey" ?
Have you got the data from the firebase already?
If you got the data in an arraylist already, simply plug it in an adapter (you will need to create a RecyclerView adapter, a class to describe the value of each RecyclerView items) and set it to the RecyclerView and you are good to go!
You can use FirebasRecyclerAdapter as you recyclerView adapter. check out this link for a guide on how to use it.
https://medium.com/android-grid/how-to-use-firebaserecycleradpater-with-latest-firebase-dependencies-in-android-aff7a33adb8b

firebase recycler adapter doesnt load images but show data

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

Categories

Resources