滑动其中一行后 Recyclerview 不显示任何数据

Recyclerview doesn't show any data after swiping one of the rows

我已经在我的应用程序中添加了滑动功能,可以删除指定的笔记。几天来,我在滑动后显示数据时遇到了问题。为了澄清: 比方说,我们在回收站视图中有两个项目。每当我们滑动其中一个时,第二个应该是可见的,但直到我重新 运行 应用程序。我该如何解决?

主要Activity

   public class MainActivity extends AppCompatActivity implements MemoAdapter.OnNoteListener {


    private static final String TAG = "MainActivity";

    //Vars
    private ArrayList<Note> mNotes = new ArrayList<>();
    private MemoRepository mRepository;
    private MemoAdapter mMemoAdapter;

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

        mRepository = new MemoRepository(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.memoToolbar);
        setSupportActionBar(toolbar);
        setTitle("My memos");

        initRecyclerView();

    }
    //This method would be called after getting result from memo_content such as new memo, or edited existing memo.
    @Override
    protected void onResume() {
        super.onResume();
        getMemos();
    }

    private void getMemos(){
        mRepository.getAllMemos().observe(this, new Observer<List<Note>>() {
            @Override
            public void onChanged(List<Note> notes) {
                if (mNotes.size() > 0){
                    notes.clear();
                }
                if (notes != null){
                    mNotes.addAll(notes);
                    mMemoAdapter.watchMemoChanges((ArrayList<Note>) notes);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.icon_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.addNewNote:
                Toast.makeText(this, "Click!", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, memo_content.class));

                break;
            case R.id.deleteAllNotes:
                Toast.makeText(this, "Delete!", Toast.LENGTH_SHORT).show();
                mRepository.deleteAllMemos();
                break;

        }
        return super.onOptionsItemSelected(item);
    }
    private void initRecyclerView(){
        //UI
        RecyclerView mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
        mMemoAdapter = new MemoAdapter(mNotes, this);
        new ItemTouchHelper(itemTouch).attachToRecyclerView(mRecyclerView);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mMemoAdapter);

    }
    @Override
    public void onMemoClick(int position) {
        Intent intent = new Intent(this, memo_content.class);
        intent.putExtra("memo_content", mNotes.get(position));
        startActivity(intent);
    }

    private ItemTouchHelper.SimpleCallback itemTouch = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.START | ItemTouchHelper.LEFT) {
        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            mRepository.deleteMemo(mNotes.get(viewHolder.getAdapterPosition()));
            Log.d(TAG, "onSwiped: "+mNotes.get(viewHolder.getAdapterPosition()));

        }
    };
}

适配器

public class MemoAdapter extends RecyclerView.Adapter<MemoAdapter.MemoViewHolder> {

private ArrayList<Note> mNotes;
private OnNoteListener mListener;

public class MemoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView title, timestamp;

    private MemoViewHolder(@NonNull final View itemView) {
        super(itemView);
        this.title = (TextView) itemView.findViewById(R.id.title);
        this.timestamp = (TextView) itemView.findViewById(R.id.timestamp);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        mListener.onMemoClick(getAdapterPosition());
    }
}

public MemoAdapter(ArrayList<Note> notes, OnNoteListener listener) {
    this.mNotes = notes;
    this.mListener = listener;
}

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

@Override
public void onBindViewHolder(@NonNull MemoViewHolder holder, int position) {
    holder.title.setText(mNotes.get(position).getTitle());
    holder.timestamp.setText(mNotes.get(position).getTimestamp());
}

public void watchMemoChanges(ArrayList<Note> notes){
    this.mNotes = notes;
    notifyDataSetChanged();
}



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

public interface OnNoteListener{
   void onMemoClick(int position);
}

简答:

当您通过观察者从数据库接收到 LiveData 列表中的更改时,您需要删除语句 notes.clear()

详细解答

当你第一次运行你的应用程序时,它会显示所有正确的列表,因为条件if (mNotes.size() > 0)不满足,所以收到的列表不会通过notes.clear()清除, 因此 RecyclerView 将显示正确的数据。

但是当你删除一个笔记时,当观察者被新列表再次触发时,条件 if (mNotes.size() > 0) 将被满足,所以你将在喂养之前清除来自数据库的列表适配器,因此 RecyclerView 将没有数据。

所以要解决这个问题,请将 notes.clear(); 替换为 mNotes.clear();

所以正确的代码是:

private void getMemos(){
    mRepository.getAllMemos().observe(this, new Observer<List<Note>>() {
        @Override
        public void onChanged(List<Note> notes) {
            if (mNotes.size() > 0){
                mNotes.clear();
            }
            if (notes != null){
                mNotes.addAll(notes);
                mMemoAdapter.watchMemoChanges((ArrayList<Note>) notes);
            }
        }
    });
}

希望对您有所帮助。 我没有检查其余代码,如果还有其他问题可以帮助更多,请告诉我