## 数据库的增删改查操作和数据查询在Recycleview 上的展示
思路:1、数据库的增删改查操作以api的方式展现。
增:操作主要是增加name age;
删:以name为删除的条件删除数据
改:主要是改数据的name(没做修改age)
查:1、查找所有数据(有按条件查找的代码现在贴出,只有判断条件给大家一个思路“伪代码”)
Cursor cursor;
String key = "", value = "";
if (!nameStr.equals("")) {
key = "name";
value = nameStr;
} else if (!ageStr.equals("")) {
key = "age";
value = ageStr;
}
if (key.equals("")) {
cursor = dao2.selectStudent();
} else {
cursor = dao2.selectStudent(key, value);
}
2、展示的数据由数据库查询出来的数据存放在List集合里面,在通过recycleview的adapter数据绑定显示。
一、代码展示:
1、activity_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入name" />
<EditText
android:id="@+id/edit_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入age" />
<EditText
android:id="@+id/edit_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="删除的edit" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/update_before_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="修改行的name" />
<EditText
android:id="@+id/update_after_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="修改后的行name" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:onClick="operate"
android:text="添加" />
<Button
android:id="@+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:onClick="operate"
android:text="删除" />
<Button
android:id="@+id/update_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:onClick="operate"
android:text="改" />
<Button
android:id="@+id/select_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:onClick="operate"
android:text="查看" />
</LinearLayout>
<TextView
android:id="@+id/show_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="select textview" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycle_list"/>
</LinearLayout>
2、MainActivity2.java:
package com.example.okio;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class MainActivity2 extends AppCompatActivity {
private EditText edit_name;
private TextView show_text;
private EditText edit_age;
private EditText edit_data;
private EditText update_before_name;
private EditText update_after_name;
private RecyclerView recycle_list;
StudentDao2 dao2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
edit_name = findViewById(R.id.edit_name);
edit_age = findViewById(R.id.edit_age);
show_text = findViewById(R.id.show_text);
recycle_list = findViewById(R.id.recycle_list);
edit_data = findViewById(R.id.edit_data);
dao2 = new StudentDao2(this);
update_before_name = findViewById(R.id.update_before_name);
update_after_name = findViewById(R.id.update_after_name);
}
public void operate(View v) {
String nameStr = edit_name.getText().toString();
String ageStr = edit_age.getText().toString();
String update_before_value = update_before_name.getText().toString();
String update_after_value = update_after_name.getText().toString();
switch (v.getId()) {
case R.id.add_btn:
Student stu = new Student(nameStr, Integer.parseInt(ageStr));
dao2.addStudent(stu);
Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
break;
case R.id.delete_btn:
String edit_data_value = edit_data.getText().toString();
dao2.deleteStudent(edit_data_value);
break;
case R.id.update_btn:
dao2.updateStudent(update_before_value, update_after_value);
break;
case R.id.select_btn:
ArrayList<Student> list = new ArrayList<>();
list = dao2.getStudentList();
Radapter radapter = new Radapter(list,this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL,false);
recycle_list.setLayoutManager(linearLayoutManager);
recycle_list.setAdapter(radapter);
break;
}
}
}
3、增删改查封装的class:StudentDao2.java:
package com.example.okio;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class StudentDao2 {
private SQLiteDatabase db;
//一个构造函数,在里面声明SQLiteOpenHelper
public StudentDao2(Context context) {
SQLiteOpenHelper helper = new SQLiteOpenHelper(context, "user2", null, 1) {
@Override
public void onCreate(SQLiteDatabase db) {
//自定义创建一个存放数据库的地址:context.getDatabasePath("heihei.db").getAbsolutePath()
//通过SQLiteDatabase.openDatabase去打开数据库
//flags:SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY是读写的设置,需要时创建可创建
db = SQLiteDatabase.openDatabase(context.getDatabasePath("heihei.db").getAbsolutePath(),
null, SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY);
db.execSQL("create table if not exists user2( name varchar(255), age integer)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
};
// 调用该对象的getWritableDatabase 或 getReadableDatabase方法获得SQLiteDatabase 对象。
db = helper.getWritableDatabase();
}
//增加数据
public void addStudent(Student student) {
ContentValues values = new ContentValues();
values.put("name", student.getName());
values.put("age", student.getAge());
db.insert("user2", null, values);
}
//查询数据
public Cursor selectStudent(String... strs) {
/* //查询所有(没有参数)
String sql2 = "select * from user";
//含条件的查询(姓名/年纪)(参数形式:第一个参数值指明条件,第二个参数名指明条件值)
if (strs.length != 0) {
sql2 += "where " + strs[0] + "='" + strs[1] + "'";
}*/
Cursor cursor = db.rawQuery("select * from user2", null);
return cursor;
}
//删除数据
public void deleteStudent(String s) {
db.delete("user2", "name=?", new String[]{s});
}
//参数一:是要修改的值 参数二:是修改的值
public void updateStudent(String s1, String s2) {
//只能修改name
ContentValues values2 = new ContentValues();
values2.put("name", s2);
db.update("user2", values2, "name=?", new String[]{s1});
}
//利用Recycleview去展示数据
//第一步将数据库里的数据查询出来放进list集合里
public ArrayList<Student> getStudentList(String... strs) {
ArrayList<Student> list = new ArrayList<>();
Cursor cursor = selectStudent(strs);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Student s = new Student(name, age);
list.add(s);
}
return list;
}
}
4、方法:Student.java:
package com.example.okio;
public class Student {
private String name;
private int age;
private String before_name;
public String getBefore_name() {
return before_name;
}
public void setBefore_name(String before_name) {
this.before_name = before_name;
}
public String getAfter_name() {
return after_name;
}
public void setAfter_name(String after_name) {
this.after_name = after_name;
}
private String after_name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String nameStr, String ageStr, String idStr) {
}
}
5、select_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/name_item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/age_item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
6、Radapter.java:
必须要写content的构造方法:不然无法获取当前上下文
package com.example.okio;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import java.util.ArrayList;
import java.util.List;
public class Radapter extends RecyclerView.Adapter<Radapter.viewHolder> {
private Context context;
private TextView name_item;
private TextView age_item;
private ArrayList<Student> list = new ArrayList<>();
public Radapter(ArrayList<Student> list,Context context) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater from = LayoutInflater.from(this.context);
View inflate = from.inflate(R.layout.select_listview, parent, false);
// View view1 = LayoutInflater.from(context).inflate(R.layout.select_listview, parent, false);
return new viewHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull viewHolder holder, int position) {
Student student = list.get(position);
name_item.setText(student.getName());
age_item.setText(student.getAge()+"");
System.out.println(student.getName());
System.out.println(student.getAge());
}
@Override
public int getItemCount() {
return list.size();
}
class viewHolder extends ViewHolder {
public viewHolder(@NonNull View itemView) {
super(itemView);
name_item = itemView.findViewById(R.id.name_item);
age_item = itemView.findViewById(R.id.age_item);
}
}
}