主布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/insert"
android:text="增加"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/delete"
android:text="删除"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/update"
android:text="修改"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/select"
android:text="查看"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
</ListView>
</LinearLayout>
主类:
package com.example.shujukiushipei;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.CursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button insert, update, delete, select;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
insert = findViewById(R.id.insert);
update = findViewById(R.id.update);
delete = findViewById(R.id.delete);
select = findViewById(R.id.select);
lv = findViewById(R.id.lv);
MyHekp myHekp = new MyHekp(MainActivity.this);
final SQLiteDatabase writableDatabase = myHekp.getWritableDatabase();//增加 删除 更新
final SQLiteDatabase readableDatabase = myHekp.getReadableDatabase();//查看
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
writableDatabase.execSQL("insert into user ( name ) values ( ? ) ", new String[]{"李晓娜"});
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
writableDatabase.execSQL("update user set name = ? where _id = ? ", new String[]{"胡晓璐", "2"});
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
writableDatabase.execSQL("delete from user where name = ? and _id = ? ", new String[]{"李晓娜", "6"});
}
});
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = readableDatabase.rawQuery("select * from user ", null);
MyAdapter myAdapter = new MyAdapter(MainActivity.this, cursor);
lv.setAdapter(myAdapter);
}
});
}
}
适配器(CursorAdapter):
class MyAdapter extends CursorAdapter {
public MyAdapter(Context context, Cursor c) {
super(context, c);
}
//获取到适配的布局文件,返回回去view
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.cursoradapter, null);
return view;
}
//对布局里面的内容进行设置
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView textView1 = view.findViewById(R.id.s_id);
TextView textView2 = view.findViewById(R.id.s_name);
//通过字段名拿到id 再通过id拿到名字
String id = cursor.getString(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
textView1.setText(id);
textView2.setText(name);
}
}
这个里面的增加删除修改都是写死的代码,如果想要手动进行这些操作的话,可以通过AlertDialog.Builder(对话框)来实现.