Note that there are some explanatory texts on larger screens.

plurals
  1. PONot able to retrieve data from DB second time in Android
    primarykey
    data
    text
    <p>I have created an app that can save contacts from text box to database. Contacts would be number entered or from default contacts. This app has about 5 edit text.</p> <p>The problem over here is: The first time when I enter the number and click save button the number is getting saved and also gets retrieved properly. When I do the same the second time for a new number. The number is getting saved. When I try to retrieve the numbers from DB I can't see the number. </p> <p>I am using getter and setter methods for storing the transaction values.</p> <p>Below is what I have tried and getting the problem: </p> <p>From edittext to database via getters and setters method.</p> <pre><code> Contacts contacts = new Contacts(); //arrsendcontactsinverse is the arraylist which stores all the numbers contacts.setArrcontactsofbean(arrsendcontactsinverse); database.addContacts(contacts); </code></pre> <p>//to retrieve the data i am using the code below.</p> <pre><code>db = new DataBase(this); if(db.getContactsCount()&gt;0) { Contacts contactFromDb = db.getContacts(); contact1.setText(contactFromDb.getArrcontactsofbean().get(0)); contact2.setText(contactFromDb.getArrcontactsofbean().get(1)); contact3.setText(contactFromDb.getArrcontactsofbean().get(2)); contact4.setText(contactFromDb.getArrcontactsofbean().get(3)); contact5.setText(contactFromDb.getArrcontactsofbean().get(4)); } } </code></pre> <p>This is the code in the DataBase.java.</p> <pre><code>public class DataBase extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String TABLE_CONTACTS = "ContactsTable"; private static final String KEY_CONTACT1 = "contact1"; //column name private static final String KEY_CONTACT2 = "contact2"; //column name private static final String KEY_CONTACT3 = "contact3"; //column name private static final String KEY_CONTACT4 = "contact4"; //column name private static final String KEY_CONTACT5 = "contact5"; //column name ArrayList&lt;String&gt; arrcontactofdatabasehandler= new ArrayList&lt;String&gt;(); public Databasehandler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } public void onCreate(SQLiteDatabase db) { String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS +"(" +KEY_CONTACT1+ " TEXT," +KEY_CONTACT2+ " TEXT," +KEY_CONTACT3+ " TEXT," +KEY_CONTACT4+ " TEXT," +KEY_CONTACT5+ " TEXT," + ")"; db.execSQL(CREATE_CONTACTS_TABLE); } // Adding new contact void addContacts(Contacts contacts) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_CONTACT1,contacts.getArrcontactsofbean().get(0)); values.put(KEY_CONTACT2,contacts.getArrcontactsofbean().get(1)); values.put(KEY_CONTACT3,contacts.getArrcontactsofbean().get(2)); values.put(KEY_CONTACT4,contacts.getArrcontactsofbean().get(3)); values.put(KEY_CONTACT5,contacts.getArrcontactsofbean().get(4)); db.insert(TABLE_CONTACTS, null, values); db.close(); } // Getting contacts Contacts getContacts() { String selectQuery = "SELECT * FROM " + TABLE_CONTACTS; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); Contacts contacts = new Contacts(); // looping through all rows and adding to list if (cursor.moveToFirst()) { do { arrcontactofdatabasehandler.add(cursor.getString(0)); arrcontactofdatabasehandler.add(cursor.getString(1)); arrcontactofdatabasehandler.add(cursor.getString(2)); arrcontactofdatabasehandler.add(cursor.getString(3)); arrcontactofdatabasehandler.add(cursor.getString(4)); } while (cursor.moveToNext()); } contacts.setArrcontactsofbean(arrcontactofdatabasehandler); cursor.close(); db.close(); return contacts; } public int getContactsCount() { String countQuery = "SELECT * FROM " + TABLE_CONTACTS; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); int count = cursor.getCount(); cursor.close(); // return count db.close(); return count; } public void dropTable() { SQLiteDatabase db = this.getWritableDatabase(); db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS); String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS +"(" +KEY_CONTACT1+ " TEXT," +KEY_CONTACT2+ " TEXT," +KEY_CONTACT3+ " TEXT," +KEY_CONTACT4+ " TEXT," +KEY_CONTACT5+ " TEXT," + ")"; db.execSQL(CREATE_CONTACTS_TABLE); db.close(); } } </code></pre> <p>Contacts.java.(these are the getter and setter methods )</p> <pre><code> public class Contacts { ArrayList&lt;String&gt; arrcontactsofbean= new ArrayList&lt;String&gt;(); public Contacts() { } public ArrayList&lt;String&gt; getArrcontactsofbean() { return arrcontactsofbean; } public void setArrcontactsofbean(ArrayList&lt;String&gt; arrcontactsofbean) { this.arrcontactsofbean = arrcontactsofbean; } } </code></pre> <p>I couldn't find out the exact problem here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload