Recyclerview not updating the data list

五迷三道 提交于 2019-12-24 11:55:45

问题


I'm using recyclerview to display data from sqlite database using cursor , when I delete an item from database the item gets deleted but my recycler view doesn't refresh.

I have to go back to my main activity and then the data gets refreshed. I have used notifyItemRemoved() and notifyDataSetChanged()

Here is my code.

Adapter

public class BookmarkAdapter extends RecyclerView.Adapter<BookmarkAdapter.ViewHolder> {

public Cursor cursor;
DatabaseHelper mDBHelper;

public BookmarkAdapter(){

}

public void setCursor(Cursor cursor){
    this.cursor = cursor;
}


@NonNull
@Override
public BookmarkAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {

    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    View v = inflater.inflate(R.layout.bookmark_item_layout, parent,false);
    BookmarkAdapter.ViewHolder viewHolder = new BookmarkAdapter.ViewHolder(v,context);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull final BookmarkAdapter.ViewHolder holder, int position) {

    cursor.moveToPosition(position);
    holder.tvWordBookmark.setText(cursor.getString(1));

    holder.btnDeleteBookmark.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String  id = cursor.getString(0);

            mDBHelper.deleteBookmark(id);
           notifyItemRemoved(holder.getAdapterPosition());
           notifyDataSetChanged();

        }
    });
}

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);
}

@Override
public int getItemCount() {
    if (cursor == null) return 0;
    return cursor.getCount();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    Context context;
    TextView tvWordBookmark;
    ImageButton btnDeleteBookmark;
    BookmarkAdapter bookmarkAdapter;
    public ViewHolder(@NonNull View itemView, final Context context) {
        super(itemView);

        this.context = context;
        mDBHelper = new DatabaseHelper(context);
        bookmarkAdapter = new BookmarkAdapter();

        tvWordBookmark = itemView.findViewById(R.id.tvWordBookmark);
        btnDeleteBookmark = itemView.findViewById(R.id.btnDeleteBookmark);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();
                cursor.moveToPosition(position);

                Intent intent = new Intent(context, DetailActivity.class);
                intent.putExtra("ID",cursor.getString(0));
                intent.putExtra("WORD",cursor.getString(1));
                intent.putExtra("DEFINITION",cursor.getString(2));
                context.startActivity(intent);

            }
        });
    }
}
}

And here is my activity

public class BookmarkActivity extends AppCompatActivity {


private RecyclerView rvWordBookmark;
private BookmarkAdapter bookmarkAdapter;
private DatabaseHelper mDBHelper;

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

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    rvWordBookmark = findViewById(R.id.recyclerviewBookmark);
    rvWordBookmark.setLayoutManager(new LinearLayoutManager(this));

    mDBHelper = new DatabaseHelper(this);

    bookmarkAdapter = new BookmarkAdapter();
    bookmarkAdapter.setCursor(mDBHelper.getBookmarkWord(""));
    rvWordBookmark.setAdapter(bookmarkAdapter);

}
@Override
public void onBackPressed() {
    super.onBackPressed();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id== android.R.id.home){
        onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

回答1:


You've to update your data list on click method below before notifyDataSetChanged();

 holder.btnDeleteBookmark.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        String  id = cursor.getString(0);

        mDBHelper.deleteBookmark(id);

        //here
       notifyItemRemoved(holder.getAdapterPosition());
       notifyDataSetChanged();

    }
});


来源:https://stackoverflow.com/questions/54805332/recyclerview-not-updating-the-data-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!