Android SQLiteOpenHelper(数据库)+CursorAdapter(适配器)

主布局:

<?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(对话框)来实现.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值