我不是安卓开发者,我更喜欢C#,但是,我想玩一下Android,因为我有一个安卓设备,我提前道歉。
我想在安卓上开发一个简单的应用程序,具有基本的CRUD功能(创建,读取,更新和删除)而基本选项是使用 SQLiteOpenHelper,我见过 DAO 生成器(如 greenDAO),它们使生活变得更加简单。
所以,第一个问题是使用什么DAO生成器(如果全部的话)?
接下来,如何正确显示它,虽然我知道我需要 listview,但我仍然不知道如何将数据库绑定到它和查询。每次从"搜索"屏幕到主"列表显示"屏幕进行新查询时,我都会正确显示。
你需要像这样创建第一个数据库处理程序文件。
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static final String DB_NAME = "ContactManager";
private static final String TABLE_NAME = "Contacts";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phNo";
public DatabaseHandler(Context context) {
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String createContact = "Create Table " + TABLE_NAME + " (" + KEY_ID
+ " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PH_NO
+ " TEXT)";
db.execSQL(createContact);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("Drop Table IF EXISTS" + TABLE_NAME);
onCreate(db);
}
// Add New Contacts
public void AddContact(Contact contact) {
SQLiteDatabase DB = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_PH_NO, contact.getPhNo());
DB.insert(TABLE_NAME, null, values);
DB.close();
}
// Read Contact
public Contact ReadContact(int id) {
SQLiteDatabase DB = this.getReadableDatabase();
Cursor cursor = DB.query(TABLE_NAME, new String[] { KEY_ID, KEY_NAME,
KEY_PH_NO }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
DB.close();
return contact;
}
public List<Contact> ReadAllContact() {
//AddContact(new Contact("Arpan", "1234567"));
List<Contact> ContactList = new ArrayList<Contact>();
String query = "Select * from " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhNo(cursor.getString(2));
ContactList.add(contact);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return ContactList;
}
public int UpdateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues value = new ContentValues();
value.put(KEY_NAME, contact.getName());
value.put(KEY_PH_NO, contact.getPhNo());
int norow=db.update(TABLE_NAME, value, KEY_ID + "=?",
new String[] { String.valueOf(contact.getID()) });
db.close();
return norow;
}
public void DeleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_ID + "=?",
new String[] { String.valueOf(contact.getID()) });
db.close();
}
public int getContactCount() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from " + TABLE_NAME, null);
int norow=cursor.getCount();
cursor.close();
db.close();
return norow;
}
}