Home » Android » java – Showing blank data after retrieve data from Realtime Firebase-Exceptionshub

java – Showing blank data after retrieve data from Realtime Firebase-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have a problem with the searchView from Firebase. I don’t know how to do it, even though I’ve watched many videos on youtube but I still got errors. So I hope you guys can help me out with this problem because I’m just a new starter. below is my code.

My ViewImageFile.java class.

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.SearchView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

import java.util.ArrayList;

public class viewImageUpload extends AppCompatActivity implements ImageAdapter.OnItemCLickListener {

    private RecyclerView mRecyleView;
    private ImageAdapter mAdapter;
    private ArrayList < DatabaseImage > mUploads;
    private FirebaseStorage mStorage;

    private ProgressBar progressBar;

    private ValueEventListener mDBListener;
    private DatabaseReference mDatabaseReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_image_upload);

        Toolbar toolbar = findViewById(R.id.toolbarmain);
        setSupportActionBar(toolbar);

        mRecyleView = findViewById(R.id.recycleviewimage);
        mRecyleView.setHasFixedSize(true);
        mRecyleView.setLayoutManager(new LinearLayoutManager(this));

        progressBar = (ProgressBar) findViewById(R.id.progress_circular);
        mUploads = new ArrayList < > ();

        mAdapter = new ImageAdapter(viewImageUpload.this, mUploads);
        mRecyleView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(viewImageUpload.this);

        mStorage = FirebaseStorage.getInstance();

        mDatabaseReference = FirebaseDatabase.getInstance().getReference("uploads");
        mDBListener = mDatabaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mUploads.clear();

                for (DataSnapshot postSnapShot: dataSnapshot.getChildren()) {
                    DatabaseImage upload = postSnapShot.getValue(DatabaseImage.class);
                    upload.setkeys(postSnapShot.getKey());
                    mUploads.add(upload);
                }
                mAdapter.notifyDataSetChanged();

                progressBar.setVisibility(View.INVISIBLE);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(viewImageUpload.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
                progressBar.setVisibility(View.INVISIBLE);

            }
        });

    }

    @Override
    public void onitemClick(int position) {
        Toast.makeText(this, "You have click on Image " + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDeleteClick(int position) {
        DatabaseImage selectedItem = mUploads.get(position);
        final String selectedKey = selectedItem.getkeys();

        StorageReference imageRef = mStorage.getReferenceFromUrl(selectedItem.getImgUrl());
        imageRef.delete().addOnSuccessListener(new OnSuccessListener < Void > () {
            @Override
            public void onSuccess(Void aVoid) {
                mDatabaseReference.child(selectedKey).removeValue();
                Toast.makeText(viewImageUpload.this, "Delete Successfully", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDatabaseReference.removeEventListener(mDBListener);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.about) {
            Toast.makeText(getApplicationContext(), "You Click About", Toast.LENGTH_SHORT).show();

        } else if (id == R.id.search) {
            Toast.makeText(getApplicationContext(), "You Click Search", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.signOut) {
            FirebaseAuth.getInstance().signOut();
            startActivity(new Intent(getApplicationContext(), SplashScreen.class));
            finish();
        }
        return true;
    }
}

My ImageAdapter.java class

package com.example.lasttic;

import android.content.Context;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class ImageAdapter extends RecyclerView.Adapter < ImageAdapter.ImageViewHolder > {

    private Context mContext;
    private ArrayList < DatabaseImage > mUploads;
    private ArrayList < DatabaseImage > arraylistFiltered;

    private OnItemCLickListener mListener;

    public ImageAdapter(Context context, ArrayList < DatabaseImage > uploads) {

        mContext = context;
        mUploads = uploads;
        arraylistFiltered = new ArrayList < > (mUploads);

    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(mContext).inflate(R.layout.image_item, parent, false);

        ImageViewHolder viewHolder = new ImageViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {

        final DatabaseImage uploadCur = mUploads.get(position);
        holder.imageName.setText(uploadCur.getImgName());
        Picasso.get().load(uploadCur.getImgUrl())
            .placeholder(R.drawable.imagepreview)
            .fit()
            .centerCrop()
            .into(holder.imageUpload);

        holder.Detailkk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, TicketDetail.class);
                mContext.startActivity(intent);
            }
        });

    }

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

    public class ImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener {

        public TextView imageName;
        public ImageView imageUpload;
        public Button Detailkk;

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);
            imageName = itemView.findViewById(R.id.imageName);
            imageUpload = itemView.findViewById(R.id.imageUpload);
            Detailkk = itemView.findViewById(R.id.detail22);

            itemView.setOnClickListener(this);
            itemView.setOnCreateContextMenuListener(this);

        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION) {
                    mListener.onitemClick(position);
                }
            }
        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            menu.setHeaderTitle("Select Action");
            MenuItem delete = menu.add(Menu.NONE, 1, 1, "Delete");

            delete.setOnMenuItemClickListener(this);

        }

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (mListener != null) {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION) {
                    switch (item.getItemId()) {
                        case 1:
                            mListener.onDeleteClick(position);
                            return true;

                    }

                }
            }
            return false;
        }
    }

    public interface OnItemCLickListener {
        void onitemClick(int position);

        void onDeleteClick(int position);
    }
    public void setOnItemClickListener(OnItemCLickListener listener) {
        mListener = listener;
    }
}

My DatabaseImage.Java class.

 package com.example.lasttic;

 import com.google.firebase.database.Exclude;

 public class DatabaseImage {

     private String mimgUrl;
     private String imgName;
     private String mkeys;

     public DatabaseImage() {

     }

     public DatabaseImage(String Name, String imgUrl) {

         if (Name.trim().equals("")) {
             Name = "No Name";
         }

         imgName = Name;
         mimgUrl = imgUrl;

     }
     public String getImgName() {
         return imgName;
     }

     public void setImgName(String Name) {
         imgName = Name;
     }

     public String getImgUrl() {
         return mimgUrl;
     }

     public void setImgUrl(String imgUrl) {
         mimgUrl = imgUrl;
     }

     @Exclude
     public String getkeys() {
         return mkeys;
     }
     @Exclude
     public void setkeys(String key) {
         mkeys = key;
     }
 }
How to&Answers: