这是适配器( RVAdapter_Reminders.java )
static List<DatabaseModel> dbList;
static Context context;
DBHelper dbHelper;
private int position;
public RVAdapter_Reminders(Context context, List<DatabaseModel> dbList) {
this.dbList = new ArrayList<DatabaseModel>();
this.context = context;
this.dbList = dbList;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
@Override
public RVAdapter_Reminders.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.rv_reminder_template, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(final RVAdapter_Reminders.ViewHolder holder, final int position) {
holder.title.setText(dbList.get(position).getTitle());
holder.description.setText(dbList.get(position).getDescription());
holder.address.setText(dbList.get(position).getAddress());
holder.buttonViewOptions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//creating a popup menu
PopupMenu popup = new PopupMenu(context, holder.buttonViewOptions);
//inflating menu from xml
popup.inflate(R.menu.reminder_rv_menu);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
setPosition(holder.getAdapterPosition());
return false;
}
});
popup.show();
}
});
}
@Override
public void onViewRecycled(ViewHolder holder) {
holder.itemView.setOnClickListener(null);
super.onViewRecycled(holder);
}
@Override
public int getItemCount() {
return dbList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {
public TextView title, description, address, buttonViewOptions;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.task_title);
description = (TextView) itemView.findViewById(R.id.desc_title_text_tv);
address = (TextView) itemView.findViewById(R.id.address_title_text_tv);
buttonViewOptions = (TextView) itemView.findViewById(R.id.buttonViewOptions);
itemView.setOnCreateContextMenuListener(this);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, R.id.menu_delete_option,
Menu.NONE, R.string.delete_menu_text);
}
}
这是适配器在RecyclerView
中充气数据的ActivityReminders.java
FloatingActionButton fab;
DBHelper dbHelper;
List<DatabaseModel> dbList;
RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminders);
fab = (FloatingActionButton) findViewById(R.id.fab);
try {
getSupportActionBar().setTitle("Reminders");
} catch (Exception e) {
e.printStackTrace();
}
dbHelper = new DBHelper(this);
dbList = new ArrayList<DatabaseModel>();
dbList = dbHelper.getDataFromDB();
mRecyclerView = (RecyclerView) findViewById(R.id.rv_reminders);
mRecyclerView.setHasFixedSize(true);
// using a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
//specifying an adapter
mAdapter = new RVAdapter_Reminders(this, dbList);
mRecyclerView.setAdapter(mAdapter);
registerForContextMenu(mRecyclerView);
}
public void addReminder(View view) {
Intent intent = new Intent(Reminders.this, NewReminder.class);
startActivity(intent);
}
@Override
public void onBackPressed() {
Intent intent = new Intent(Reminders.this, MainActivity.class);
startActivity(intent);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int position = -1;
try{
position = ((RVAdapter_Reminders)mRecyclerView.getAdapter()).getPosition();
}catch (Exception e) {
Log.d(String.valueOf(this), e.getLocalizedMessage(), e);
return super.onContextItemSelected(item);
}
switch (item.getItemId()){
case R.id.menu_delete_option:
onReminderDelete(dbList.get(position).getTitle());
break;
}
return super.onContextItemSelected(item);
}
private void onReminderDelete(String title){
dbHelper.deleteARow(title);
mRecyclerView.setAdapter(mAdapter);
}
DBHelper类中的DeleteRow函数:
public void deleteARow(String title) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(REMINDERS_TABLE, RemindersContract.TITLE + " = " + title, null);
Log.d(String.valueOf(this), title + " deleted");
db.close();
}
最后, RemindersContract 类:
public static final String TITLE = "title";
我知道我犯了一个愚蠢的错误,但是我的头脑很多,无法找到错误的位置。 请尽快!
答案 0 :(得分:1)
您的mAdapter
仍然具有已删除值的值。您必须将dbList
更新为当前值。
private void onReminderDelete(String title){
dbHelper.deleteARow(title);
dbList.remove(/* value that you want remove*/);
mAdapter.notifyDataSetChanged();
}