I am fetching data from sqlite and displaying in a list view. All the data in sqlite is stored as String
. For example,
ImageName UploadStatus
test1.jpg 1
test2.jpg 2
I am using cursor to fetch the data as follows:
cursor = sqLiteDatabase.rawQuery("SELECT * from activityRes", null);
ImageName_Array.clear();
UploadStatus_Array.clear();
if (cursor != null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
ImageName_Array.add(cursor.getString(cursor.getColumnIndex("ImageName")));
UploadStatus_Array.add(cursor.getString(cursor.getColumnIndex("UploadStatus")));
// if (cursor.getString(cursor.getColumnIndex("UploadStatus")) == "1") {
// UploadStatus_Array.add("Y");
// } else if (cursor.getString(cursor.getColumnIndex("UploadStatus")) == "2") {
// UploadStatus_Array.add("N");
// }
} while (cursor.moveToNext());
}
}
The UploadStatus_Array.add gets me the data and displays it as 1 and 2. However, I want to change the display as Y
and N
. I tried with the commented codes...but it is throwing the entire table out.
It's better to execute a query that will return the results as you want them and just add them to the lists.
All you have to do is use a CASE
expression in the SELECT
statement and you will have the column UploadStatus
populated with Y
s and N
s:
String sql = "SELECT ImageName, CASE UploadStatus WHEN '1' THEN 'Y' WHEN '2' THEN 'N' END AS UploadStatus FROM activityRes"
cursor = sqLiteDatabase.rawQuery(sql, null);
ImageName_Array.clear();
UploadStatus_Array.clear();
while (cursor.moveToNext()) {
ImageName_Array.add(cursor.getString(cursor.getColumnIndex("ImageName")));
UploadStatus_Array.add(cursor.getString(cursor.getColumnIndex("UploadStatus")));
}
There is no need to check if cursor
is null
. The result of rawQuery()
is never null
.
Also there is no need to check if cursor.getCount()
is greater than 0
because while (cursor.moveToNext())
is enough.