安卓中实现对SQLITE数据表的增删改查

直接上代码

MainActiviy.java


package com.example.sqllitcache;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends Activity {

	ListView lv;
	Button submit;
	EditText et;
	SQLiteDatabase dbwrite ;
	Dbhelper dbhelper;
	ArrayAdapter <String> adapter; 
	Cursor c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView) findViewById(R.id.listView1);
        submit=(Button) findViewById(R.id.button1);
        et=(EditText) findViewById(R.id.autoCompleteTextView1);
        adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        lv.setAdapter(adapter);
        dbhelper=new Dbhelper(this);
        dbwrite=dbhelper.getWritableDatabase();
        getData();
        //点击提交
        submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String newData=et.getText().toString();
				addData(newData);
			}
		});
        //长按列表项,修改数据
        lv.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					final int position, long arg3) {
				// TODO Auto-generated method stub
				
				new AlertDialog.Builder(MainActivity.this).setPositiveButton("我确定修改",new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						updateData(position);
					}
				}).show();
				return false;
			}
		});
        //短按列表项删除数据
        lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, position+"", 0).show();
				deleteData(position);
			}
		});
    }
    
    //添加数据的执行函数,添加到本地数据库,并提示更新listtview
    void addData(String newData)
    {
    	//添加到本地数据库
    	ContentValues content=new ContentValues();
    	String putdata=et.getText().toString();
        content.put("data", putdata);
        dbwrite.insert("datatable",null, content);
        getData();
    }
    
    //删除一条数据
    void deleteData(int position)
    {
    	c.moveToPosition(position);
		int id=c.getInt(c.getColumnIndex("_id"));
		dbwrite.delete("datatable", "_id=?",  new String[]{id+""} );
		getData(); 
    }
    //修改一条数据
    void updateData(int position)
    {
    	c.moveToPosition(position);
		ContentValues cv=new ContentValues();
		cv.put("data", "changed");
		int id=c.getInt(c.getColumnIndex("_id"));
		dbwrite.update("datatable", cv, "_id=?", new String [] {id+""});
        getData();
    }
    //获取数据
    void getData()
    {
    	c= dbwrite.query("datatable", null, null, null, null, null, null);
    	adapter.clear();
    	 //读取本地数据库 
        while(c.moveToNext())
        {
        	
        	String data=c.getString(c.getColumnIndex("data"));
        	int id=c.getInt(c.getColumnIndex("_id"));
        	adapter.add(data+":"+id);
        	System.out.println(data+": "+id);
        }
        adapter.notifyDataSetChanged();
    }
}


Dbhelper.java


package com.example.sqllitcache;

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

public class Dbhelper extends SQLiteOpenHelper {

	public Dbhelper(Context context ) {
		super(context, "mydb",null, 1);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("CREATE TABLE datatable ("+
		           "_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
		           "data String DEFAULT \"\")"  );
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}

}


你可能感兴趣的:(安卓中实现对SQLITE数据表的增删改查)