package com.cor;
import android.R.anim;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.Toast;
public class TestAndroidActivity extends Activity implements View.OnClickListener{
private static final String TAG = "AcMain";
private Button add;
private Button delete;
private Button update;
private Button select;
private SQLiteDatabase sd;
public String db_name = "gallery.sqlite";
public String table_name = "pic";
final DbHelper helper = new DbHelper(this, db_name, null, 1);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
sd = helper.getWritableDatabase();
initDataBase(sd);
updateSpinner();
}
private void updateSpinner() {
// TODO Auto-generated method stub
Spinner spinner = (Spinner)findViewById(R.id.spinner1);
final Cursor cursor = sd.query("pic", null, null, null, null, null, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item,cursor,new String[]{"filename","description"},new int[]{android.R.id.text1,android.R.id.text2});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
OnItemSelectedListener selectedListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long id) {
// TODO Auto-generated method stub
cursor.moveToPosition(position);
Toast.makeText(TestAndroidActivity.this, cursor.getString(2), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
spinner.setOnItemSelectedListener(selectedListener);
}
private void initDataBase(SQLiteDatabase sd2) {
ContentValues cv = new ContentValues();
cv.put("fileName", "pic1.jpg");
cv.put("description", "圖片1");
sd2.insert(table_name, "", cv);
cv.put("fileName", "pic2.jpg");
cv.put("description", "圖片2");
sd2.insert(table_name, "", cv);
cv.put("fileName", "pic3.jpg");
cv.put("description", "圖片3");
sd2.insert(table_name, "", cv);
cv.put("fileName", "pic4.jpg");
cv.put("description", "圖片4");
sd2.insert(table_name, "", cv);
}
private void findView() {
add = (Button)findViewById(R.id.add);
delete = (Button)findViewById(R.id.delete);
update = (Button)findViewById(R.id.update);
select = (Button)findViewById(R.id.select);
add.setOnClickListener(this);
delete.setOnClickListener(this);
update.setOnClickListener(this);
select.setOnClickListener(this);
}
@Override
public void onClick(View v) {
ContentValues cv = new ContentValues();
switch(v.getId()) {
case R.id.add:
cv.put("filename", "filename");
cv.put("description","description");
long value_insert = sd.insert("pic", null, cv);
if(-1 == value_insert)
{
Toast.makeText(this, "添加圖片失敗", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "添加圖片成功", Toast.LENGTH_SHORT).show();
}
updateSpinner();
break;
case R.id.delete:
long number = sd.delete("pic", "filename='pic2.jpg'", null);
Toast.makeText(this, "刪除了"+number+"條記錄", Toast.LENGTH_SHORT).show();
updateSpinner();
break;
case R.id.update:
cv.put("filename", "hello");
cv.put("description", "world");
long number_update = sd.update("pic", cv, "filename='pic2.jpg'",null);
Toast.makeText(this, number_update+"條記錄被更新", Toast.LENGTH_SHORT).show();
updateSpinner();
break;
case R.id.select:
Cursor c = sd.query("pic", null, null, null, null, null, null);
Toast.makeText(this,
"當前共有" + c.getCount() + "條記錄,下面一一顯示:",
Toast.LENGTH_SHORT).show();
//循環顯示
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
Toast.makeText(this,
"第"+ c.getInt(0) +"條數據,文件名是" + c.getString(1) + ",描述是"+c.getString(2),
Toast.LENGTH_SHORT).show();
}
//更新下拉列表
updateSpinner();
break;
}
}
@Override
protected void onDestroy() {
//this.unbindService(sc);
//this.stopService(
// new Intent("jtapp.myservicesamples.myservice"));
super.onDestroy();
sd.delete(table_name, null, null);
updateSpinner();
}
}
package com.cor;
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, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sql = "CREATE TABLE pic(_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,filename VARCHAR ,description VARCHAR)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
SQLite Android
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.