从数据库中删除项目后如何从 recyclerView 中删除项目
How to remove an item from recyclerView after removing it from database
我正在使用 recyclerView 来显示当用户单击每个项目的删除按钮时可以删除的项目列表。为此,我使用了 recyclerview 适配器。
What i want: From that recycleView adapter, when user clicks on delete button for an item, a dialog fragment is shown and user has to enter a code when that code is valid then the item is removed from both the database and the recyclerView
What i've tried: After calling the show() method on the dialogFragment in the adapter, i'm calling removeItem(getPostion()).
What i get: When the dialog shows up, the item is removed from the recyclerView before the user enters the code but is still in the database
I 've tried other things but none of them worked.
求助!!!
RecyclerView 适配器(EBAdapter)
public EBViewHolder(View itemView) {
super(itemView);
contact_row = (LinearLayout) itemView.findViewById(R.id.contact_row);
contact_row.setOnClickListener(this);
contact_photo = (ImageView) itemView.findViewById(R.id.contact_photo);
contact_photo.setOnClickListener(this);
contact_fullname = (TextView) itemView.findViewById(R.id.contact_fullname);
contact_relationship = (TextView) itemView.findViewById(R.id.contact_relationship);
contact_action = (ImageView) itemView.findViewById(R.id.contact_action);
contact_action.setOnClickListener(this);
contact_discard = (ImageView) itemView.findViewById(R.id.discard_action);
contact_discard.setOnClickListener(this);
}
@Override
public void onClick(View view){
//Handling recyclerView action clicked
String status;
if(view instanceof ImageView) {
if (view == contact_photo) {
Log.d("Contact", contact_photo.getId() + "photo " + getPosition() + "clicked");
ShowContactPhoto dialog = new ShowContactPhoto();
dialog.setStyle(DialogFragment.STYLE_NO_TITLE,0);
dialog.show(manager, "ScP_dialog");
}
if (view == contact_action) {
Log.d("Contact", contact_fullname.getText() + "action " + getPosition() + "clicked");
status = "active";
Date created_at = new Date();
Date updated_at = new Date();
AddInEmergencyBase dialog = new AddInEmergencyBase(((BitmapDrawable)contact_photo.getDrawable()).getBitmap(), contact_fullname.getText().toString(),contact_relationship.getText().toString(),status,created_at.toString(),updated_at.toString());
dialog.show(manager, "AiB_dialog");
Log.d("Contact", "Dialog Dismissed now");
}
//REGION OF INTEREST:
if (view == contact_discard) {
Log.d("Contact", contact_discard.getId() + "photo " + getPosition() + "clicked");
status = "inactive";
dialog = new DiscardEmergencyPers(contact_fullname.getText().toString(),status,mModels,getPosition(),rc);
position = getPosition();
dialog.show(manager, "DeP_dialog");
//dialog.getDialog().setOnDismissListener(this);
//onDismiss(dialog);
removeItem(getPosition());
Log.d("Contact", dialog.getDialog()+"item removed from data actually updated so hihihi....");
/*ShowContactPhoto dialog = new ShowContactPhoto();
dialog.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
dialog.show(manager, "ScP_dialog");*/
}
}
else{
Log.d("Contact", contact_row.getId() + "row " + getPosition() + "clicked");
}
DialogFragment (DiscardEmergencyPers)
public class DiscardEmergencyPers extends DialogFragment implements View.OnClickListener, AdapterView.OnItemSelectedListener,DialogInterface.OnDismissListener, DialogInterface {
private final String fullname;
private final List<EmBaseInfo> mModels;
private final String status;
private final int position;
private final RecyclerView rc;
//private final String updated_at;
EditText pin_code;
Button cancel, done;
Communicator com;
private EmergenciaDBAdapter emergencia_helper;
private Context context;
private RecyclerView recycler_view;
public DiscardEmergencyPers(String fullname, String status,List<EmBaseInfo> mModels, int position, RecyclerView rc){
this.fullname=fullname;
this.status = status;
this.mModels = mModels;
this.position = position;
this.rc =rc;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
com = (Communicator) activity;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_discardemperson, null);
context = getActivity().getApplicationContext();
getDialog().setTitle("Remove emergency contact");
//Spinner that contains relationships with the current contact
pin_code = (EditText) view.findViewById(R.id.pin);
/*ArrayAdapter adapter = ArrayAdapter.createFromResource(context, R.array.relationship, android.R.layout.simple_spinner_dropdown_item);
relationship_spinner.setAdapter(adapter);
relationship_spinner.setOnItemSelectedListener(this);*/
//End of Spinner
//
emergencia_helper = new EmergenciaDBAdapter(context);
//spinner = (Spinner)view.findViewById(R.id.spinner);
cancel = (Button) view.findViewById(R.id.cancel);
done = (Button) view.findViewById(R.id.done);
cancel.setOnClickListener(this);
done.setOnClickListener(this);
setCancelable(false);
return view;
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.cancel){
dismiss();
com.onDialogMessage("Cancel clicked");
}
else{
com.onDialogMessage("Done clicked");
if(pin_code.getEditableText().toString().equals("12345")){
dismiss();
emergencia_helper.update(status, fullname/*,updated_at*/);
//EBAdapter n_adapter = new EBAdapter(getActivity(),removeItem(this.position),getFragmentManager(),rc);
//rc.setAdapter(n_adapter);
//rc.getAdapter().(this.position);
rc.getAdapter().notifyDataSetChanged();
rc.getAdapter().notifyItemRemoved(0);
//Bundle savedInstanceState=null;
//getActivity().onCreate(savedInstanceState);
CharSequence text = "Good Pin "+rc.getAdapter();
Log.d("Contacts", rc.getClass()+"");
Log.d("Contacts", rc.getAdapter()+" "+getActivity());
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration).show();
}
else{
CharSequence text = "Wrong Pin";
//Log.d("Contacts", mModels.toString());
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration).show();
}
//long id=emergencia_helper.insert(photo,fullname,phone,relationship, status,created_at,updated_at);
/*if(id<0){
CharSequence text = "Row insertion unsuccessful ";
//Log.d("Contacts", mModels.toString());
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration).show();
}
else{
CharSequence text = "Row insertion successful ";
//Log.d("Contacts", mModels.toString());
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration).show();
}*/
}
}
你的代码很复杂!
但根据您的需要:从数据库和回收站视图中删除项目在用户输入正确的代码后。所以,基本上你可以通过一些步骤到达它:
为每个 recyclerview 项目(或项目上的按钮)创建侦听器,当用户单击它时 -> 打开一个对话框。
用户输入正确的代码后(你必须检查是否正确)然后做两件事:
-> 通过适配器从 recyclerview 中删除视图:
dataSource.remove(index); // remember to remove it from your adapter data source
notifyItemRemoved(index);
-> 从数据库中删除数据(如何删除取决于您)
如果数据没有从你的数据库中删除,请调试你的删除函数是否被调用,如果它有效,或者有任何错误!
希望对您有所帮助!
我正在使用 recyclerView 来显示当用户单击每个项目的删除按钮时可以删除的项目列表。为此,我使用了 recyclerview 适配器。
What i want: From that recycleView adapter, when user clicks on delete button for an item, a dialog fragment is shown and user has to enter a code when that code is valid then the item is removed from both the database and the recyclerView
What i've tried: After calling the show() method on the dialogFragment in the adapter, i'm calling removeItem(getPostion()).
What i get: When the dialog shows up, the item is removed from the recyclerView before the user enters the code but is still in the database I 've tried other things but none of them worked.
求助!!! RecyclerView 适配器(EBAdapter)
public EBViewHolder(View itemView) {
super(itemView);
contact_row = (LinearLayout) itemView.findViewById(R.id.contact_row);
contact_row.setOnClickListener(this);
contact_photo = (ImageView) itemView.findViewById(R.id.contact_photo);
contact_photo.setOnClickListener(this);
contact_fullname = (TextView) itemView.findViewById(R.id.contact_fullname);
contact_relationship = (TextView) itemView.findViewById(R.id.contact_relationship);
contact_action = (ImageView) itemView.findViewById(R.id.contact_action);
contact_action.setOnClickListener(this);
contact_discard = (ImageView) itemView.findViewById(R.id.discard_action);
contact_discard.setOnClickListener(this);
}
@Override
public void onClick(View view){
//Handling recyclerView action clicked
String status;
if(view instanceof ImageView) {
if (view == contact_photo) {
Log.d("Contact", contact_photo.getId() + "photo " + getPosition() + "clicked");
ShowContactPhoto dialog = new ShowContactPhoto();
dialog.setStyle(DialogFragment.STYLE_NO_TITLE,0);
dialog.show(manager, "ScP_dialog");
}
if (view == contact_action) {
Log.d("Contact", contact_fullname.getText() + "action " + getPosition() + "clicked");
status = "active";
Date created_at = new Date();
Date updated_at = new Date();
AddInEmergencyBase dialog = new AddInEmergencyBase(((BitmapDrawable)contact_photo.getDrawable()).getBitmap(), contact_fullname.getText().toString(),contact_relationship.getText().toString(),status,created_at.toString(),updated_at.toString());
dialog.show(manager, "AiB_dialog");
Log.d("Contact", "Dialog Dismissed now");
}
//REGION OF INTEREST:
if (view == contact_discard) {
Log.d("Contact", contact_discard.getId() + "photo " + getPosition() + "clicked");
status = "inactive";
dialog = new DiscardEmergencyPers(contact_fullname.getText().toString(),status,mModels,getPosition(),rc);
position = getPosition();
dialog.show(manager, "DeP_dialog");
//dialog.getDialog().setOnDismissListener(this);
//onDismiss(dialog);
removeItem(getPosition());
Log.d("Contact", dialog.getDialog()+"item removed from data actually updated so hihihi....");
/*ShowContactPhoto dialog = new ShowContactPhoto();
dialog.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
dialog.show(manager, "ScP_dialog");*/
}
}
else{
Log.d("Contact", contact_row.getId() + "row " + getPosition() + "clicked");
}
DialogFragment (DiscardEmergencyPers)
public class DiscardEmergencyPers extends DialogFragment implements View.OnClickListener, AdapterView.OnItemSelectedListener,DialogInterface.OnDismissListener, DialogInterface {
private final String fullname;
private final List<EmBaseInfo> mModels;
private final String status;
private final int position;
private final RecyclerView rc;
//private final String updated_at;
EditText pin_code;
Button cancel, done;
Communicator com;
private EmergenciaDBAdapter emergencia_helper;
private Context context;
private RecyclerView recycler_view;
public DiscardEmergencyPers(String fullname, String status,List<EmBaseInfo> mModels, int position, RecyclerView rc){
this.fullname=fullname;
this.status = status;
this.mModels = mModels;
this.position = position;
this.rc =rc;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
com = (Communicator) activity;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_discardemperson, null);
context = getActivity().getApplicationContext();
getDialog().setTitle("Remove emergency contact");
//Spinner that contains relationships with the current contact
pin_code = (EditText) view.findViewById(R.id.pin);
/*ArrayAdapter adapter = ArrayAdapter.createFromResource(context, R.array.relationship, android.R.layout.simple_spinner_dropdown_item);
relationship_spinner.setAdapter(adapter);
relationship_spinner.setOnItemSelectedListener(this);*/
//End of Spinner
//
emergencia_helper = new EmergenciaDBAdapter(context);
//spinner = (Spinner)view.findViewById(R.id.spinner);
cancel = (Button) view.findViewById(R.id.cancel);
done = (Button) view.findViewById(R.id.done);
cancel.setOnClickListener(this);
done.setOnClickListener(this);
setCancelable(false);
return view;
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.cancel){
dismiss();
com.onDialogMessage("Cancel clicked");
}
else{
com.onDialogMessage("Done clicked");
if(pin_code.getEditableText().toString().equals("12345")){
dismiss();
emergencia_helper.update(status, fullname/*,updated_at*/);
//EBAdapter n_adapter = new EBAdapter(getActivity(),removeItem(this.position),getFragmentManager(),rc);
//rc.setAdapter(n_adapter);
//rc.getAdapter().(this.position);
rc.getAdapter().notifyDataSetChanged();
rc.getAdapter().notifyItemRemoved(0);
//Bundle savedInstanceState=null;
//getActivity().onCreate(savedInstanceState);
CharSequence text = "Good Pin "+rc.getAdapter();
Log.d("Contacts", rc.getClass()+"");
Log.d("Contacts", rc.getAdapter()+" "+getActivity());
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration).show();
}
else{
CharSequence text = "Wrong Pin";
//Log.d("Contacts", mModels.toString());
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration).show();
}
//long id=emergencia_helper.insert(photo,fullname,phone,relationship, status,created_at,updated_at);
/*if(id<0){
CharSequence text = "Row insertion unsuccessful ";
//Log.d("Contacts", mModels.toString());
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration).show();
}
else{
CharSequence text = "Row insertion successful ";
//Log.d("Contacts", mModels.toString());
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration).show();
}*/
}
}
你的代码很复杂!
但根据您的需要:从数据库和回收站视图中删除项目在用户输入正确的代码后。所以,基本上你可以通过一些步骤到达它:
为每个 recyclerview 项目(或项目上的按钮)创建侦听器,当用户单击它时 -> 打开一个对话框。
用户输入正确的代码后(你必须检查是否正确)然后做两件事:
-> 通过适配器从 recyclerview 中删除视图:
dataSource.remove(index); // remember to remove it from your adapter data source
notifyItemRemoved(index);
-> 从数据库中删除数据(如何删除取决于您)
如果数据没有从你的数据库中删除,请调试你的删除函数是否被调用,如果它有效,或者有任何错误!
希望对您有所帮助!