• SQLite Database


    布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <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=".MainActivity">
    
        <EditText
            android:id="@+id/editText_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:ems="10"
            android:hint="Input name?"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <EditText
            android:id="@+id/editText_age"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="11dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:ems="10"
            android:hint="Input age?"
            android:inputType="number"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText_name" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="44dp"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp"
            android:onClick="add"
            android:text="Add"
            app:layout_constraintEnd_toStartOf="@+id/button2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/editText_age" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="6dp"
            android:layout_marginRight="6dp"
            android:onClick="delete"
            android:text="Delete"
            app:layout_constraintBottom_toTopOf="@+id/listView"
            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:layout_marginEnd="2dp"
            android:layout_marginRight="2dp"
            android:onClick="update"
            android:text="Update"
            app:layout_constraintBottom_toTopOf="@+id/listView"
            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:layout_marginEnd="1dp"
            android:layout_marginRight="1dp"
            android:onClick="retrieve"
            android:text="Retrieve"
            app:layout_constraintBottom_toTopOf="@+id/listView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/button3" />
    
        <ListView
            android:id="@+id/listView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button" />
    </android.support.constraint.ConstraintLayout>
    

    MySQLHelper

    package com.example.sqlitedemo2;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    public class MySQLHelper extends SQLiteOpenHelper {
    
        public static final String PersonTable = "person";
    
        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 if not exists person(_id integer primary key autoincrement,name text,age integer)");
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
        }
    }
    
    

    MainActivity.java

    package com.example.sqlitedemo2;
    
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.AsyncTask;
    import android.support.annotation.Nullable;
    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 {
    
        MySQLHelper mySQLHelper;
        SQLiteDatabase db;
        EditText editText_name,editText_age;
        ListView listView;
    
        SimpleCursorAdapter adapter;
    
        @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);
    
            mySQLHelper = new MySQLHelper(this,"db",null,1);
            db = mySQLHelper.getWritableDatabase();
            Cursor cursor = db.query(MySQLHelper.PersonTable,null,null,null,null,null,null,null);
    
            adapter = 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(adapter);
        }
    
        public void add(View view){
            String name = editText_name.getText().toString();
            String age = editText_age.getText().toString();
    //        db.execSQL("insert into person(name,age) values('"+name+"','"+age+"')");
            ContentValues contentValues = new ContentValues();
            contentValues.put("name",name);
            contentValues.put("age",age);
            db.insert(MySQLHelper.PersonTable,null,contentValues);
    
            reload();
    
        }
    
        public void delete(View view){
            String name = editText_name.getText().toString();
            db.delete(MySQLHelper.PersonTable,"name=?",new String[]{name});
            reload();
        }
    
    
        public void update(View view){
            String name = editText_name.getText().toString();
            String age = editText_age.getText().toString();
            ContentValues contentValues = new ContentValues();
            contentValues.put("name",name);
            contentValues.put("age",age);
            db.update(MySQLHelper.PersonTable,contentValues,"name=?",new String[]{name});
            reload();
        }
    
        public void retrieve(View view){
            String name = editText_name.getText().toString();
            new MyAsyncTask().execute("%"+name+"%");
    //        Cursor cursor=db.query(MySQLHelper.PersonTable,null,"name like *",new String[]{name},null,null,null,null);
    //        adapter.swapCursor(cursor);
        }
    
    
        private void reload() {
            Cursor cursor = db.query(MySQLHelper.PersonTable,null,null,null,null,null,null,null);
            adapter.swapCursor(cursor);
        }
    
        class  MyAsyncTask extends AsyncTask<String, Nullable,Cursor>{
    
            @Override
            protected Cursor doInBackground(String... strings) {
                Cursor cursor=db.query(MySQLHelper.PersonTable,null,"name like ?",new String[]{"%"+strings[0]+"%"},null,null,null,null);
                return cursor;
            }
    
            @Override
            protected void onPostExecute(Cursor cursor) {
                super.onPostExecute(cursor);
                adapter.swapCursor(cursor);
            }
        }
    }
    
    
    
  • 相关阅读:
    Java框架第二次课
    知识扩展1——最大似然估计
    知识扩展2——熵 ,KL散度,交叉熵,JS散度,Wasserstein 距离(EarthMover距离)
    知识扩展3——广义线性模型GLM
    3 逻辑回归
    朴素贝叶斯
    GAN 的后序
    mysql 多个字段字符串合并成一个字段
    qt离线下载
    “用户”,你凭什么说改框架?
  • 原文地址:https://www.cnblogs.com/lyszyl/p/10804989.html
Copyright © 2020-2023  润新知