我不是Android开发者,我更喜欢C#但是,因为我有一个Android设备,我想玩Android一点,我提前道歉。
我想在android中使用基本的CRUD函数(创建,读取,更新和删除)开发一个简单的应用程序 虽然基本的选择是使用SQLiteOpenHelper我已经看到了DAO生成器(如greenDAO),它让生活变得更加简单。
那么,第一个问题是使用DAOgenerator(如果全部)?
接下来,如何正确显示它,虽然我知道我需要listview但我仍然没有弄清楚如何将数据库绑定到它和查询。每次从“搜索”屏幕到主“列表显示”屏幕进行新查询时,我都会进行正确的显示。
答案 0 :(得分:0)
您需要创建第一个这样的数据库处理程序文件..
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;
}
}