vendredi 24 avril 2015

Android app - sqlite database issue on close

I am creating an Android app in Eclipse, and the section I am having issues with is where I am trying to populate a ListView with data from a SQLite database.

When I run the app and try to open this particular fragment, the app crashes. When I comment out the line newDB.close() on the 4th last line of BreakfastMenuFragment.java, it will load but the listview is not populated.

I have tried to find the issue but no luck, any help will be appreciated.

BreakfastMenuFragment.java

package info.androidhive.slidingmenu;

import java.io.IOException;
import java.util.ArrayList;
import android.app.Fragment;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class BreakfastMenuFragment extends Fragment {

    public BreakfastMenuFragment(){}

    private ArrayList<String> results = new ArrayList<String>();
    private String tableName = DBHelper.tableName;
    private SQLiteDatabase newDB;
    /** Called when the activity is first created. 
     * @return */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View rootView = inflater.inflate(R.layout.menu_layout, container, false);
        ListView listView = (ListView) rootView.findViewById(R.id.menuList);

        try {
            openAndQueryDatabase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        displayResultList(listView);        
return rootView;
    }
    private void displayResultList(ListView listView) {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity().getApplicationContext(),
                android.R.layout.simple_list_item_1, results);

        listView.setAdapter(adapter);
    }
    private void openAndQueryDatabase() throws IOException {
        try {
            DBHelper dbHelper = new DBHelper(this.getActivity().getApplicationContext());
            newDB = dbHelper.getWritableDatabase();
            Cursor c = newDB.rawQuery("SELECT itemdesc, itemprice FROM " + tableName +  " WHERE menu LIKE 'Breakfast'", null);

            if (c != null ) {
                if  (c.moveToFirst()) {
                    do {
                        String itemDesc = c.getString(c.getColumnIndex("itemdesc"));
                        double itemPrice = c.getDouble(c.getColumnIndex("itemprice"));
                        results.add("" + itemDesc + " - Price: £"+ itemPrice +"");
                    }while (c.moveToNext());
                } 
            }           
        } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        } finally {
            if (newDB != null) 
                newDB.execSQL("DELETE FROM " + tableName);
                newDB.close();
        }
    }
}

DBHelper.java

package info.androidhive.slidingmenu;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper{

    public SQLiteDatabase DB;
    public String DBPath;
    public static String DBName = "orchard.sqlite";
    public static final int version = '1';
    public static Context currentContext;
    public static String tableName = "menuitems";


    public DBHelper(Context context) throws IOException {
        super(context, DBName, null, version);
        currentContext = context;
        DBPath = "/data/data/" + context.getPackageName() + "/databases";
        createDatabase();
    }

    private void createDatabase() throws IOException {
        boolean dbExists = checkDbExists();

        if (dbExists) {
            // do nothing
        } else {
                    // Open your local db as the input stream
                    String dbname = "orchard.sqlite";
                    InputStream myInput = currentContext.getAssets().open(dbname);
                    // Path to the just created empty db
                    String outFileName = currentContext.getFilesDir().getPath() + dbname;
                    // Open the empty db as the output stream
                    OutputStream myOutput = new FileOutputStream(outFileName);
                    // transfer bytes from the inputfile to the outputfile
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = myInput.read(buffer)) > 0) {
                        myOutput.write(buffer, 0, length);
                    }
                    // Close the streams
                    myOutput.flush();
                    myOutput.close();
                    myInput.close();

                }   
            }       

    private boolean checkDbExists() {
        SQLiteDatabase checkDB = null;

        try {
            String myPath = DBPath + DBName;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY);

        } catch (SQLiteException e) {

            // database does't exist yet.

        }

        if (checkDB != null) {

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }
}

Aucun commentaire:

Enregistrer un commentaire