jeudi 10 décembre 2015

Data not getting saved in android sqlite

I am creating a database where there is input form and print format. When I insert the data I receive the data till "insert" method in helper class. But the same format will not come to "List" method. Can't figure of the issue.

The Main class

    else{
            ModelOne india = new ModelOne();
            // convet editable object from get text to string and then into integer
            india.id = Integer.parseInt(idno.getText().toString());     
            india.Item = itemsf.getSelectedItem().toString();
            india.Price = pricef.getText().toString();
            india.Weight = kgf.getText().toString();
            india.Place = place.getText().toString();

            db.addVegetables(india);
            list = db.getAllContacts();
            idno.setText(" ");
            pricef.setText(" ");
            kgf.setText(" ");
            place.setText(" ");

            Toast.makeText(getApplicationContext(),"Saved Successfully.", 
                    Toast.LENGTH_SHORT).show();         



        break;
        }

The Helper class

public List<ModelOne> getAllContacts() {
    List<ModelOne> contactListveg = new ArrayList<ModelOne>();  // Array list created to hold the data from database ( rows).
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_VEGETABLE;

    SQLiteDatabase db = this.getWritableDatabase(); //this is requesting database to allow to write some data to it.
    Cursor cursor = db.rawQuery(selectQuery, null);
    // cr
    // we can write here as db.close() (if required). Manipulating data with cursor is very effiient. 

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            ModelOne contact = new ModelOne();
            contact.setId(Integer.parseInt(cursor.getString(0)));
            contact.setItem(cursor.getString(1));
            contact.setPrice(cursor.getString(2));
            contact.setWeight(cursor.getString(3));
            contact.setPlace(cursor.getString(4));

            // Adding contact to list
            contactListveg.add(contact);
        } while (cursor.moveToNext());             // do this untill the while condition is satisfied. keep repeating the task
        //
    }

    // return contact list
    return contactListveg;
}

}

Aucun commentaire:

Enregistrer un commentaire