[Android]联系人-删除修改

  1. 界面显示

添加按钮点击,holder.imgDelete.setlog();具体代码

 public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //映射布局文件,生成相应的组件
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.data_item, parent, false);
        MyViewHolder vh = new MyViewHolder(v);
        vh.txtName = v.findViewById(R.id.txtNameMainItem);
        vh.txtTel = v.findViewById(R.id.txtTelMainItem);
        vh.imgView = v.findViewById(R.id.imgMainItem);
        vh.imdDelete=v.findViewById(R.id.imgDelete);
        vh.imgEdit=v.findViewById(R.id.imgEdit);

        vh.imdDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String s=(String)view.getTag();
                AlertDialog dlg=new AlertDialog.Builder(view.getContext())
                        .setTitle("删除提示:")
                        .setIcon(R.drawable.down)
                        .setMessage("是否删除"+s+"?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String ss="delete from contacts where name='"+s+"';";
                                db.execSQL(ss);
                                updateData();
                            }
                        })
                        .setNegativeButton("取消",null)
                        .create();
                    dlg.show();

                    }
        });

        vh.imgEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s=(String)view.getTag();
                Log.d("test","Edit"+s);
            }
        });
        return vh;
    }
复制代码
设置每个数据项的View组件的属性
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        cursor.moveToFirst();//先移动到第0条记录
        cursor.move(position);//再移动到第position条记录
        holder.txtName.setText(cursor.getString(0));
        holder.txtTel.setText(cursor.getString(1));
        if (1 == cursor.getInt(2))
            holder.imgView.setImageResource(R.drawable.avatar_male);
        else
            holder.imgView.setImageResource(R.drawable.avatar_female);

        holder.imdDelete.setTag(1,cursor.getString(0));
        holder.imgEdit.setTag(1,cursor.getString(0));
    }
  1. 准备主键修改

  2. 链接数据库,实时更新,指定主键where

  3. 改变之后联系人信息更改

  4. 详细代码

    package com.example.testcontacts;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.database.Cursor;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.Toast;
    
    public class EditActivity extends AppCompatActivity {
        private EditText dtName, dtTel, dtAddress, dtEmail;
        private RadioButton rbtnMale, rbtnFemale;
        private Button btnEdit, btnCancel;
        private String sName;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_edit);
            initView();
    
            btnCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
            sName = getIntent().getStringExtra("name");
            String s = "select name,gender,tel,address,email from contacts where name='" + sName+"';";
            Cursor cursor = MyAdapter.db.rawQuery(s, null);
            if (!cursor.moveToNext()) {
                Toast.makeText(this, "找不到"+sName+"的记录", Toast.LENGTH_SHORT).show();
                finish();
                return;
            }
    
            dtName.setText(cursor.getString(0));
            int iGender = cursor.getInt(1);
            if (iGender > 0)
                rbtnMale.setChecked(true);
            else
                rbtnFemale.setChecked(true);
            dtTel.setText(cursor.getString(2));
            dtAddress.setText(cursor.getString(3));
            dtEmail.setText(cursor.getString(4));
    
            btnEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String sGender = "true";
                    if (rbtnFemale.isChecked())
                        sGender = "false";
                    String name = dtName.getText().toString();
                    String sTel = dtTel.getText().toString();
                    String sAddress = dtAddress.getText().toString();
                    String sEmail = dtEmail.getText().toString();
    
                    String s = "update contacts set name='" + name+"', gender="+sGender+",tel='" + sTel+
                            "',address='"+sAddress+"',email='"+sEmail+"' where name='"+ sName + "';";
                    MyAdapter.db.execSQL(s);
                    finish();
                }
            });
        }
    
        private void initView()
        {
            dtName = findViewById(R.id.dtEditName);
            dtTel = findViewById(R.id.dtEditTel);
            dtAddress = findViewById(R.id.dtEditAddress);
            dtEmail = findViewById(R.id.dtEditEmail);
            rbtnMale = findViewById(R.id.rEditBtnMale);
            rbtnFemale = findViewById(R.id.rEditBtnFemale);
            btnEdit = findViewById(R.id.btnEdit);
            btnCancel = findViewById(R.id.btnEditCancel);
        }
    }
    
相关推荐
GIS程序媛—椰子9 分钟前
【Vue 全家桶】7、Vue UI组件库(更新中)
前端·vue.js
confiself12 分钟前
大模型系列——LLAMA-O1 复刻代码解读
java·开发语言
DogEgg_00115 分钟前
前端八股文(一)HTML 持续更新中。。。
前端·html
Wlq041517 分钟前
J2EE平台
java·java-ee
ZL不懂前端18 分钟前
Content Security Policy (CSP)
前端·javascript·面试
小黄人软件21 分钟前
android浏览器源码 可输入地址或关键词搜索 android studio 2024 可开发可改地址
android·ide·android studio
木舟100922 分钟前
ffmpeg重复回听音频流,时长叠加问题
前端
XiaoLeisj24 分钟前
【JavaEE初阶 — 多线程】Thread类的方法&线程生命周期
java·开发语言·java-ee
王大锤439132 分钟前
golang通用后台管理系统07(后台与若依前端对接)
开发语言·前端·golang
豪宇刘38 分钟前
SpringBoot+Shiro权限管理
java·spring boot·spring