(Android)在尝试使用数据库信息填充微调器时,我在代码中做错了什么

时间:2015-03-17 03:40:22

标签: android database spinner

我让数据显示在一个微调器中,它包含在我的main中创建的食物对象(名称,卡路里),然后使这些对象(现在只是名称)显示在一个旋转器中活动。这项活动实际上将成为更大计划的一部分,最终我希望能够让用户能够“计算”#34;他们吃掉的卡路里总量加起来所有选择的食物。现在,我只是希望能够让食物出现,这样我才能进一步前进。但是,每当我去运行它,我的应用程序只是崩溃没有错误消息说明问题。如果有人知道我需要做什么,请帮忙。我的代码如下:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.List;


public class  MainActivity extends ActionBarActivity implements
    OnItemSelectedListener {


    MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1);

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Foods food1 = new Foods("Apple", "80");
        Foods food2 = new Foods("Bagel", "200");
        Foods food3 = new Foods("Biscuit", "65" );
        Foods food4 = new Foods("Banana", "105");
        Foods food5 = new Foods("Beef Roast", "205");
        Foods food6 = new Foods("Corn", "60");
        Foods food7 = new Foods("Cereal", "120");
        Foods food8 = new Foods("Chicken", "240");
        Foods food9 = new Foods("Eggs", "105");
        Foods food10 = new Foods("Cabbage", "30");
        Foods food11 = new Foods("Oatmeal", "160");
        Foods food12 = new Foods("Pancake", "60");
        Foods food13 = new Foods("Pears", "100");
        Foods food14 = new Foods("Pizza", "290");
        Foods food15 = new Foods("Ice Cream", "270");
        Foods food16 = new Foods("Pork Chop", "335");
        Foods food17 = new Foods("Ham", "250");
        Foods food18 = new Foods("Ribs", "270");
        Foods food19 = new Foods("Popcorn", "55");
        Foods food20 = new Foods("Baked Potato", "220");
        Foods food21 = new Foods("Rice", "225");
        Foods food22 = new Foods("Salad", "85");
        Foods food23 = new Foods("Spaghetti", "360");
        Foods food24 = new Foods("Bread", "65");
        Foods food25 = new Foods("Fish", "175");


        dbHandler.addFood(food1);
        dbHandler.addFood(food2);
        dbHandler.addFood(food3);
        dbHandler.addFood(food4);
        dbHandler.addFood(food5);
        dbHandler.addFood(food6);
        dbHandler.addFood(food7);
        dbHandler.addFood(food8);
        dbHandler.addFood(food9);
        dbHandler.addFood(food10);
        dbHandler.addFood(food11);
        dbHandler.addFood(food12);
        dbHandler.addFood(food13);
        dbHandler.addFood(food14);
        dbHandler.addFood(food15);
        dbHandler.addFood(food16);
        dbHandler.addFood(food17);
        dbHandler.addFood(food18);
        dbHandler.addFood(food19);
        dbHandler.addFood(food20);
        dbHandler.addFood(food21);
        dbHandler.addFood(food22);
        dbHandler.addFood(food23);
        dbHandler.addFood(food24);
        dbHandler.addFood(food25);

        spinner.setOnItemSelectedListener(this);
        loadSpinnerData();
    }

   private void loadSpinnerData()
    {
        List<String> foodnames = dbHandler.getFoodNames();

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, foodnames);

        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(dataAdapter);



    }




    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        String food = parent.getItemAtPosition(position).toString();

        // Showing selected spinner item
        Toast.makeText(parent.getContext(), "You selected: " + food,
                Toast.LENGTH_LONG).show();


    }
}




public class Foods {
    private int _id;

    public int get_id() {
        return _id;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    private String name;
    private String calories;

    public Foods(){
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCalories() {
        return calories;
    }

    public void setCalories(String calories) {
        this.calories = calories;
    }

    public Foods(String foodname, String foodcalories){
        this.name = foodname;
        this.calories = foodcalories;

    }
}

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;
import java.util.ArrayList;
import java.util.List;

public class MyDBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "BroncoWellness.db";
    public static final String TABLE_FOODS = "food";
    public static final String COLUMN_FOOD_ID = "_id";
    public static final String COLUMN_FOOD_NAME = "foodname";
    public static final String COLUMN_FOOD_CALORIES = "foodcalories";


    @Override
    public void onCreate(SQLiteDatabase db) {

        String Foodquery = "CREATE TABLE " + TABLE_FOODS + "(" +
                COLUMN_FOOD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_FOOD_NAME + " TEXT " +
                COLUMN_FOOD_CALORIES + " TEXT " +
                ");";

        db.execSQL(Foodquery);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_FOODS);
        onCreate(db);

    }

    public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context,DATABASE_NAME ,factory, DATABASE_VERSION);}




    public void addFood(Foods food){
        ContentValues values = new ContentValues();
        values.put(COLUMN_FOOD_NAME, food.getName());
        values.put(COLUMN_FOOD_CALORIES, food.getCalories());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_FOODS,null,values);
        db.close();
    }


public List<String> getFoodNames(){
    List<String> foodnames = new ArrayList<String>();
    String selectQuery = "SELECT  * FROM " + TABLE_FOODS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            foodnames.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();

    return foodnames;
}

}

2 个答案:

答案 0 :(得分:0)

在您的代码中,您正在onCreate()方法之外初始化微调器。它应该是这样的 -

Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = (Spinner) findViewById(R.id.spinner);

//your code

}

这将解决您的问题。

答案 1 :(得分:0)

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // **Spinner element here**
    spinner = (Spinner) findViewById(R.id.spinner);