所花时间:五十分钟左右,不到一个小时,今天主要是敲代码了,视频基本跳着看的
代码量:增部分的后四十行,删的37行,改的65行
了解到的知识点:
增代码
package com.example.test3_6;import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import com.example.sqlitetest271.bean.Student;
import com.example.sqlitetest271.util.ToastUtil;
public class InsertActivity extends AppCompatActivity {
private EditText etName,etNumber,etScore;
private RadioButton rbMan,rbWoman;
private MySQLiteOpenHelper mMySQLiteOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert);
initView();
mMySQLiteOpenHelper = new MySQLiteOpenHelper(this);
}
private void initView() {
etName = findViewById(R.id.et_name);
etNumber = findViewById(R.id.et_number);
etScore = findViewById(R.id.et_score);
rbMan = findViewById(R.id.rb_man);
rbWoman = findViewById(R.id.rb_woman);
}
public void insert(View view) {
String name = etName.getText().toString().trim();
String number = etNumber.getText().toString().trim();
String score = etScore.getText().toString().trim();
String gender = "";
if (rbMan.isChecked()) {
gender = "男";
}
if (rbWoman.isChecked()) {
gender = "女";
}
Student student = new Student();
student.setName(name);
student.setNumber(number);
student.setGender(gender);
student.setScore(score);
// 插入数据库中
long rowId = mMySQLiteOpenHelper.insertData(student);
if (rowId != -1) {
ToastUtil.toastShort(this, "添加成功!");
} else {
ToastUtil.toastShort(this, "添加失败!");
}
}
} 删代码 package com.example.test3_6;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.example.sqlitetest271.util.ToastUtil;
public class DeleteActivity extends AppCompatActivity {
private EditText etName;
private MySQLiteOpenHelper mMySQLiteOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete);
etName = findViewById(R.id.et_name);
mMySQLiteOpenHelper = new MySQLiteOpenHelper(this);
}
public void delete(View view) {
String name = etName.getText().toString().trim();
// 按姓名从数据库中删除
int row = mMySQLiteOpenHelper.deleteFromDbByName(name);
if (row > 0) {
ToastUtil.toastLong(this, "删除成功,删了" + row + "条数据");
}else{
ToastUtil.toastLong(this, "删除失败,没有找到符合条件的数据");
}
}
} 改代码 package com.example.test3_6;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import com.example.sqlitetest271.bean.Student;
import com.example.sqlitetest271.util.ToastUtil;
public class UpdateActivity extends AppCompatActivity {
private EditText etName,etNumber,etScore;
private RadioButton rbMan,rbWoman;
private MySQLiteOpenHelper mMySQLiteOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
initView();
mMySQLiteOpenHelper = new MySQLiteOpenHelper(this);
}
private void initView() {
etName = findViewById(R.id.et_name);
etNumber = findViewById(R.id.et_number);
etScore = findViewById(R.id.et_score);
rbMan = findViewById(R.id.rb_man);
rbWoman = findViewById(R.id.rb_woman);
}
public void update(View view) {
String name = etName.getText().toString().trim();
String number = etNumber.getText().toString().trim();
String score = etScore.getText().toString().trim();
String gender = "";
if (rbMan.isChecked()) {
gender = "男";
}
if (rbWoman.isChecked()) {
gender = "女";
}
Student student = new Student();
student.setName(name);
student.setNumber(number);
student.setGender(gender);
student.setScore(score);
// 更新数据库
long rowId = mMySQLiteOpenHelper.updateData(student);
if (rowId > 0) {
ToastUtil.toastShort(this, "更新成功!");
} else {
ToastUtil.toastShort(this, "没有数据被更新!");
}
}
} 标签:findViewById,日报,private,3.7,软件工程,student,import,android,id From: https://www.cnblogs.com/HD-Kudu-2219/p/18059749