So, I was following tutorial and tried to make it work with my sql database. So, seems like there is no error but this red words(Gljive,they are bolded). Is there supposed to be class name or what? I putted basic word (gljive,means mushroom in my language).I didn't write all my columns beacuse i don't want to write them all if this ain't gonna work.. I just want to load database and later to fill specific column in specific spinners. Program shows that it cannot resolve symbol Gljive.
package com.example.shromid;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class DatabaseHelper extends SQLiteAssetHelper {
//DB info
private static final String DB_NAME = "gljive.db";
private static final int DB_VERSION = 1;
//Table GLJIVE
public static final String TABLE_GLJIVE = "gljive";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAZIV = "Naziv";
public static final String COLUMN_KBOJA = "Klobuk_Boja";
public static final String COLUMN_KOBLIK = "Klobuk_Oblik";
public static final String COLUMN_KTEKSTURA = "Klobuk_Tekstura";
public static final String COLUMN_LBOJA = "Listici_Boja";
public static final String COLUMN_SOBLIK = "Strucak_Oblik";
public static final String COLUMN_SDNO = "Strucak_Dno";
public static final String COLUMN_MBOJA = "Meso_Boja";
public static final String COLUMN_MMIRIS = "Meso_Miris";
public static final String COLUMN_STANISTE = "Staniste";
public static final String COLUMN_SGRUPA = "Staniste_Grupa";
public static final String COLUMN_UPOTREBLJIVOST = "Upotrebljivost";
private static final String orderBy = DatabaseHelper.COLUMN_NAZIV + " ASC ";
private Context mContext;
private SQLiteDatabase mDB;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
mContext = context;
setForcedUpgradeVersion();
}
public **Gljive** getGljiveById(int id) {
**Gljive** gljive = null;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(
DatabaseHelper.TABLE_GLJIVE, null, DatabaseHelper.COLUMN_ID + " = 1 ",
new String[]{String.valueOf(1)}, null, null, orderBy);
if (cursor != null) {
cursor.moveToFirst();
gljive = cursorToGljive(cursor);
cursor.close();
}
return gljive;
}
public **Gljive** getGljiveByNaziv(String Naziv) {
**Gljive** gljive = null;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(
DatabaseHelper.TABLE_GLJIVE, null, DatabaseHelper.COLUMN_NAZIV + " 2",
new String[]{name}, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
gljive = cursorToGljive(cursor);
cursor.close();
}
return gljive;
}
public List<**Gljive**> getGljiveByKBoja(String Klobuk_Boja) {
List<**Gljive**> gljive = new ArrayList<Gljive>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(
DatabaseHelper.TABLE_GLJIVE, null, DatabaseHelper.COLUMN_KBOJA + " 3 ",
new String[]{COLUMN_KBOJA}, null, null, orderBy);
if (cursor != null) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Gljive gljive = cursorToGljive(cursor);
gljive.add(gljive);
cursor.moveToNext();
}
cursor.close();
}
return gljive;
}
private **Gljive** cursorToGljive(Cursor cursor){
**Gljive** gljive= new Gljive();
return gljive;
}
}
public Gljive getGljiveById(int id) {....}
Is saying define a public method called getGljiveById
, which returns a Gljive object.
To have a Gljive object you would need a class named Gljive
Is there supposed to be class name or what?
Yes, so create a file named Gljive.java
and then have something like the following :-
public class Gljive {
private long mId;
private String mNaziv' // etc for all the properties (columns)
// Constructor
public Gljive(String naziv, ) {
this.mNaziv = naziv
}
// Setters/getters
public long getId() {
return this.mId;
}
public void setId(long id) {
this.mId = id;
}
public String getNaziv() {
return this.Naziv;
}
.... etc
}
Using the above (shortened) Gljive class you could elsewhere (e.g. in your Database Helper):-
//Create(construct) a Gljive instance
Gljive my_first_gljive = new Gljive("Button"); // Creates an instance using the constructor
my_first_gljive.setId(100); // Sets the mId for this instance to 100
String gljive_name_to_print = my_first_gljive.getNaziv();
Much of your code would need to then corectly accommodate/utilise the Gljive objects, for example cursorToGljive
could not be :-
private Gljive cursorToGljive(Cursor cursor){
Gljive gljive= new Gljive();
return gljive;
}
It would be along the lines of (based upon the shortened Gljive above):-
private Gljive cursorToGljive(Cursor cursor){
Gljive gljive= new Gljive(
cursor.getString(cursor.getColumnIndex(COLUMN_NAZIV)
);
gljive.setId(cursor.getColumnIndex(COLUMN_ID)); //Note(1)
return gljive;
}
Notes
using the above cursorToGljive
method and correcting some errors the other methods in the DatabaseHelper could be (see comments) :-
public Gljive getGljiveById(int id) {
Gljive gljive = null;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(
TABLE_GLJIVE, null, COLUMN_ID + "=?", //<<<< =?
new String[]{String.valueOf(id)}, //<<<< uses the passed id
null, null, orderBy);
if (cursor.moveToFirst()) { //<<<<< cursor will not be null
gljive = cursorToGljive(cursor);
cursor.close();
}
return gljive;
}
public Gljive getGljiveByNaziv(String Naziv) {
Gljive gljive = null;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(
TABLE_GLJIVE, null, COLUMN_NAZIV + "=?", //<<<< =?
new String[]{Naziv}, //<<<< use passed Naziv as argument
null, null, null);
if (cursor.moveToFirst()) { //<<<< cursor never null
gljive = cursorToGljive(cursor);
cursor.close();
}
return gljive;
}
public List<Gljive> getGljiveByKBoja(String Klobuk_Boja) {
List<Gljive> gljive = new ArrayList<>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(
TABLE_GLJIVE, null, COLUMN_KBOJA + "=?", //<<<< =?
new String[]{Klobuk_Boja}, //<<<< use passed value as argument
null, null, orderBy);
while (cursor.moveToNext()) { // compact loop removed null check
gljive.add(cursorToGljive(cursor)) // add Gljive object to List
}
cursor.close();
return gljive;
}
Note! This is in-principle code, it has not been tested so may have errors. Furthemore it is a shortened version just basically catering for the Naziv column. Gljive.java should be amended to cater for all the members (properties), likewise/accordingly cursorToGljive
should be amended.