編輯:關於Android編程
/* (程序頭部注釋開始)
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙台大學計算機學院學生
* All rights reserved.
* 文件名稱:數據庫應用—添加查詢
* 作 者: 雷恆鑫
* 完成日期: 2012 年 08 月 14 日
* 版 本 號: V1.0
* 對任務及求解方法的描述部分
* 輸入描述:
* 問題描述:
* 程序輸出:
* 程序頭部的注釋結束
*/
方法:在“NotesDbAdapter”類中新增一個“getall”方法來查詢“notes”數據表中的所有數據,修改後的“DummyNote.java”文件如下:
[java]
package com.demo.android.dummynote;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.content.Intent;
import android.widget.ListView;
public class DummyNote extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Tell the list view which view to display when the list is empty
getListView().setEmptyView(findViewById(R.id.empty));
setAdapter();
}
private String[] note_array = {
"gasolin",
"crota",
"louk",
"magicion"
};
private NotesDbAdapter mDbHelper;
private Cursor mNotesCursor;
private void setAdapter(){
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
fillData();
}
private void fillData(){
mNotesCursor = mDbHelper.getall();
startManagingCursor(mNotesCursor);
//Create an array to specify the field we want to display in the list
String[] from = new String[]{NotesDbAdapter.KEY_NOTE};
//an array of the fields we want to bind those fields to
int[] to = new int[]{android.R.id.text1};
//Now create a simple cursor adapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,mNotesCursor,from,to);
setListAdapter(adapter);
}
}
修改後的“DummyDbAdapter.java”文件如下:
[java]
package com.demo.android.dummynote;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class NotesDbAdapter {
private static final String DATABASE_NAME = "notes.db";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_TABLE = "notes";
private static final String DATABASE_CREATE = "creat table notes("
+ "_id INTEGER PRIMARY KEY," + "note TEXT," + "created INTEGER,"
+ "modified INTEGER" + ");";
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(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
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
private Context mCtx = null;
private DatabaseHelper dbHelper;
private SQLiteDatabase db;
public NotesDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public NotesDbAdapter open() throws SQLException {
dbHelper = new DatabaseHelper(mCtx);
db = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
private static final String KEY_ROWID = "_id";
static final String KEY_NOTE = "note";
private static final String KEY_CREATED = "created";
// get all entries
public Cursor getall() {
return db.rawQuery("SELECT * FROM notes", null);
}
}
經驗積累:
1.我學會了如何使用“mDbHelper.getall()”語句。
2.“ListActivity”提供的“setListAdapter”方法,是將接口綁定到“ListView”界面組件上。
作者:leihengxin
前言在上一篇文章中,我們介紹了下拉刷新上拉加載RecyclerView的使用,從這篇開始,我將對這個項目的具體實現詳細介紹,這篇首先介紹添加刪除頭尾部的實現。大家都知道r
為了讓用戶的使用更舒適所以有些情況使用動畫是很有必要的,Android在3.0以前支持倆種動畫Tween動畫以及Frame動畫。Tween動畫支持簡單的平移,縮放,旋轉,
做過Android開發都會陸續用到這個開源庫EventBus。EventBus是一款針對Android優化的發布/訂閱事件總線。主要功能是替代Intent,Handler
相關概念1.Handler:可以看做是一個工具類,用來向消息隊列中插入消息的;2.Thread:所有與Handler相關的功能都是與Thread密不可分的,Handler