TQC-210 Listview操作sqlite新增、刪除、修改
這是TQC的210題,
程式碼為自行撰寫,
僅供參考。
主要練習使用ListView,來操作資料庫新增、刪除、修改,
注意ListView01.setOnItemClickListener事件取得id跟值。
ListView01.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener(){
@Override
public void onItemClick(android.widget.AdapterView arg0,
View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub\
_id=cursor.getInt(0);
EditText01.setText(cursor.getString(1));
Button02.setEnabled(true);
Button03.setEnabled(true);
}});
程式:
package COM.TQC.GDD02;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class GDD02 extends Activity {
private static final String DBNAME = "MY_DB";
private static final String TABLENAME = "MY_TABLE2";
private static final String FIELD01_NAME = "_id";
private static final String FIELD02_NAME = "_text1";
private SQLiteDatabase dataBase;
private android.database.Cursor cursor;
private int _id = -1;
private EditText EditText01;
private Button Button01;
private Button Button02;
private Button Button03;
private ListView ListView01;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findview();
setlistview();
setlisenter();
}
public void findview()
{
EditText01 = (EditText) findViewById(R.id.EditText01);
Button01 = (Button) findViewById(R.id.BtnAdd);
Button02 = (Button) findViewById(R.id.BtnUpdate);
Button03 = (Button) findViewById(R.id.BtnDelete);
ListView01 = (ListView) findViewById(R.id.ListView01);
}
public void setlistview()
{
dataBase = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
String CREATE_SQL = "create table if not exists "+TABLENAME+" ("+FIELD01_NAME+" integer primary key autoincrement, "+FIELD02_NAME+" varchar not null);";
dataBase.execSQL(CREATE_SQL);
cursor=dataBase.query(TABLENAME, null, null, null, null, null, null);
android.widget.SimpleCursorAdapter adapter = new android.widget.SimpleCursorAdapter(this, R.layout.list,
cursor, new String[]
{ FIELD02_NAME }, new int[]
{ R.id.CheckedTextView01 });
this.ListView01.setAdapter(adapter);
}
public void setlisenter()
{
Button02.setEnabled(false);
Button03.setEnabled(false);
Button01.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(!EditText01.getText().equals("")){
insert(""+EditText01.getText());
}
}});
Button02.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(_id!=-1 && !EditText01.getText().equals("")){
update(_id,""+EditText01.getText());
Button02.setEnabled(false);
Button03.setEnabled(false);
}
}});
Button03.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(_id!=-1){
delete(_id);
Button02.setEnabled(false);
Button03.setEnabled(false);
}
}});
ListView01.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener(){
@Override
public void onItemClick(android.widget.AdapterView arg0,
View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub\
_id=cursor.getInt(0);
EditText01.setText(cursor.getString(1));
Button02.setEnabled(true);
Button03.setEnabled(true);
}});
}
private void insert(String text){
ContentValues localContentValues = new ContentValues();
localContentValues.put(FIELD02_NAME, text);
long l = this.dataBase.insert(TABLENAME, null, localContentValues);
boolean bool = this.cursor.requery();
}
private void update(int id,String text)
{
String[] arrayOfString=new String[] {Integer.toString(id)};
ContentValues localContentValues = new ContentValues();
localContentValues.put(FIELD02_NAME, text);
int i = this.dataBase.update(TABLENAME, localContentValues, "_id = ?", arrayOfString);
boolean bool = this.cursor.requery();
}
private void delete(int id)
{
String[] arrayOfString=new String[] {Integer.toString(id)};
int i = this.dataBase.delete(TABLENAME, "_id = ?", arrayOfString);
boolean bool = this.cursor.requery();
}
}
自我LV~