从适配器调用方法?

时间:2018-08-08 14:32:40

标签: android firebase google-cloud-firestore android-adapter firebaseui

我有以下代码:

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,一个简单的FirestoreRecyclerAdapter。但是现在我需要在适配器内部调用removeItemMethod。但是该怎么做?

我尝试了类似的方法,但所有方法都无效。

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

我在这个问题上坐了很长时间。我希望任何地方都能解决我的问题。

先谢谢了。 〜mb

1 个答案:

答案 0 :(得分:1)

您收到此消息是因为原始的FirestoreRecyclerAdapter没有此方法。您应该创建一个扩展FirestoreRecyclerAdapter的单独类,并在创建适配器时使用它。

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);
    }
}

类似这样的事情。而当您创建适配器时:

public static MyAdapter adapter;

并初始化它:

adapter = new MyAdapter(response)

此外,保持适配器静态也不是一个好主意。

编辑:在构造函数中传递项目-notesList-。