Android中sqlite的学习
实现了增加数据,删除数据,跟查询。绑定Spinner控件跟ListView控件,实现点击列表删除选项。
package com.example.splite; import java.util.ArrayList; import java.util.List; 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 DbHelpe extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "test"; private static final String TABLE_LABELS = "labels"; private static final String KEY_ID = "id"; private static final String KEY_NAME = "name"; SQLiteDatabase db; public DbHelpe(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub String sql = "CREATE TABLE " + TABLE_LABELS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS" + TABLE_LABELS); onCreate(db); } //增加 public void insertLabel(String label){ db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, label); db.insert(TABLE_LABELS, null, values); db.close(); // Closing database connection } //查找 public List<String> getAllLabels(){ List<String> labels = new ArrayList<String>(); String selectQuery = "SELECT * FROM " + TABLE_LABELS; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); if (cursor.moveToFirst()) { do { labels.add(cursor.getString(1)); } while (cursor.moveToNext()); } cursor.close(); return labels; } //删除 public void delete (String name){ if(db==null) db=this.getReadableDatabase(); String sql = "delete from labels where name='"+name+ "'"; db.execSQL(sql); } }
主类里有一个方法,就是当点击添加数据按钮后,系统的文字输入框会自动隐藏,实现代码
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
package com.example.splite; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.ScrollView; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener { Spinner spinner; EditText edittext; Button button; ScrollView scr; ListView list; Cursor mcursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = (ListView)findViewById(R.id.listview); button = (Button)findViewById(R.id.btn_add); edittext = (EditText)findViewById(R.id.input_label); spinner = (Spinner)findViewById(R.id.spinner); list.setOnItemClickListener(this); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub String data = edittext.getText().toString().trim(); if(data.trim().length()>0){ DbHelpe db = new DbHelpe(getApplicationContext()); db.getWritableDatabase(); int a = 0; a=db.getAllLabels().size(); db.insertLabel(data); Log.d("ddd", db.getAllLabels().size()+""); if(db.getAllLabels().size()>a){ Toast.makeText(getApplicationContext(), "插入成功", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(getApplicationContext(), "插入失败", Toast.LENGTH_SHORT).show(); } Log.d("aa", a+""); edittext.setText(""); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0); loadData(); }else { Toast.makeText(getApplicationContext(), "请输入数据", Toast.LENGTH_SHORT).show(); } } }); loadData(); } public void loadData(){ DbHelpe db = new DbHelpe(getApplicationContext()); List<String> data = db.getAllLabels(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); ArrayAdapter<String> listadapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, data); list.setAdapter(listadapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub DbHelpe db = new DbHelpe(getApplicationContext()); db.getReadableDatabase(); String data = arg0.getItemAtPosition(arg2).toString(); Log.d("sss", data); db.delete(data); loadData(); } }