android SQlite结合SimpleCursorAdapter的简单使用

SQLite类:

package com.example.cuboo.mytest;

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

/**
 * Created by cuboo on 2016/8/12.
 */
public class mysqlite extends SQLiteOpenHelper {
    public mysqlite(Context context,String name,int version) {
        super(context, name, null,version);
    }

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

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

调用:

package com.example.cuboo.mytest;

import android.content.ContentValues;
import android.content.Context;
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.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

	private EditText ev_name, ev_age, ev_query;
	private Button btn_add, btn_query, btn_queryall;
	private ListView lv_query;
	private SimpleCursorAdapter adapter;
	private mysqlite dp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ev_name = (EditText) findViewById(R.id.ev_name);
		ev_age = (EditText) findViewById(R.id.ev_age);
		ev_query = (EditText) findViewById(R.id.ev_query);
		btn_add = (Button) findViewById(R.id.btn_add);
		btn_query = (Button) findViewById(R.id.btn_query);
		btn_queryall = (Button) findViewById(R.id.btn_queryall);
		lv_query = (ListView) findViewById(R.id.lv_query);

		// 创建数据库
		dp = new mysqlite(this, "user", 1);
	

		btn_add.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				String name = ev_name.getText().toString();
				int age = Integer.parseInt(ev_age.getText().toString());
				insert(name, age);
			}
		});

		btn_query.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				query(ev_query.getText().toString());
			}
		});

		btn_queryall.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				queryall();
			}
		});
	}

	private void insert(String name, int age) {//插入
		SQLiteDatabase dbwriter = dp.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put("name", name);
		values.put("age", age);
		dbwriter.insert("user", null, values);
		System.out.println(age);

		dbwriter.close();
	}

	private void query(String name) { //查询
		SQLiteDatabase dbreader = dp.getReadableDatabase();
		Cursor cursor = dbreader
				.query("user", new String[] { "_id", "name", "age" },
						"name like ?", new String[] { name }, null, null, null);
		adapter = new SimpleCursorAdapter(this, R.layout.list_aty, cursor,
				new String[] { "name", "age" }, new int[] { R.id.tv_name,
						R.id.tv_age });
		lv_query.setAdapter(adapter);
		dbreader.close();
	}

	private void queryall() {//查询全部
		SQLiteDatabase dbreader = dp.getReadableDatabase();
		Cursor cursor = dbreader.query("user", null, null, null, null, null,
				null);

		adapter = new SimpleCursorAdapter(this, R.layout.list_aty, cursor,
				new String[] { "name", "age" }, new int[] { R.id.tv_name,
						R.id.tv_age });
		lv_query.setAdapter(adapter);
		dbreader.close();
	}
}


你可能感兴趣的:(Android)