[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);
        }
    }
相关推荐
吞掉星星的鲸鱼3 分钟前
使用高德api实现天气查询
前端·javascript·css
我命由我123455 分钟前
Spring Boot 自定义日志打印(日志级别、logback-spring.xml 文件、自定义日志打印解读)
java·开发语言·jvm·spring boot·spring·java-ee·logback
lilye666 分钟前
程序化广告行业(55/89):DMP与DSP对接及数据统计原理剖析
java·服务器·前端
zhougl9962 小时前
html处理Base文件流
linux·前端·html
花花鱼2 小时前
node-modules-inspector 可视化node_modules
前端·javascript·vue.js
HBR666_2 小时前
marked库(高效将 Markdown 转换为 HTML 的利器)
前端·markdown
战族狼魂4 小时前
CSGO 皮肤交易平台后端 (Spring Boot) 代码结构与示例
java·spring boot·后端
careybobo4 小时前
海康摄像头通过Web插件进行预览播放和控制
前端
xyliiiiiL5 小时前
ZGC初步了解
java·jvm·算法
杉之5 小时前
常见前端GET请求以及对应的Spring后端接收接口写法
java·前端·后端·spring·vue