Hi again stackoverflow community...
I'm trying to figure out how to load information from a table in my SQLite database into an AutoCompleteTextView. I've searched through hundreds of posts and websites and I'm at wits end now; I desperately need help. All of the posts that seemed helpful have different methods of creating tables to the one I have used, and so the syntax for using the simple cursor adapter isn't making any sense to me.
My program allows a user to enter food items into a database. From a different section of this program, I want the users to be able to add these food items into their own little food storage - I have already created the XML file for this. However, I don't have a clue how to return the FoodItems 'Name' column into my AutoCompleteTextView - I've read something about an Array Adapter but quite frankly I have no idea what I'm doing.
I've already created the table and there is data in the database. I'll only show the code relevant to my problem so as not to overload you. The code used for writing the database is:
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "ChangeMe.db";
public static final String TABLE_CREATE_FOODS = "CREATE TABLE IF NOT EXISTS FoodItems (Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Name TEXT, Category TEXT, Calorie NUMERIC, Fat NUMERIC, Carbohydrate NUMERIC)";
Then, I have a method to set these food items:
public long SetFoodItems(String name, String category, Float calorie, Float fat, Float carbohydrate) {
String query = "SELECT * FROM FoodItems";
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("Name", name);
values.put("Category", category);
values.put("Calorie", calorie);
values.put("Fat", fat);
values.put("Carbohydrate", carbohydrate);
long id = db.insert("FoodItems", null, values);
return id;
}
I've also made a cursor to obtain all of the records from the FoodItems table:
public Cursor ObtainFoodItems()
{
SQLiteDatabase db = this.getReadableDatabase();
String Query = "SELECT Name FROM FoodItems";
Cursor CR = db.rawQuery(Query, null);
return CR;
}
So, can anyone tell me how I return this cursor into an autoCompleteTextView? Please bear in mind that I am a complete newbie to Java (and coding in general), so simplified suggestions would be highly appreciated. I'm just a final year student trying to meet a deadline.
Thank you!
Aucun commentaire:
Enregistrer un commentaire