布局主要分两个
其中主布局是
对于ListView的布局定义则是
自定义的adapter
package com.example.kimdemon.sqllite;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
public class dapter extends CursorAdapter{
public dapter(Context context,Cursor cursor){
super(context,cursor,0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
return LayoutInflater.from(context).inflate(R.layout.list,viewGroup,false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView name = (TextView) view.findViewById(R.id.yf_name);
name.setText(cursor.getString(cursor.getColumnIndex("name")));
}
}
数据库的创建和打开
package com.example.kimdemon.sqllite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Create extends SQLiteOpenHelper {
private String sql="create table student(_id integer primary key autoincrement,"
+"name text not null ,classmate text not null,age integer)";
public Create(Context context){
super(context,"ManagerInfo",null,1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("drop table if exists person");
onCreate(sqLiteDatabase);
}
}
数据库的操作
package com.example.kimdemon.sqllite;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class Manager {
private Create create;
private SQLiteDatabase sqLiteDatabase;
public Manager(Context context){
create = new Create(context);
}
public void insert(String name){
sqLiteDatabase = create.getWritableDatabase();
String sql = "insert into person(name) values('"+name+"')";
sqLiteDatabase.execSQL(sql);
}
public Cursor selectAll(){
sqLiteDatabase = create.getReadableDatabase();
Cursor cursor = sqLiteDatabase.query("manager",null,null,null,null,null,null);
return cursor;
}
public void delete(String id){
sqLiteDatabase = create.getWritableDatabase();
String sql = "delete person where _id="+String.valueOf(id);
sqLiteDatabase.execSQL(sql);
}
}
主Java代码则是
package com.example.kimdemon.sqllite;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private EditText yf_input;
private ListView yf_listview;
private dapter dapter;
private Create create;
private Manager manager;
private Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yf_input = (EditText) findViewById(R.id.yf_input);
yf_listview = (ListView) findViewById(R.id.yf_listview);
manager = new Manager(this);
cursor = manager.selectAll();
if (cursor != null ){
dapter = new dapter(MainActivity.this,cursor);
yf_listview.setAdapter(dapter);
}
}
public void onClick(View view){
switch (view.getId()){
case R.id.yf_add:
String name = yf_input.getText().toString();
manager.insert(name);
cursor = manager.selectAll();
if (cursor != null){
dapter = new dapter(this,cursor);
yf_listview.setAdapter(dapter);
}
break;
case R.id.yf_delete:
if (cursor != null){
if (cursor.moveToFirst()){
manager.delete(cursor.getString(cursor.getColumnIndex("_id")));
cursor = manager.selectAll();
}
}
dapter = new dapter(this,cursor);
yf_listview.setAdapter(dapter);
break;
}
}
}