android sqlite和listview 使用小例子

博客迁移


sdk: mini8 target18


项目截图android sqlite和listview 使用小例子_第1张图片


演示


主要代码


adapter

package com.my.dbtest.adapter;

import java.util.ArrayList;
import java.util.List;

import com.icedcap.dbtest.R;
import com.my.dbtest.helper.Persion;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class PersionAdapter extends BaseAdapter {

    public ArrayList persions = null;
    Activity context;
    public ArrayList checkedIds;

    public PersionAdapter(Activity context) {
        super();
        checkedIds = new ArrayList();
        this.context = context;
        persions = new ArrayList();
    }
    public void setPersions(ArrayList persions){
        this.persions = persions;
    }
    @Override
    public int getCount() {
        return persions.size();
    }

    @Override
    public Object getItem(int position) {
        return persions.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("static-access")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        final Persion item = this.persions.get(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(context).from(context).inflate(R.layout.persionitem, null);

            holder = new ViewHolder();
            holder.ck = (CheckBox)convertView.findViewById(R.id.ck);
            holder.tv1 = (TextView)convertView.findViewById(R.id.tv1);
            holder.tv2 = (TextView)convertView.findViewById(R.id.tv2);
            holder.tv3 = (TextView)convertView.findViewById(R.id.tv3);
            holder.tv4 = (TextView)convertView.findViewById(R.id.tv4);
            holder.tv5 = (TextView)convertView.findViewById(R.id.tv5);
            holder.ck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if(isChecked){
                        checkedIds.add(holder.tv1.getText().toString());
                    }else{
                        checkedIds.remove(holder.tv1.getText().toString());
                    }
                    item.CheckState = isChecked;
                }
            });
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv1.setText(item.Id);
        holder.tv2.setText(item.Name);
        holder.tv3.setText(item.Age);
        holder.tv4.setText(item.Height);
        holder.tv5.setText(item.Weight);
        holder.ck.setChecked(item.CheckState);
        //赋值
        return convertView;
    }

    class ViewHolder {
        CheckBox ck;
        TextView tv1;
        TextView tv2;
        TextView tv3;
        TextView tv4;
        TextView tv5;
    }
}

dbHeper


package com.my.dbtest.helper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbHelper extends SQLiteOpenHelper {
    private SQLiteDatabase db = null;
    private Cursor cursor = null;
    private String TAG = "DBTest";
    private boolean isInit;
    private String createString;

    public DbHelper(Context context, String dbName, CursorFactory factory,
            int version) {
        super(context, dbName, factory, version);
    }

    // 建表
    @Override
    public void onCreate(SQLiteDatabase db) {
        this.db = db;
        db.execSQL(createString);
    }

    public void inItDb(String createString) {
        if (isInit)
            return;
        this.createString = createString;
        this.isInit = true;
    }

    // 增
    public void insert(ContentValues values, String tableName) {
        SQLiteDatabase db = getWritableDatabase();
        System.out.println("values" + values.get("id") + values.get("name")
                + values.get("age"));
        db.insert(tableName, null, values);
        Log.i(TAG, "增加一行");
        db.close();
    }

    // 删除某一行
    public void delete(String id, String tableName) {
        // if (db == null) {
        SQLiteDatabase db = getWritableDatabase();
        // }
        db.delete(tableName, "id=?", new String[] { String.valueOf(id) });
        Log.i(TAG, "删除一行");
    }

    // 更新某一行
    public void update(ContentValues values, String string, String tableName) {
        SQLiteDatabase db = getWritableDatabase();
        db.update(tableName, values, "id=?", new String[] { string });
        db.close();
        Log.i(TAG, "更新一行");
    }

    // 按id查询
    public Cursor query(String string, String tableName) {
        SQLiteDatabase db = getWritableDatabase();
        System.out.println("id---->" + string);
        cursor = db.query(tableName, null, "id=?", new String[] { string },
                null, null, null);
        Log.i(TAG, "按id查询一行");
        return cursor;
    }

    public Cursor query(String tableName) {
        SQLiteDatabase db = getWritableDatabase();
        cursor = db.query(tableName, null, null, null, "id", null, "id");
        Log.i(TAG, "查询所有");
        return cursor;
    }

    // 按sql语句操作数据库
    public void handleBySql(String sql) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL(sql);
        Log.i(TAG, "执行sql语句");
    }

    // 关闭数据库
    public void close() {
        if (db != null) {
            db.close();
            db = null;
        }
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}



项目源码:http://pan.baidu.com/s/1c0fR48S

你可能感兴趣的:(android)