SQLite delete and modify by id

delete

ListView Click the pop-up box to confirm the deletion

mAddressLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
    
    
        final AlertDialog.Builder builder = new AlertDialog.Builder(AddressActivity.this);
        builder.setTitle("是否删除");
        builder.setCancelable(false);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    
    
            @Override
            public void onClick(DialogInterface dialog, int which) {
    
    
                int id1 = mList.get(position).getId();
                String sql = "delete from address where id = " + id1;
                writableDatabase.execSQL(sql);
                mList.clear();
                getData();
                changer();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    
    
            @Override
            public void onClick(DialogInterface dialog, int which) {
    
    
                dialog.dismiss();
            }
        });
        builder.show();
    }
});

modify

Long press to carry data through SharedPreferences to jump to the modification page

mAddressLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    
    
   @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
    
    
        SharedPreferences sp = getSharedPreferences("addressid", MODE_APPEND);
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt("id", mList.get(position).getId());
        edit.putInt("img", mList.get(position).getImg());
        edit.putString("name", mList.get(position).getName());
        edit.putString("phone", mList.get(position).getPhone());
        edit.apply();
        Intent intent = new Intent(AddressActivity.this, AddressUpdateActivity.class);
        startActivity(intent);
        finish();
        return false;
    }
});

Modify the page to get data and display

SharedPreferences sp = getSharedPreferences("addressid", MODE_APPEND);
        SharedPreferences.Editor edit = sp.edit();
        id = sp.getInt("id", 0);
        int img = sp.getInt("img", 0);
        String name = sp.getString("name", "");
        String phone = sp.getString("phone", "");
        mAddressUpdateName.setText(name);
        mAddressUpdatePhone.setText(phone);
        mAddressUpdateImg.setImageResource(img);

Click edit and return to the main page

String name = mAddressUpdateName.getText().toString().trim();
String phone = mAddressUpdatePhone.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
    
    
    Toast.makeText(getApplicationContext(), "请输入姓名", Toast.LENGTH_SHORT).show();
    return;
}
if (TextUtils.isEmpty(phone)) {
    
    
    Toast.makeText(getApplicationContext(), "请输入电话号码", Toast.LENGTH_SHORT).show();
    return;
}
ContentValues values = new ContentValues();
values.put("name", name);
values.put("phone", phone);
values.put("img", imgList[imgId]);
writableDatabase.update("address", values, "id=?", new String[]{
    
    id + ""});
Intent intent1 = new Intent(AddressUpdateActivity.this, AddressActivity.class);
startActivity(intent1);
finish();

Guess you like

Origin blog.csdn.net/qq_42048638/article/details/102857305