[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);
        }
    }
    
相关推荐
类人_猿1 小时前
ASP.NET Web(.Net Framework) Http服务器搭建以及IIS站点发布
前端·iis·asp.net·.net·http站点服务器
小黄编程快乐屋1 小时前
各个排序算法基础速通万字介绍
java·算法·排序算法
材料苦逼不会梦到计算机白富美3 小时前
贪心算法-区间问题 C++
java·c++·贪心算法
组态软件4 小时前
web组态软件
前端·后端·物联网·编辑器·html
前端Hardy4 小时前
HTML&CSS:MacBook Air 3D 动画跃然屏上
前端·javascript·css·3d·html
小小李程序员7 小时前
LRU缓存
java·spring·缓存
cnsxjean7 小时前
SpringBoot集成Minio实现上传凭证、分片上传、秒传和断点续传
java·前端·spring boot·分布式·后端·中间件·架构
hadage2337 小时前
--- stream 数据流 java ---
java·开发语言
ZL_5677 小时前
uniapp中使用uni-forms实现表单管理,验证表单
前端·javascript·uni-app
《源码好优多》8 小时前
基于Java Springboot汽配销售管理系统
java·开发语言·spring boot