插入数据:
更新数据
删除数据
登录微信
单行数据item布局展示代码
<?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="20dp">
<TextView
android:id="@+id/show_id"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:background="#FAF3F3"
android:textColor="#F66B9A"
android:gravity="center"
android:text="" />
<TextView
android:id="@+id/show_name"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:background="#FAF3F3"
android:textColor="#F66B9A"
android:text=""
/>
<TextView
android:id="@+id/show_age"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:background="#FAF3F3"
android:textColor="#F66B9A"
android:text="" />
</LinearLayout>
主界面activityMain布局代码
<?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_Sqlite"
>
<Button
android:id="@+id/insert_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="插入数据" />
<Button
android:id="@+id/update_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="更新数据" />
<Button
android:id="@+id/delete_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="删除数据" />
<Button
android:id="@+id/query_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="查询数据" />
<Button
android:id="@+id/init_db"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清空数据库" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="60dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="18sp"
android:textColor="@color/black"
android:text="name :" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="18sp"
android:textColor="@color/black"
android:text="age :" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/set_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<EditText
android:id="@+id/set_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/login_chat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录微信" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#F6F3F3"
android:orientation="horizontal">
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="@color/black"
android:gravity="center"
android:text="id" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="@color/black"
android:gravity="center"
android:text="name" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="@color/black"
android:gravity="center"
android:text="age" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_show"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
MainActivity_Sqlite代码
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MainActivity_Sqlite extends AppCompatActivity {
private Button insert_data,update_data,delete_data,query_data,init_data,login_chat;
private EditText set_name,set_age;
private RecyclerView recyclerView;
private List<Map<String,Object>> data;
private AdapterShow adapterShow;
private TextView id;
private Integer sum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_sqlite);
sum=0;
MyDAO myDAO=new MyDAO(this );
data = new ArrayList<Map<String,Object>>();
initView();
setOnClick(myDAO,this);
}
private void setOnClick(MyDAO myDAO,Context context){
set_name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
set_name.setText("");
}
});
set_age.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
set_age.setText("");
}
});
insert_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String age =set_age.getText().toString();
String name =set_name.getText().toString();
myDAO.myInsert(name,age);
}
});
update_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String age =set_age.getText().toString();
String name =set_name.getText().toString();
myDAO.myupdata(name,age);
}
});
delete_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String age =set_age.getText().toString();
String name =set_name.getText().toString();
myDAO.mydelete(name,age);
}
});
query_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clearList();
String age =set_age.getText().toString();
String name =set_name.getText().toString();
data = myDAO.namequery();
LinearLayoutManager manager = new LinearLayoutManager(context);
adapterShow = new AdapterShow(context,data);
recyclerView.setAdapter(adapterShow);
recyclerView.setLayoutManager(manager);
}
});
login_chat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String age =set_age.getText().toString();
String name =set_name.getText().toString();
if(myDAO.login_chat(age,name)){
Intent intent = new Intent(context,MainActivity.class);
context.startActivity(intent);
}
else
Toast.makeText(context,"用户名或密码错误",Toast.LENGTH_SHORT).show();
}
});
init_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myDAO.initDatabase();
}
});
}
private void clearList(){
if(sum!=0){
data.clear();
adapterShow.notifyDataSetChanged();
}
sum++;
}
private void initView() {
recyclerView = findViewById(R.id.list_show);
insert_data = findViewById(R.id.insert_data);
update_data = findViewById(R.id.update_data);
delete_data = findViewById(R.id.delete_data);
query_data = findViewById(R.id.query_data);
login_chat = findViewById(R.id.login_chat);
set_age = findViewById(R.id.set_age);
set_name = findViewById(R.id.set_name);
id = findViewById(R.id.show_id);
init_data = findViewById(R.id.init_db);
}
}
recyclerView适配器代码
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import java.util.Map;
public class AdapterShow extends RecyclerView.Adapter<AdapterShow.myviewholder> {
private List<Map<String,Object>> data;
private Context context;
private View inflater;
public AdapterShow(Context context,List<Map<String,Object>> data){
this.data= data;
this.context=context;
}
@Override
public myviewholder onCreateViewHolder( ViewGroup viewGroup, int viewType) {
inflater = LayoutInflater.from(context).inflate(R.layout.db_item,viewGroup,false);
myviewholder myviewholder=new myviewholder(inflater);
return myviewholder;
}
@Override
public void onBindViewHolder(myviewholder holder, @SuppressLint("RecyclerView") int position ){
holder.id.setText(data.get(position).get("id").toString());
holder.name.setText(data.get(position).get("name").toString());
holder.age.setText(data.get(position).get("age").toString());
}
@Override
public int getItemCount() {
return data.size();
}
class myviewholder extends RecyclerView.ViewHolder{
TextView id;
TextView name;
TextView age;
public myviewholder( View itemView) {
super(itemView);
id = itemView.findViewById(R.id.show_id);
name=itemView.findViewById(R.id.show_name);
age=itemView.findViewById(R.id.show_age);
}
}
public void removeItem(int position){
data.remove(position);//删除数据源,移除集合中当前下标的数据
notifyItemRemoved(position);//刷新被删除的地方
notifyItemRangeChanged(position,getItemCount()); //刷新被删除数据,以及其后面的数据
}
}
MyOpenHelper代码
package com.example.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("drop table if exists person");
sqLiteDatabase.execSQL("create table person (id integer primary key,"+ "name varcher,age integer)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int ordVersion, int newVersion) {
}
}
接口类MyDAO代码
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MyDAO {
private SQLiteDatabase db;
private List<Map<String,Object>> data;
public MyDAO(Context context){
SQLiteOpenHelper helper =new MyOpenHelper(context,"test.db",null,1);
db= helper.getWritableDatabase();
data = new ArrayList<Map<String,Object>>();
}
public void myInsert(String name,String age) {
db.execSQL("INSERT INTO person VALUES (null, ?, ?)",
new Object[]{name,age });
}
public List<Map<String,Object>> namequery(){
Cursor c = db.rawQuery( "SELECT * FROM person where age>?", new String[]{"1"} );
while (c.moveToNext()) {
@SuppressLint("Range") int id = c.getInt(c.getColumnIndex("id"));
@SuppressLint("Range") String name= c.getString( c.getColumnIndex("name") );
@SuppressLint("Range") int age = c.getInt( c.getColumnIndex("age") );
// result= result+("id="+id+",name="+name+",age="+age+"\n");
Map<String,Object> map=new HashMap<String,Object>();
map.put("id",id);
map.put("name",name);
map.put("age",age);
data.add(map);
}
c.close();
return data;
}
public void myupdata(String name,String age){
db.execSQL("Update person set age=? where name=?",
new Object[]{age,name});
}
public boolean login_chat(String age,String name){
Cursor c = db.rawQuery( "SELECT * FROM person where name = ? and age =?", new String[]{name,age} );
if(c.moveToNext())
return true;
else
return false;
}
public void mydelete(String name,String age){
db.execSQL("delete from person where name=?",
new Object[]{name});
}
public void initDatabase(){
db.execSQL("drop table if exists person");
db.execSQL("create table person (id integer primary key,"+ "name varcher,age integer)");
}
}