vendredi 17 juin 2016

Can't read SQLite stored values

In my app I have a FloatingButton,and when it's clicked the user can insert his favourite movie in a Dialog. The strings are stored,as I can read them in the debugger. However,when the user clicks the add button in the alert dialog,nothing is shown in the MainActivity's recycler view. Which means I the string values are not read.

Here is my MainActivity's code.

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

    fab = (FloatingActionButton) findViewById(R.id.floatingButton);
    layout = new LinearLayoutManager(this);
    myList.setLayoutManager(layout);
    myAdapter = new MyListAdapter(myarr);
    myList.setAdapter(myAdapter);

    fetchDataFromDB();

    listener=new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {
            showEntryForm();
        }
    };
    fab.setOnClickListener(listener);
}

public void showEntryForm()
{
    final Dialog mydialog = new Dialog(this);

    mydialog.setContentView(R.layout.dialog_entry_layout);
    mydialog.show();

    movieTitle = (EditText) mydialog.findViewById(R.id.titleText);
    descriptionText = (EditText) mydialog.findViewById(R.id.descText);


    Button add = (Button) mydialog.findViewById(R.id.add);
    Button cancel = (Button) mydialog.findViewById(R.id.cancel);



    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mydialog.dismiss();
        }
    });

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myarr.clear();
            myAdapter.notifyDataSetChanged();
            saveToDB();
            mydialog.dismiss();
        }
    });




}

private void saveToDB(){


    MyMovie myMovie = new MyMovie();

    myMovie.setMovieTitle(movieTitle.getText().toString());
    myMovie.setContent(descriptionText.getText().toString());

    dba.addMovies(myMovie);

    dba.close();



    movieTitle.setText("");
    descriptionText.setText("");

    Toast.makeText(getApplicationContext(),"Movie Saved",Toast.LENGTH_SHORT).show();
}

private void fetchDataFromDB() {

    dba = new DatabaseHandler(getApplicationContext());

    ArrayList<MyMovie> moviesFromDB = dba.getMovieDetails();

    for(int i=0; i<moviesFromDB.size();i++){

            String movietile = moviesFromDB.get(i).getMovieTitle();
            String desc = moviesFromDB.get(i).getContent();

            MyMovie f = new MyMovie();
            f.setMovieTitle(movietile);
            f.setContent(desc);

            myarr.add(f);

    }
    dba.close();
}
}

As you can see I have one method called saveToDB and fetchDataFromDB(). I want to do both operations in the same activity.

And this is my DatabaseHandler class which extends the SQLiteOpenHelper.

 @Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_MOVIES_TABLE = "CREATE TABLE " + Constants.TABLE_NAME +
            "(" + Constants.KEY_ID + " INTEGER PRIMARY KEY, " + Constants.MOVIE_TITLE +
            " TEXT, "  +  Constants.CONTENT + " TEXT);";
    db.execSQL(CREATE_MOVIES_TABLE);
}

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

    onCreate(db);
}

//add content to table
public void addMovies(MyMovie folder) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(Constants.MOVIE_TITLE, folder.getMovieTitle());

    db.insert(Constants.TABLE_NAME, null, values);

    db.close();

    Log.d("Theo","heeeey!data saved");
}

//get all folders
public ArrayList<MyMovie> getMovieDetails() {



    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(Constants.TABLE_NAME, new String[]{
                    Constants.KEY_ID, Constants.MOVIE_TITLE,Constants.CONTENT}, null, null, null, null, Constants.KEY_ID + " DESC");


    if (cursor.moveToFirst()) {
        do {

            MyMovie myMovie = new MyMovie();
            myMovie.setMovieTitle(cursor.getString(cursor.getColumnIndex(Constants.MOVIE_TITLE)));

            myMovie.setContent(cursor.getString(cursor.getColumnIndex(Constants.CONTENT)));
            myMovie.setItemId(cursor.getInt(cursor.getColumnIndex(Constants.KEY_ID)));



            moviesList.add(myMovie);
        }while(cursor.moveToNext());


    }
    return moviesList;
}

public void deleteFolder(int id){
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(Constants.TABLE_NAME,Constants.KEY_ID + " = ? ",new String[]  {String.valueOf(id)});
    db.close();
}
}

Any ideas?

Thanks,

Theo.

Aucun commentaire:

Enregistrer un commentaire