How to remove an item from recyclerView after removing it from database

安稳与你 提交于 2019-12-10 18:37:17

问题


I am using a recyclerView to display a list of items that can be deleted when user clicks on delete button of each item. In order to do that, i've used a recyclerview adapter.

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.

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();
        }*/
    }
}    

回答1:


Your code is quite complex!

But from what you want: delete item from db and recyclerview after user input right code. So, basically you can reach it by some steps:

  1. Create listener for each recyclerview item (or button on item), when user click that -> open a dialog.

  2. After user input the right code (you have to check if it right) then do two things:

-> Remove view from recyclerview via adapter:

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

-> Remove data from database (how to remove depend on your)

If the data was not removed from your db, please debug your delete function if was called, if it work, or any mistake!

Hope it help!



来源:https://stackoverflow.com/questions/31444127/how-to-remove-an-item-from-recyclerview-after-removing-it-from-database

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