Android studio 操作数据库游标适配器

运行效果

Android studio 操作数据库游标适配器_第1张图片

文件结构

Android studio 操作数据库游标适配器_第2张图片

主要代码

MainActivity

package cn.edu.sicnu.sqlitedemo;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends AppCompatActivity {

    EditText editText_name,editText_age;
    ListView listView;

    //操作数据库游标适配器
    SimpleCursorAdapter cursorAdapter;
    SQLiteDatabase db;
    MySQLHelper dbhelper;
    Cursor cursor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText_name = findViewById(R.id.editText_name);
        editText_age = findViewById(R.id.editText_age);
        listView = findViewById(R.id.listView);

        dbhelper = new MySQLHelper(this,"lgy.db",null,1);
        db = dbhelper.getWritableDatabase();
//        cursor = db.rawQuery("select * from person",null);
        cursor = db.query("person",null,null,null,null,null,null);


        cursorAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor,new String[]{"name","age"},new int[]{android.R.id.text1,android.R.id.text2}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        listView.setAdapter(cursorAdapter);
    }

    public void add(View view){
//        db.execSQL("insert into person(name,age) values(?,?)",new Object[]{editText_name.getText().toString(),editText_age.getText().toString()});
        ContentValues contentValues = new ContentValues();
        contentValues.put("name",editText_name.getText().toString());
        contentValues.put("age",editText_age.getText().toString());

        db.insert("person",null,contentValues);


        cursor = db.query("person",null,null,null,null,null,null);
        cursorAdapter.swapCursor(cursor);

        clearEdit();
    }

    public void delete(View view){
//        db.execSQL("delete from person where name=?",new Object[]{editText_name.getText().toString()});

        db.delete("person","name=?",new String[]{editText_name.getText().toString()});

        cursor = db.query("person",null,null,null,null,null,null);
        cursorAdapter.swapCursor(cursor);
        clearEdit();

    }

    public void update(View view){
//        db.execSQL("update person set age=? where name=?",new Object[]{editText_age.getText().toString(),editText_name.getText().toString()});

        ContentValues contentValues = new ContentValues();
        contentValues.put("age",editText_age.getText().toString());

        db.update("person",contentValues,"name=?",new String[]{editText_name.getText().toString()});

        cursor = db.query("person",null,null,null,null,null,null);
        cursorAdapter.swapCursor(cursor);
        clearEdit();

    }

    public void query(View view){
        cursor = db.query("person",null,null,null,null,null,null);
        cursorAdapter.swapCursor(cursor);
    }

    public void clearEdit(){
        editText_name.setText("");
        editText_age.setText("");
        editText_name.requestFocus();
    }

}

MySQLHelper

package cn.edu.sicnu.sqlitedemo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by liguiyang on 2018/5/3.
 */

public class MySQLHelper extends SQLiteOpenHelper {
    public MySQLHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table person(_id integer primary key autoincrement,name text,age integer)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

activity_main.xml


<android.support.constraint.ConstraintLayout 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="cn.edu.sicnu.sqlitedemo.MainActivity">

    <EditText
        android:id="@+id/editText_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="3dp"
        android:ems="10"
        android:hint="Input your name?"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toTopOf="@+id/editText_age"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText_age"
        android:layout_width="353dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Input your age?"
        android:inputType="number"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="@+id/button2"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toBottomOf="@+id/editText_name" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:onClick="add"
        android:text="add"
        app:layout_constraintBottom_toTopOf="@+id/listView"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText_age" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="delete"
        android:text="delete"
        app:layout_constraintBottom_toBottomOf="@+id/button"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintStart_toEndOf="@+id/button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="update"
        android:text="update"
        app:layout_constraintBottom_toBottomOf="@+id/button2"
        app:layout_constraintEnd_toStartOf="@+id/button4"
        app:layout_constraintStart_toEndOf="@+id/button2" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="query"
        android:text="query"
        app:layout_constraintBottom_toBottomOf="@+id/button3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button3" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="368dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/button2"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toBottomOf="@+id/button" />
android.support.constraint.ConstraintLayout>

你可能感兴趣的:(Android,studio)