从recyclerview适配器中删除共享流程中的项目
我试图在Recyclerview适配器中删除共享播放中的项目。但是,直到我从活动中回来并回来之前,该项目才消失。我需要在被删除后尽快消失。
方法
public boolean RemoveFromWantToRead(AllBooksActivityModel book) {
ArrayList<AllBooksActivityModel>books = getWantToReadBooks();
if (null != books){
for (AllBooksActivityModel b: books){
if (b.getBookID() == book.getBookID()){
if (books.remove(b)){
//We create a 'if' loop here is if only the thing happened, the loop actions.
Gson gson = new Gson();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(WANT_TO_READ_BOOKS);
editor.putString(WANT_TO_READ_BOOKS,gson.toJson(books));
editor.commit();
return true;
}
} }
}return false;
}
这就是我尝试删除项目的方式。
holder.BtnDltTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Do you want to delete this massage");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String EleName = allBooksActivityModels.get(position).getBookName();
if( Ulitls.getInstance(context).RemoveFromCurrenlyReading(allBooksActivityModels.get(position))){
Toast.makeText(context, EleName + " Removed", Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}else{
Toast.makeText(context, "Something Wrong Happens, Try again", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.create().show();
}
});
i tried to delete an item in sharedPreferences, in recyclerView adapter. but that item doesn't disappear till I go back from the activity and come back. I need it to disappear soon after being deleted.
Method
public boolean RemoveFromWantToRead(AllBooksActivityModel book) {
ArrayList<AllBooksActivityModel>books = getWantToReadBooks();
if (null != books){
for (AllBooksActivityModel b: books){
if (b.getBookID() == book.getBookID()){
if (books.remove(b)){
//We create a 'if' loop here is if only the thing happened, the loop actions.
Gson gson = new Gson();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(WANT_TO_READ_BOOKS);
editor.putString(WANT_TO_READ_BOOKS,gson.toJson(books));
editor.commit();
return true;
}
} }
}return false;
}
This is how I tried to delete an item.
holder.BtnDltTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Do you want to delete this massage");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String EleName = allBooksActivityModels.get(position).getBookName();
if( Ulitls.getInstance(context).RemoveFromCurrenlyReading(allBooksActivityModels.get(position))){
Toast.makeText(context, EleName + " Removed", Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}else{
Toast.makeText(context, "Something Wrong Happens, Try again", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.create().show();
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论