删除
ListView 点击弹出弹框确定删除
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();
}
});
修改
长按通过SharedPreferences携带数据跳转到修改页面
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;
}
});
修改页面获取数据并显示
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);
点击修改并返回主页面
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();