mercredi 27 mai 2015

Having issues creating object from constructor in Android

I am a relatively new developer and have been working on a project for quite some time. I have been attempting to use an SQLite database that I can insert into and later view (However I will worry about viewing entries later). I thought I had everything working but when I try to make an object out of my constructor for my database class it wont allow me to use the parameters I need. I am also using Fragments if that matters as I feel that may be part of the issue. I would appreciate any help. I wont show my entire application unless you guys think it is needed but I will show all classes that are involved. Let me know if you guys can help.

First Class (DatabaseManager):

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

import java.sql.SQLException;

public class DatabaseManager{
    public Context mAppContext;
    private SQLiteDatabase mDataBase;
    private PhotoGalleryDatabase mHelper;
    private static final String DB_NAME = "vacations.sqlite";
    private static final int VERSION = 1;
    private static final String VACATION_TABLE = "vacation_entries";

    public static final String VACATION_ID = "vacationID";
    public static final String VACATION_DATE = "vacationDate";
    public static final String VACATION_LOCATION = "vacationLocation";
    public static final String VACATION__DESCRIPTION = "description";
    public static final String VACATION_URL = "picURL";
    public static final String LATITUDE = "latitude";
    public static final String LONGITUDE = "longitude";



    public DatabaseManager(Context appContext) {
        this.mAppContext = appContext;
        mHelper = new PhotoGalleryDatabase(mAppContext);
    }


    private static class PhotoGalleryDatabase extends SQLiteOpenHelper {


        public PhotoGalleryDatabase(Context context){
            super(context, DB_NAME, null, VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db){
            //Create the VacationEntries Table
            db.execSQL("CREATE TABLE " + VACATION_TABLE + " (" + VACATION_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    VACATION_DATE + " TEXT, " + VACATION_LOCATION +  " TEXT, " + VACATION__DESCRIPTION + " TEXT, " + LATITUDE + " REAL, " + LONGITUDE + " REAL" + VACATION_URL + " BLOB);");


        }

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

        }
    }





    public DatabaseManager open() throws SQLException{
        mDataBase = mHelper.getWritableDatabase();
        return this;
    }

    public long createEntry(String location, String date, String latitude, String longitude, String description) {
        ContentValues cv = new ContentValues();
        cv.put(VACATION_LOCATION, location);
        cv.put(VACATION_DATE, date);
        cv.put(VACATION__DESCRIPTION, description);
        cv.put(LATITUDE, latitude);
        cv.put(LONGITUDE, longitude);
        return mDataBase.insert(VACATION_TABLE, null, cv);

    }

    public void close(){
        mHelper.close();
    }

}



Second Class (Fragment):


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.example.student.photogallery2.R;

import java.sql.SQLException;

public class DatabaseFormFragment extends VisibleFragment{
    EditText description_editText, longitude_editText, latitude_editText, location_editText;
    EditText datePicker;
    Button submit_button;




    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.vacation_photo_form, container, false);

        description_editText = (EditText)v.findViewById(R.id.description_editText);
        longitude_editText = (EditText)v.findViewById(R.id.longitude_editText);
        latitude_editText = (EditText)v.findViewById(R.id.latitude_editText);
        datePicker = (EditText)v.findViewById(R.id.datePicker);
        location_editText = (EditText)v.findViewById(R.id.location_editText);

        submit_button= (Button)v.findViewById(R.id.submit_button);
        submit_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String location = location_editText.getText().toString();
                String date = datePicker.getText().toString();
                String latitude = latitude_editText.getText().toString();
                String longitude = longitude_editText.getText().toString();
                String description = description_editText.getText().toString();


                DatabaseManager entry = new DatabaseManager(DatabaseFormFragment.this);
                try {
                    entry.open();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                entry.createEntry(location, date, latitude, longitude, description);
                entry.close();




            }
        });

        return v;
    }


}

    enter code here

Aucun commentaire:

Enregistrer un commentaire