公共类 AndroidListViewCursorAdaptorActivity 扩展 Activity {
private BisdakDbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dbHelper = new BisdakDbAdapter(this);
dbHelper.open();
//Clean all data
dbHelper.deleteAllData();
//Add some data
dbHelper.insertData();
//Generate ListView from SQLite Database
displayListView();
}
private void displayListView() {
Cursor cursor = dbHelper.fetchAllData();
// The desired columns to be bound
String[] columns = new String[] {
BisdakDbAdapter.ENGLISH,
BisdakDbAdapter.TAGALOG,
BisdakDbAdapter.BISAYA,
BisdakDbAdapter.DESCRIPTION
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.english,
//R.id.tagalog,
//R.id.bisaya,
//R.id.description,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(this, R.layout.translator_info, cursor, columns, to);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
**this portion is the onclick listener Items .......**
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
*public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
数据将显示在 searchWord 类中,如下所示
**Toast.makeText(getApplicationContext(),
"English: " + cursor.getString(1) + "\n" +
"Tagalog: " + cursor.getString(2) + "\n" +
"Bisaya: " + cursor.getString(3) + "\n" +
"Parts of Speech: " + cursor.getString(4),
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(AndroidListViewCursorAdaptorActivity.this, searchWord.class);
startActivity(intent);***
}
});
我的 searchWord 类。这是下一个活动。。
公共类 searchWord 扩展 Activity {
private BisdakDbAdapter dbHelper;
TextView textView1, textView2, textView3, textView4;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.translator_info);
dbHelper = new BisdakDbAdapter(this);
dbHelper.open();
}
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
textView1 = (TextView) findViewById(R.id.english);
textView1.setText(cursor.getString(1));
textView2 = (TextView) findViewById(R.id.tagalog);
textView2.setText(cursor.getString(2));
textView3 = (TextView) findViewById(R.id.bisaya);
textView3.setText(cursor.getString(3));
textView4 = (TextView) findViewById(R.id.description);
textView4.setText(cursor.getString(4));
};
}
数据库适配器
公共类 BisdakDbAdapter {
public static final String ROWID = "_id";
public static final String ALPHABET = "alphabet";
public static final String ENGLISH = "english";
public static final String DESCRIPTION = "description";
public static final String TAGALOG = "tagalog";
public static final String BISAYA = "bisaya";
private static final String TAG = "BisdakDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "BisdakDb";
private static final String SQLITE_TABLE = "Words";
private static final int DATABASE_VERSION = 1;
private final Context mCtx;
private static final String DATABASE_CREATE =
"CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
ROWID + " integer PRIMARY KEY autoincrement," +
ENGLISH + "," +
TAGALOG + "," +
BISAYA + "," +
DESCRIPTION + ","+
ALPHABET + ","+
" UNIQUE (" + ENGLISH +"));";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
onCreate(db);
}
}
public BisdakDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public BisdakDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}
public long createData(String alphabet, String english,
String description, String tagalog, String bisaya) {
ContentValues initialValues = new ContentValues();
initialValues.put(ALPHABET, alphabet);
initialValues.put(ENGLISH, english);
initialValues.put(DESCRIPTION, description);
initialValues.put(TAGALOG, tagalog);
initialValues.put(BISAYA, bisaya);
return mDb.insert(SQLITE_TABLE, null, initialValues);
}
public boolean deleteAllData() {
int doneDelete = 0;
doneDelete = mDb.delete(SQLITE_TABLE, null , null);
Log.w(TAG, Integer.toString(doneDelete));
return doneDelete > 0;
}
public Cursor fetchDataByName(String inputText) throws SQLException {
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {ROWID,
ENGLISH, TAGALOG, BISAYA, DESCRIPTION},
null, null, null, null, null);
}
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {ROWID,
ENGLISH, TAGALOG, BISAYA, DESCRIPTION},
ENGLISH + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchAllData() {
Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {ROWID,
ENGLISH, TAGALOG, BISAYA, DESCRIPTION},
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public void insertData() {
createData("a","abaca","noun","abaka","abaka");
createData("a","abandon","verb","pabayaan","biyaan");
createData("a","abbreviate","verb","paikliin","pagmubo");
createData("a","abdomen","noun","tiyan","tiyan");
}
}