Android SQLiteDatabase數據庫, SimpleCursorAdapter, Cursor

Android SQLiteDatabase數據庫, SimpleCursorAdapter, Cursor。

這裡寫圖片描述

創建SimpleCursorAdapter 所指定的layout



    
    

package shortcut.song.com.myapplication;

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

public class DBTestActivity extends AppCompatActivity {
    SQLiteDatabase db;
    Button btn = null;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dbtest);
        //創建或打開數據庫(此處需要使用絕對路經)
        db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/my.db3", null);
        listView = (ListView)findViewById(R.id.listview_db);
        btn = (Button)findViewById(R.id.btn_db);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //獲取用戶輸入
                String title = ((EditText)findViewById(R.id.edit_title_db)).getText().toString();
                String content = ((EditText)findViewById(R.id.edit_content_db)).getText().toString();

                try {
                    insertData(db , title , content);
                    Cursor cursor = db.rawQuery("select * from news_inf", null);
                    inflateList(cursor);
                } catch (SQLiteException e) {
                    //執行DDL創建數據表
                    //執行DDL創建數據表
                    db.execSQL("create table news_inf(_id integer primary key autoincrement,"
                            + " news_title varchar(50),"
                            + " news_content varchar(255))");
                    //執行insert語句插入數據
                    insertData(db , title , content);
                    //執行查詢
                    Cursor cursor = db.rawQuery("select * from news_inf", null);
                    inflateList(cursor);

                }
            }
        });

    }


    private void insertData(SQLiteDatabase db, String title, String content){
        //執行插入語句
        //執行插入語句
        db.execSQL("insert into news_inf values(null , ? , ?)"
                , new String[]{title , content});
    }

    private void inflateList(Cursor cursor) {
        SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(
                DBTestActivity.this
                ,R.layout.db_line, cursor
                ,new String[]{"news_title", "news_content"}
                ,new int[]{R.id.my_title, R.id.my_content}
                , CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        listView.setAdapter(simpleCursorAdapter);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (db != null && db.isOpen()) {
            db.close();
        }
    }
}

運行效果:

這裡寫圖片描述

You May Also Like