Search code examples
androidfirebasegoogle-cloud-firestoreandroid-adapterfirebaseui

Calling a method from adapter?


I have the following Code:

public static FirestoreRecyclerAdapter adapter;

private void loadList() {

    Query query = db.collection("x").document(firebase_user_uid).collection("x");

    FirestoreRecyclerOptions<Note> response = new FirestoreRecyclerOptions.Builder<Note>()
            .setQuery(query, Note.class)
            .build();

    adapter = new FirestoreRecyclerAdapter<Note, NoteViewHolder>(response) {
        @Override
        protected void onBindViewHolder(NoteViewHolder holder, int position, Note model) {
            final Note note = notesList.get(position);

            holder.title.setText(note.getTitle());
            holder.content.setText(note.getContent());
            if (note.getNote_image_url() != null) {
                Glide.with(MainActivity.this).load(note.getNote_image_url()).into(holder.bg_note_image);
                holder.bg_note_image.setVisibility(View.VISIBLE);
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateNote(note);
                }
            });
        }

        @Override
        public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note_view, parent, false);
            return new NoteViewHolder(view);
        }

        @Override
        public void onError(FirebaseFirestoreException e) {
            Log.e("error", e.getMessage());
        }

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

        public void removeItem(int position) {
            notesList.remove(position);
            // notify the item removed by position
            // to perform recycler view delete animations
            // NOTE: don't call notifyDataSetChanged()
            notifyItemRemoved(position);
        }

        public void restoreItem(Note item, int position) {
            notesList.add(position, item);
            // notify item added by position
            notifyItemInserted(position);
        }
    };

    adapter.notifyDataSetChanged();
    recyclerView.setAdapter(adapter);
}

SO, a simple FirestoreRecyclerAdapter. But now I need to call the removeItemMethod inside the adapter. But how to do this?

I tried something like this, but all that didn't work.

public void deleteItem(int position) {
  adapter.removeItem(position);
}

I am sitting so long on this Problem. I hope anywhere is a solution to my Problem.

Thanks in advance. ~mb


Solution

  • You get this message because the original FirestoreRecyclerAdapter does not have this method. You should create a separate class extending FirestoreRecyclerAdapter and use it when creating an adapter.

    public class MyAdapter extends FirestoreRecyclerAdapter<Note, NoteViewHolder> {
    
    public List<Notes> notesList;
    
        public MyAdapter(List<Notes> notes, FirestoreRecyclerOptions<Note> response) {
        super(response);
        notesList = notes;
        }
    
            @Override
            protected void onBindViewHolder(NoteViewHolder holder, int position, 
            Note model) {
            final Note note = notesList.get(position);
    
            holder.title.setText(note.getTitle());
            holder.content.setText(note.getContent());
            if (note.getNote_image_url() != null) {
                Glide.with(MainActivity.this).load(note.getNote_image_url()).into(holder.bg_note_image);
                holder.bg_note_image.setVisibility(View.VISIBLE);
            }
    
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateNote(note);
                }
            });
        }
    
        @Override
        public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note_view, parent, false);
            return new NoteViewHolder(view);
        }
    
        @Override
        public void onError(FirebaseFirestoreException e) {
            Log.e("error", e.getMessage());
        }
    
        @Override
        public int getItemCount() {
            return notesList.size();
        }
    
        public void removeItem(int position) {
            notesList.remove(position);
            // notify the item removed by position
            // to perform recycler view delete animations
            // NOTE: don't call notifyDataSetChanged()
            notifyItemRemoved(position);
        }
    
        public void restoreItem(Note item, int position) {
            notesList.add(position, item);
            // notify item added by position
            notifyItemInserted(position);
        }
    }
    

    Something like this. And when you create an adapter :

    public static MyAdapter adapter;
    

    And initialising it:

    adapter = new MyAdapter(response)
    

    Also, it is not a good idea to keep an adapter static.

    Edit: pass the items - notesList - in the constructor.