0

This is my firebase storage image and I want to display all images from url into gridview by taking the current user id

This is my main activity onstart() that leads to adapter class. I want to show all the images under current user id like grid view.

How to display all user image posts in grid view from firebase above image.

@Override
protected void onStart() {
    super.onStart();
    FirebaseUser currentUser = mAuth.getCurrentUser();
    uploads = new ArrayList<Blog>();
    if (currentUser != null && !currentUser.isAnonymous()) {
        mUserf.child("online").setValue("true");
        mProgressbar.setMessage("Welcome.....");
        mProgressbar.show();
        mProgressbar.dismiss();
        ConnectivityManager check = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);

        NetworkInfo info = check.getActiveNetworkInfo();            //for checking whether app is connected to a network or not..
        if(info!=null && info.isConnected()) {
            //Toast.makeText(MainActivity.this, "network available", Toast.LENGTH_SHORT).show();
            firebaseAuth.addAuthStateListener(authStateListener);
            mProgressbar.setMessage("Fetching Blogs.....");
            mProgressbar.show();
            mProgressbar.dismiss();
            list = new ArrayList<>();
            adapter = new MyCustomAdapter(this,list,firebaseAuth,mdatabaseReference,likesdatabaseReference);
            recyclerView = (RecyclerView) findViewById(R.id.blog_list);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setAdapter(adapter);
            alpha = FirebaseDatabase.getInstance().getReference().child("Blog").child(mAuth.getCurrentUser().getUid());


            mchildEventListener = new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    mProgressbar.dismiss();


                    String j = dataSnapshot.getKey();
                    Log.v("GETKEYZZZ", j);
                    Blog friendlyMessage = dataSnapshot.getValue(Blog.class);
                    friendlyMessage.setPostkey(j);
                    list.add(friendlyMessage);
                    adapter.notifyDataSetChanged();

                }

                public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
                public void onChildRemoved(DataSnapshot dataSnapshot){}
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
                public void onCancelled(DatabaseError databaseError) {}
            };
            mquery.addChildEventListener(mchildEventListener);

        } else {
            //Toast.makeText(MainActivity.this, "No network available", Toast.LENGTH_SHORT).show();

            Toast.makeText(getApplicationContext(),"Please Check Your Internet Connection", Toast.LENGTH_LONG).show();
        }
       // mDatabseUsers.child("online").setValue("true");
    } else {
        Intent user1 = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(user1);
    }
}

This is my adapter class where

public class MyCustomAdapter extends RecyclerView.Adapter<MyCustomAdapter.myviewholder> {

    Context context;
    ArrayList<Blog> list;
    List <String> imageUrls;
    //List<String> imageUrls ;
    LayoutInflater inflator;
    int lastPosition = 1;
    boolean mprogressLike = true,mprogressview = true;
    DatabaseReference likeDatabaseReference;
    FirebaseAuth firebaseAuth;
    DatabaseReference blogDatabaseReference;


    public MyCustomAdapter(Context context, ArrayList<Blog> list,FirebaseAuth firebaseAuth,DatabaseReference blogDatabaseReference, DatabaseReference likeDatabaseReference) {

        this.context=context;
        this.list=list;
        inflator=LayoutInflater.from(context);
        this.likeDatabaseReference=likeDatabaseReference;
        this.firebaseAuth=firebaseAuth;
        this.blogDatabaseReference=blogDatabaseReference;
    }
    @Override
    public myviewholder onCreateViewHolder(ViewGroup parent, int position) {

        View v=inflator.inflate(R.layout.posts,parent,false);//this view will contain appearance of each layout i.e each row..
        myviewholder holder=new myviewholder(v);// we are passing the view of each row to the myviewholder class
        return holder;
    }

    @Override
    public void onBindViewHolder(final myviewholder holder, int position) {//here we will inflate datas in the widgets i.e image and title..
        //It is called for each row..so every row is inflated here..

        final Blog p=list.get(position);
        holder.title.setText(p.getTitle());
        holder.username.setText(p.getUsername());
        holder.viewno.setText(p.getTimestamp());
        /*int count = 0;

        for(HospitalModel.Images images: hospitalModelList.get(position).getImagesList()) {
            count += images.size();
        }*/


        for(Blog model : list) {
            imageUrls = new ArrayList<>();;
            imageUrls.addAll(model.getUrl());
            Log.v("IMURLSSS", String.valueOf(imageUrls));


            Picasso.with(context).load(imageUrls.get(position)).into(holder.imageview);
            Log.v("IMGGPOS", imageUrls.get(position));
        }

        Picasso.with(context).load(p.getImage()).into(holder.userdp);
    }



    @Override
    public int getItemCount() {
        return list.size();
    }

    public class myviewholder extends RecyclerView.ViewHolder implements View.OnClickListener {
        // It contains the elements in each row that we will inflate in the recyclerView..
        TextView title,desc,username,likeno,viewno;
        ImageView imageview,userdp, over, comments, likes;
        ImageButton likebtn,viewbtn;

        public myviewholder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.blog_desc);//here we link with the elements of each view i.e each row and
            // desc = (TextView) itemView.findViewById(R.id.postdesc);//here we link with the elements of each view i.e each row and
            username = (TextView) itemView.findViewById(R.id.blog_user_name);
            viewno = (TextView) itemView.findViewById(R.id.blog_date);
            likeno = (TextView) itemView.findViewById(R.id.blog_like_count);
            userdp = (ImageView) itemView.findViewById(R.id.blog_user_image);
            imageview = (ImageView) itemView.findViewById(R.id.blog_image);
            comments = (ImageView) itemView.findViewById(R.id.blog_comment_icon);
            //here we link with the elements of each view i.e each row and
            // likebtn = (ImageButton)itemView.findViewById(R.id.likebtn);
            // viewbtn = (ImageButton)itemView.findViewById(R.id.viewbtn);
            comments.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mprogressview = true;
                    int pos = getAdapterPosition();
                    Blog b = list.get(pos);
                    Intent intent = new Intent(context,CommentBox.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("post_key",b.getUid().toString());
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            });
            over = (ImageView) itemView.findViewById(R.id.overflow);


            over.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showFilterPopup(v);
                }
            });

        });

        itemView.setOnClickListener(this);
    }

}
KENdi
  • 7,576
  • 2
  • 16
  • 31
KYM
  • 29
  • 8

1 Answers1

0

To display all those url's, please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference urlRef = rootRef.child("Blog").child(uid).child("url");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String url = ds.getValue(String.class);
            Log.d("TAG", url);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
urlRef.addListenerForSingleValueEvent(valueEventListener);

And here is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a RecyclerView using FirebaseRecyclerAdapter.

Edit:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Blog").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String uid = dataSnapshot.child("uid").getValue(String.class);
        String title = dataSnapshot.child("title").getValue(String.class);
        String timestamp = dataSnapshot.child("timestamp").getValue(String.class);
        Log.d("TAG", uid + " / " + title  + " / " + timestamp);

        for(DataSnapshot ds : dataSnapshot.child("url").getChildren()) {
            String url = ds.getValue(String.class);
            Log.d("TAG", url);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • . Thanks a lottt. Once check my adapter logic is correct or any changes because i need to show all the images with current id but its showing user name and description with all images and showing each image like a each post . – KYM Jun 15 '18 at 10:38
  • I want to display all images along with uid, title, timestamp into one post – KYM Jun 15 '18 at 11:00
  • Thanks. I'm confused with adapter class to show. This is my Blog class where im storing in firebase and i want to retrieve from that class. In My Blog class i have taken image urls as arraylist – KYM Jun 15 '18 at 11:21
  • Have you tried my solution above? Does this code log the desired values? – Alex Mamo Jun 15 '18 at 11:25
  • yaa its showing exact values in log but i want to show in adapter from my blog class i took image urls as arraylist and remaining all setters and getters as string . Confused in adapter class – KYM Jun 15 '18 at 11:29
  • If you are interested, **[this](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Jun 15 '18 at 11:31
  • I didn't got it one . your are taking from String.class and how should i create a adapter class to show the values from String.class. I tired with firebase adapter but its not showing the values. – KYM Jun 16 '18 at 08:22
  • If you want to use the String class, please see my answer from this **[post](https://stackoverflow.com/questions/48622480/showing-firebase-data-in-listview)**. – Alex Mamo Jun 16 '18 at 08:36
  • Facing issues with adapter class i.e. my blog class have all string getters and setters except image urls . Image urls are in arraylist and now i want to show the images combine with single username, uid like single post with multiple images. Issues with adapter class – KYM Jun 16 '18 at 16:48