利用SQLite做出student数据库,使用RecyclerView
可与之前的文章https://blog.csdn.net/easonfff/article/details/79743154
ContentProvider&ContentResolver结合并用
布局代码
activity_main
<RelativeLayout 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"
tools:context="com.homework.activity.sqlrecyclerview.MainActivity">
<LinearLayout
android:id="@+id/header_ll"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<EditText
android:id="@+id/add_et"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/add_btn"
android:text="添加"
android:layout_width="80dp"
android:layout_height="match_parent" />
LinearLayout>
<LinearLayout
android:layout_alignParentBottom="true"
android:id="@+id/bottom_ll"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<Button
android:id="@+id/random_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="随机显示"/>
<Button
android:id="@+id/all_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="全部列出"/>
LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_below="@id/header_ll"
android:layout_above="@id/bottom_ll"
android:id="@+id/Student_rlv"
android:layout_width="match_parent"
android:layout_height="match_parent">
android.support.v7.widget.RecyclerView>
RelativeLayout>
RecyclerView布局视图代码
main_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<TextView
android:gravity="center_vertical"
android:text="张三"
android:textSize="30sp"
android:id="@+id/name_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="160dp"
android:layout_height="60dp">
<Button
android:id="@+id/delete_btn"
android:text="删除"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="@+id/modify_btn"
android:text="修改"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
LinearLayout>
LinearLayout>
DBHelper步骤
1. 创建数据库student
2. 继承SQLiteOpenHelper
3. 在onCreate中调用建表语句
DBHelper
public class DBHelper extends SQLiteOpenHelper{
private String sql="create table student(" +
"id integer primary key autoincrement not null," +
"name text)";
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
工具箱entity
Student实体类
public class Student {
String Name;
public Student(String name) {
Name = name;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
适配器StudentAdapter步骤
StudentAdapter
public class StudentAdapter extends RecyclerView.Adapter{
private List studentList;
private Context context;
public StudentAdapter(List studentList, Context context) {
this.studentList = studentList;
this.context=context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.main_item,parent,false);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.nameTv.setText(studentList.get(position).getName());
//删除学生信息
holder.delBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//删除当前行
deleteStudent(position);
}
});
holder.modBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//修改当前行学生姓名
modifyStudent(position);
}
});
}
private void modifyStudent(final int position) {
final EditText editText=new EditText(context);
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("修改"+studentList.get(position).getName());
builder.setView(editText);
// builder.setMessage("您确定要修改"+studentList.get(position).getName()+"吗");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Toast.makeText(context,editText.getText().toString(),Toast.LENGTH_SHORT).show();
//修改更新数据库
DBHelper dbHelper=new DBHelper(context,"student_db",null,1);
SQLiteDatabase database=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",editText.getText().toString());
database.update("student",values,"name=?",new String[]{studentList.get(position).getName()});
//修改list内容
studentList.get(position).setName(editText.getText().toString());
notifyDataSetChanged();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
private void deleteStudent(final int position) {
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("删除提醒");
builder.setMessage("您确定要删除"+studentList.get(position).getName()+"吗");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//删除
DBHelper dbHelper=new DBHelper(context,"student_db",null,1);
SQLiteDatabase database=dbHelper.getWritableDatabase();
database.delete("student","name=?",new String[]{studentList.get(position).getName()});
studentList.remove(position);
notifyDataSetChanged();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
@Override
public int getItemCount() {
return studentList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView nameTv;
Button delBtn;
Button modBtn;
public ViewHolder(View itemView) {
super(itemView);
nameTv=itemView.findViewById(R.id.name_tv);
delBtn=itemView.findViewById(R.id.delete_btn);
modBtn=itemView.findViewById(R.id.modify_btn);
}
}
}
MainActivity步骤
1. 绑定id,绑定列表RecyclerView,绑定适配器
2. 设置按钮监听,添加增加数据、全部显示、随机显示功能
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText addEt;
private Button addBtn;
private Button radomBtn;
private Button allBtn;
private List studentList=new ArrayList<>();
private RecyclerView recyclerView;
private StudentAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindID();
}
private void bindID() {
addEt=findViewById(R.id.add_et);
addBtn=findViewById(R.id.add_btn);
radomBtn=findViewById(R.id.random_btn);
allBtn=findViewById(R.id.all_btn);
recyclerView=findViewById(R.id.Student_rlv);
addBtn.setOnClickListener(this);
radomBtn.setOnClickListener(this);
allBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.add_btn:
addStudentToDB();
break;
case R.id.random_btn:
randomStudentToDB();
break;
case R.id.all_btn:
queryAll();
break;
}
}
private void queryAll() {
studentList.clear();
DBHelper dbHelper=new DBHelper(this,"student_db",null,1);
SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase();
Cursor cursor=sqLiteDatabase.query("student",null,null,null,null,null,null);
cursor.moveToFirst();
do{
String name=cursor.getString(cursor.getColumnIndex("name"));
Student s=new Student(name);
studentList.add(s);//取出所有学生姓名,填充到列表
}while (cursor.moveToNext());
adapter=new StudentAdapter(studentList,this);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
}
private void addStudentToDB(){
String name=addEt.getText().toString();
DBHelper dbHelper=new DBHelper(this,"student_db",null,1);
SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",name);
sqLiteDatabase.insert("student",null,values);
}
private void randomStudentToDB(){
DBHelper dbHelper=new DBHelper(this,"student_db",null,1);
SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase();
Cursor cursor=sqLiteDatabase.query("student",null,null,null,null,null,null);
cursor.moveToFirst();
do{
String name=cursor.getString(cursor.getColumnIndex("name"));
Student s=new Student(name);
studentList.add(s);
}while (cursor.moveToNext());
cursor.close();
//绑定adapter
adapter=new StudentAdapter(studentList,this);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
}
}