从SQLITE数据库填充微调器的问题

时间:2016-01-04 11:47:55

标签: android android-sqlite android-spinner

我是Android开发的新手,我在使用微调器加载数据库SQLITE值时遇到了问题。我总是得到强制关闭

这是我的代码

Database Handler.java

public void onCreate(SQLiteDatabase db) {
String creation_query5 = "CREATE TABLE food (name text primary key, calorie integer)";
db.execSQL(creation_query5);
}

 public ArrayList<String> getAllFood(){

    ArrayList<String> list=new ArrayList<String>();
    // Open the database for reading
    SQLiteDatabase db = this.getReadableDatabase();
    // Start the transaction.
    db.beginTransaction();


    try
    {

        String selectQuery = "SELECT * FROM food";
        Cursor cursor = db.rawQuery(selectQuery, null);
        if(cursor.getCount() >0)

        {
            while (cursor.moveToNext()) {
                // Add province name to arraylist
                String name= cursor.getString(cursor.getColumnIndex("name"));
                list.add(name);

            }


        }
        db.setTransactionSuccessful();

    }
    catch (SQLiteException e)
    {
        e.printStackTrace();

    }
    finally
    {
        db.endTransaction();
        // End the transaction.
        db.close();

        // Close database
    }
    return list;


}


public void insertFood(String pname, int cal) {

    // Open the database for writing
    SQLiteDatabase db = this.getWritableDatabase();
    // Start the transaction.
    db.beginTransaction();
    ContentValues values;


    try

    {
        values = new ContentValues();
        values.put("name", pname);
        values.put("calorie",cal);
        // Insert Row
        db.insert("food", null, values);
        // Insert into database successfully.
        db.setTransactionSuccessful();


    }
    catch (Exception e)
    {
        e.printStackTrace();

    }

    finally

    {
        db.endTransaction();
        // End the transaction.
        db.close();
        // Close database
    }
}

MainActivity.java (在创建方法中)

DatabaseHandler  db = new DatabaseHandler(getApplicationContext());
spinner=(Spinner) findViewById(R.id.spinner);
    tv=(TextView) findViewById(R.id.textView7);
    db.insertFood("potato",50);
    db.insertFood("tomato",1);
    ArrayList<String> list=db.getAllFood();
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, R.id.text, list);
    spinner.setAdapter(adapter);

请您查看我的代码并指导我哪里出错,任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

重写您的代码,如下所示

DatabaseHandler  db = new DatabaseHandler(TestActivity.this);
        db.insertFood("potato",50);
        db.insertFood("tomato",1);
        ArrayList<String> list=db.getAllFood();
        if(list != null)
            Log.e("LOG", list.toString());
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(TestActivity.this,
                android.R.layout.simple_spinner_dropdown_item, android.R.id.text1, list);
        spinner.setAdapter(adapter);

答案 1 :(得分:0)

我为你创建了数据库类,试试这个:

package com.seasiainfotech.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;

/**
 * Created by jyomin on 1/4/2016.
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    //database name and version
    public static String DATABASENAME = "YourApp.sql"; // Database Name
    //first version was 33 which was released to client
    public static final int DATABASE_VERSION = 1;
    Context mContext;

    private static final String FOOD_TABLE = "Food";
    private static final String CALOIRE = "Calorie";
    private static final String NAME = "NAME";
    public static String CREATE_TABLE_FOOD = "CREATE TABLE "
            + FOOD_TABLE + "(" + NAME + " TEXT,"
            + CALOIRE + " TEXT" + ")";


    public DatabaseHelper(Context context) {
        super(context, DATABASENAME, null, DATABASE_VERSION);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_FOOD);
    }


    public ArrayList<String> getAllFoods() {
        ArrayList<String> list = new ArrayList<String>();
        // Open the database for reading
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor mCursor = db.rawQuery("select * from " + FOOD_TABLE, null);
        if (mCursor.getCount() > 0) {
            while (mCursor.moveToNext()) {
                list.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.NAME)));
            }
        }
        return list;
    }


    public void insertFood(String name,String catg){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(NAME, name);
        contentValues.put(CALOIRE, catg);
        db.insert(FOOD_TABLE, null, contentValues);
        db.close();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_FOOD);
        onCreate(db);
    }
}


and in your activity class write this:
      DatabaseHelper mDb=new DatabaseHelper(yourActivity.this);
        mDb.insertfood("maggi","veg");
    //to get the list from db
          yourlist=mDb.getAllFoods();