How to remove item from recyclerView after deleting it from database

I am using a recyclerView to display a list of items that can be deleted when the user clicks the delete button of each item. I used the recycliewiew adapter for this.

What I want: From this recycleView adapter, when the user clicks the delete button for an item, a snippet of dialog is displayed and the user has to enter a code, when this code is valid then the item is removed from the database and the recyclerView

What I tried: After calling the show () method on the dialog box in the adapter, I call removeItem (getPostion ()).

What I get: When the dialog appears, the item is removed from the recyclerView before the user enters the code, but is still in the database. I tried other things but none of them worked.

Please, help!!! RecyclerView adapter (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();
        }*/
    }
}    

      

+3


source to share


1 answer


Your code is pretty tricky!

But from what you want: remove item from db and recyclerview after correct user code input . So, basically you can achieve this in a few steps:

  • Create a listener for each recyclerview item (or button on item) when user clicks this -> open dialog.

  • After the user has entered the correct code (you must check if it is correct), do two things:

-> Remove view from recyclerview via adapter:



dataSource.remove(index); // remember to remove it from your adapter data source
notifyItemRemoved(index);

      

-> Delete data from database (how to delete depends on yours)

If no data has been removed from your db, debug your delete function if it was called, if it works, or any error!

Hope it helps!

0


source







All Articles