I have an error when I try to use a ListAdapter to read my database in a Fragment Class. I feel like this isn't a hard error to resolve and there is not much code to check but I don't know why I get this error : "java.lang.IllegalArgumentException: column 'theme' does not exist". My function databaseView() in my Fragment class is supposed to read my database, so I made a request to get all data so I should get all the columns.
I can even add a new entry (from another Fragment Class) in my database with my content provider, I checked with "sqliteviewer" on chrome and it works well.
I create my database with this request :
"create table "+ STOCK_TABLE + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, THEME TEXT, QUESTION TEXT, REPONSE TEXT, DIFFICULTE FLOAT)"
Here is my Fragment class :
public class ViewCardEditor extends Fragment {
StockCard stock;
ViewGroup container;
ListView listView;
SimpleCursorAdapter cursorAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup c,
Bundle savedInstanceState) {
container = c;
View view = inflater.inflate(R.layout.fragment_view_card_editor, container, false);
// Inflate the layout for this fragment
if (container != null) {
container.removeAllViews();
}
listView = (ListView) view.findViewById(R.id.listView);
stock = new StockCard(c.getContext());
databaseView();
return view;
}
public void databaseView()
{
ArrayList<String> list = new ArrayList<>();
Cursor cursor = stock.getData();
if(cursor.getCount() == 0)
{
Toast.makeText(container.getContext(), "Aucunes cartes en stock !!", Toast.LENGTH_LONG).show();
}
else
{
while(cursor.moveToNext())
{
list.add(cursor.getString(0));
list.add(cursor.getString(1));
list.add(cursor.getString(2));
list.add(cursor.getString(3));
list.add(cursor.getString(4));
//HERE is where I get my exception :
ListAdapter adapter = new SimpleCursorAdapter(container.getContext(),
R.layout.card_stock,
cursor,
new String[]{stock._ID,
stock.THEME,
stock.QUESTION,
stock.REPONSE,
stock.DIFFICULTE},
new int[]{R.id.idList, R.id.themeList, R.id.questionList, R.id.reponseList, R.id.difficultList}, 0);
listView.setAdapter(adapter);
}
}
}
}
And my SQLiteOpenHelper class
public class StockCard extends SQLiteOpenHelper {
public static final String STOCK_NAME ="StockCard.db";
public static final String STOCK_TABLE = "Carte_table";
private static int VERSION = 1;
public static final String _ID = "_id";
public static final String THEME = "theme";
public static final String QUESTION = "question";
public static final String REPONSE = "reponse";
public static final String DIFFICULTE = "difficulte";
private static StockCard instance;
//Constructeur pour AddCardEditor (Fragment de CardEditor) et ses fonctions
public StockCard(Context context) {
super(context, STOCK_NAME, null, VERSION);
//SQLiteDatabase db = this.getWritableDatabase();
}
public static StockCard getInstance(Context context) {
if( instance == null ){
instance = new StockCard(context);
}
return instance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+ STOCK_TABLE + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, THEME TEXT, QUESTION TEXT, REPONSE TEXT, DIFFICULTE FLOAT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion > oldVersion)
{
db.execSQL("DROP TABLE IF EXISTS "+STOCK_TABLE);
onCreate(db);
}
}
public Cursor getData()
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("select * from "+STOCK_TABLE, null);
return data;
}
}
Then my ContentProvider :
public class CardContentProvider extends ContentProvider {
private StockCard stock;
public static String authority = "com.example.jean.cartememoire.CardContentProvider";
private static String path ="Carte_table";
public static final String _ID = "_id";
public static final String THEME = "theme";
public static final String QUESTION = "question";
public static final String REPONSE = "reponse";
public static final String DIFFICULTE = "difficulte"; //# = un chiffre
public static final String STOCK_TABLE = "Carte_table";
private static final int ID_STOCK_TABLE = 1;
private static final int ID_THEME = 2;
private static final int ID_QUESTION = 3;
private static final int ID_REPONSE = 4;
private static final int ID_DIFFICULTE = 5;
private static final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
matcher.addURI(authority, STOCK_TABLE, ID_STOCK_TABLE);
matcher.addURI(authority, THEME, ID_THEME);
matcher.addURI(authority, QUESTION, ID_QUESTION);
matcher.addURI(authority, REPONSE, ID_REPONSE);
matcher.addURI(authority, DIFFICULTE, ID_DIFFICULTE);
}
@Override
public boolean onCreate() {
stock = StockCard.getInstance(getContext());
return true;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = stock.getReadableDatabase();
int code = matcher.match(uri);
long id;
Cursor cursor;
switch (code)
{
case ID_STOCK_TABLE:
cursor = db.query(STOCK_TABLE, projection, selection,
selectionArgs, null, null, sortOrder);
break;
default:
Log.d("Uri provider =", uri.toString());
throw new UnsupportedOperationException("Pas encore implémenté");
}
return cursor;
}
@Nullable
@Override
public String getType(Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = stock.getWritableDatabase();
int code = matcher.match(uri);
long id;
Uri.Builder builder = new Uri.Builder();
switch(code)
{
case ID_STOCK_TABLE:
System.out.println(values.toString());
id = db.insert(STOCK_TABLE, null, values);
builder.appendPath(STOCK_TABLE);
break;
default:
throw new UnsupportedOperationException("Pas encore implémenté");
}
builder.authority(authority);
builder = ContentUris.appendId(builder, id);
return builder.build();
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
}
Thank you for help, and sorry if bad english.
I think your problem is case sensitivity
https://code.google.com/p/android/issues/detail?id=42636
You declare your table like this:
db.execSQL("create table "+ STOCK_TABLE + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, THEME TEXT, QUESTION TEXT, REPONSE TEXT, DIFFICULTE FLOAT)");
i.e. Column name = THEME
but then you attempt to access it using:
public static final String THEME = "theme";
i.e. Column name = theme
Within the CursorAdapter
. Change one to match the other and see if it works. For example to change CardContentProvider
would be the easiest:
public static final String THEME = "THEME";