我正在创建一个有输入表单和打印格式的数据库。当我插入数据时,我收到数据直到帮助类中的“插入”方法。但是相同的格式不会出现“List”方法。无法解决问题。
主要课程
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;
}
助手类
public void addVegetables(ModelOne model) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, model.getId());
values.put(KEY_VEGITEMS, model.getItem());
values.put(KEY_PRICE, model.getPrice());
values.put(KEY_WEIGHT, model.getWeight());
values.put(KEY_PLACE, model.getPlace());
db.insert(TABLE_VEGETABLE, null, values);
db.close();
}
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;
}
}