删除Crime记录

1.在string.xml中添加下列语句

删除陋习

2.修改list_item_crime.xml



    

3.在CrimeListFragment.java的CrimeHolder中添加下列语句

private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        ...
        private Button mDeleteButton;

        public CrimeHolder(View itemView){
            ...
            mDeleteButton=(Button)itemView.findViewById(R.id.list_item_crime_delete_button);
        }
        public void bindCrime(Crime crime){
            ...
            mDeleteButton.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view){
                    AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
                    builder.setMessage("确认要删除陋习"+mCrime.getTitle()+"?")
                            .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogPreference,int position){
                                    CrimeLab.get(getActivity()).delCrime(mCrime);
                                    mAdapter.notifyDataSetChanged();
                                }
                            }).setNegativeButton("取消",null)
                            .create().show();
                }
            });
        }
        ...
    }

4.在CrimeLab.java中添加下列语句

public void delCrime(Crime c){
        String uuidString=c.getId().toString();
        mDatabase.delete(CrimeTable.NAME,CrimeTable.Cols.UUID+ " = ?",new String[]{uuidString});
    }
删除Crime记录_第1张图片
1.png
删除Crime记录_第2张图片
2.png
删除Crime记录_第3张图片
3.png

你可能感兴趣的:(删除Crime记录)