Question

I am not an android developer, I prefer C# but, I want to play with Android a bit since I have an android device, I apologize in advance for it.

I want to develop a simple app in android with the basic CRUD functions (create, read, update and delete) while the basic option is to use SQLiteOpenHelper I've seen DAO generators (like greenDAO) that make life more simple as I see it.

So, the first question is what DAOgenerator to use (if it all)?

Next, how to properly show it, while I know I need listview I still don't figure out exactly how to bind the database to it and to the querys. I'll make for proper display each time I make a new query from a "search" screen, to the main "list display" screen.

No correct solution

OTHER TIPS

You need to create first database handler file like this one..

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;
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top