- 界面显示
添加按钮点击,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));
}
-
准备主键修改
-
链接数据库,实时更新,指定主键where
-
改变之后联系人信息更改
-
详细代码
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); } }