mercredi 30 septembre 2015

Android Studio - Retrieve Data From SQLite Database and insert into Spinner

I'm relatively new to Android Studio so sorry if this is a quick fix

I'm trying to retrieve a column from my database put all records from that column into an arrayList to that then the data in the list can be used to populate a spinner

The code I have so far is as follows;

try {

            //String sql = "INSERT INTO Inventory(NAME,AMOUNT,PRICE,AMOUNTLEFT,WEIGHT) VALUES('Apple','30','1.99','28','1.01')";
            //db.execSQL(sql);
            Cursor c = db.rawQuery("SELECT NAME FROM Inventory", null);
            if (c.getCount() == 0)
            {
                showMessage("Welcome!", "Please Add Items To Your Inventory");
            }
            List InventoryList = new ArrayList();
            Integer i=0;
            if(c.moveToFirst())
            {
                do {
                    InventoryList.add(i, c.getColumnIndex("1"));
                    i+=1;
                }while(c.moveToNext());
            }
            c.close();
            db.close();
            flav1InvSpinner = (Spinner)findViewById(R.id.Combo_InvChoice1);
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, InventoryList);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            flav1InvSpinner.setAdapter(dataAdapter);




        }
        catch(Exception e)
        {
            showMessage("Error", "Cannot Connect To Database \n" + e.getMessage() );
        }

However the combo box is only displaying -1 when the emulator is running I'd expect it to display 'Apple'

Any suggestions as to what I've done wrong / missed

Thanks

Aucun commentaire:

Enregistrer un commentaire