Home » Java » displaying image saved in sql database

displaying image saved in sql database

Posted by: admin October 26, 2017 Leave a comment

Questions:

Hi guy so i have a list of property that has a List of images.At the moment there are only 3 properties that are gotten from the server. For each of these property, it has {id, property image etc}. The first property has 3 images, second has 2 images and the fourth property has 4 images. I have already saved the images into the sql database, but when i want to get the images for each property using the property id only the last property images are returned and is shown instead of the images related to each property. Please i need help.

This is my Local database class:

public class AppLocalDatabase extends SQLiteOpenHelper {
private static final String ALL_LISTINGS =  "CREATE TABLE " +
PROPERTY_LISTINGS + " ("+ PROPERTY_CATEGORIES_ID +" TEXT, "+ PROPERTY_IMAGES +" TEXT, "
+ PROPERTY_TYPE +" TEXT, "
+ PROPERTY_SHORT_DESCRIPT +" TEXT, "  + PROPERTY_ID +" TEXT, " + PROPERTY_DISPLAYER +" TEXT, "+ PROPERTY_TITLE +" TEXT, "+ PROPERTY_PRICE +" TEXT,"+ PROPERTY_CATEGORIES +" TEXT );";
private static  final String AVALIABLE_PHOTO = "CREATE TABLE "+
IMAGE_LISTINGS + " ("+ PHOTO_ID +" INTEGER PRIMARY KEY, " + PROPERTY_ID +" TEXT, "+ PHOTO_URL +" TEXT );";
private static final String ALL_CATEGORY =  "CREATE TABLE " +
AVALIABLE_CATEGORY + " ("+ PROPERTY_CATEGORIES_ID +" TEXT, "
+ PROPERTY_CATEGORIES + " TEXT, "+ PROPERTY_CATEGORY_DESCRIPTION + " TEXT, "+ PROPERTY_TYPE +" TEXT );";
private static final String ALL_DESCRIPTION =  "CREATE TABLE " +
AVALIABLE_DESCRIPTION + " ("+ DESCRIPTION_ID + " INTEGER PRIMARY KEY, " + PROPERTY_CATEGORIES_ID +" TEXT, "+ PROPERTY_CATEGORY_DESCRIPTION + " TEXT );";
private static final String AVALIABLE_CATEGORY_TYPE ="cat_type_table" ;
private static final String TYPE_ID ="type_id" ;
private static final String ALL_TYPE =  "CREATE TABLE " +
AVALIABLE_CATEGORY_TYPE+ " ("+ TYPE_ID + " INTEGER PRIMARY KEY, " + PROPERTY_CATEGORIES_ID +" TEXT, "+ PROPERTY_TYPE + " TEXT );";
public AppLocalDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//CREATE A TABLE IN THE DATABASE
sqLiteDatabase.execSQL(ALL_LISTINGS);
sqLiteDatabase.execSQL(ALL_CATEGORY);
sqLiteDatabase.execSQL(ALL_DESCRIPTION);
sqLiteDatabase.execSQL(ALL_TYPE);
sqLiteDatabase.execSQL(AVALIABLE_PHOTO);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
//UPGRADE DATABASE BY DELETING EXISTING TABLE IF THERE IS ANY
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ PROPERTY_LISTINGS);
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ AVALIABLE_CATEGORY);
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ AVALIABLE_CATEGORY_TYPE);
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ AVALIABLE_DESCRIPTION);
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ IMAGE_LISTINGS);
//CREATE A NEW TABLE AFTER DELETING THE OLD ONE
onCreate(sqLiteDatabase);
}
public void insertListings (List<ListingItemModel> all_property, String categoryName){
SQLiteDatabase db = null;
ContentValues contentValues;
if (all_property != null &&(!all_property.isEmpty())){
db = this.getWritableDatabase();
for (ListingItemModel listingItemModel : all_property){
if (!listingExists(db)){
contentValues = new ContentValues();
contentValues.put(PROPERTY_CATEGORIES,categoryName);
contentValues.put(PROPERTY_CATEGORY_DESCRIPTION, String.valueOf(listingItemModel.getDescriptions()));
contentValues.put(PROPERTY_SHORT_DESCRIPT, listingItemModel.getAdditionalText());
contentValues.put(PROPERTY_CATEGORIES_ID, listingItemModel.getCategoreyId());
contentValues.put(PROPERTY_TITLE, listingItemModel.getTitle());
contentValues.put(PROPERTY_PRICE, listingItemModel.getPrice());
contentValues.put(PROPERTY_ID, listingItemModel.getId());
contentValues.put(PROPERTY_DISPLAYER,listingItemModel.getAuthorId());
db.insert(AppLocalDatabase.PROPERTY_LISTINGS, null, contentValues);
}
}
}
if (db != null)
db.close();
}
*/
public void insertListings (List<ListingItemModel> all_property){
SQLiteDatabase db = null;
ContentValues contentValues;
if (all_property != null &&(!all_property.isEmpty())){
db = this.getWritableDatabase();
for (ListingItemModel listingItemModel : all_property){
if (!listingExists(listingItemModel.getId(),db)){
contentValues = new ContentValues();
contentValues.put(PROPERTY_SHORT_DESCRIPT, listingItemModel.getAdditionalText());
contentValues.put(PROPERTY_CATEGORIES_ID, listingItemModel.getCategoreyId());
contentValues.put(PROPERTY_TITLE, listingItemModel.getTitle());
contentValues.put(PROPERTY_PRICE, listingItemModel.getPrice());
contentValues.put(PROPERTY_ID, listingItemModel.getId());
contentValues.put(PROPERTY_DISPLAYER,listingItemModel.getAuthorId());
//contentValues.put(PROPERTY_IMAGES, listingItemModel.getPhotos());
db.insert(AppLocalDatabase.PROPERTY_LISTINGS, null, contentValues);
deletePhoto();
List<String> allPhoto = listingItemModel.getPhotos();
String propertyId = listingItemModel.getId();
insertPhoto(allPhoto, propertyId);
}
}
}
/*if (db != null)
db.close();*/
}
public void insertPhoto(List<String> all_photo,String property_Id) {
SQLiteDatabase db = null;
ContentValues contentValues;
if (all_photo != null && (!all_photo.isEmpty())) {
db = this.getWritableDatabase();
for (String singlePhoto : all_photo) {
if (!photoExists(singlePhoto, db)) {
contentValues = new ContentValues();
contentValues.put(PROPERTY_ID,property_Id );
contentValues.put(PHOTO_URL, singlePhoto);
db.insert(AppLocalDatabase.IMAGE_LISTINGS, null, contentValues);
}
}
}Log.i("List Images", all_photo.toString());
Log.i("Image Count", String.valueOf(all_photo.size()));
/*
if (db != null)
db.close();*/
}
public void insertCategories (List<BuildingCategoryItem> cat_Description){
SQLiteDatabase db = null;
ContentValues contentValues;
if (cat_Description != null && (!cat_Description.isEmpty())){
db = this.getWritableDatabase();
for (BuildingCategoryItem categoryItem : cat_Description){
if (!categoriesExists(categoryItem.getId(), db)){
contentValues = new ContentValues();
contentValues.put(PROPERTY_CATEGORIES_ID, categoryItem.getId());
contentValues.put(PROPERTY_CATEGORIES, categoryItem.getName());
db.insert(AppLocalDatabase.AVALIABLE_CATEGORY, null, contentValues);
insertType(categoryItem.getId(), categoryItem.getTypes());
insertDescription(categoryItem.getId(), categoryItem.getAllowedDescriptions());
Log.i("CategoryInsert", String.valueOf(contentValues.size()));
}
}
}
if (db != null)
db.close();
}
private void insertType(String id, List<String> types) {
SQLiteDatabase db;
ContentValues contentValues;
if (id != null &&(!id.isEmpty()) && types != null && (!types.isEmpty())){
db = this.getWritableDatabase();
for (String strTypes : types){
contentValues = new ContentValues();
contentValues.put(PROPERTY_CATEGORIES_ID, id);
contentValues.put(PROPERTY_TYPE, strTypes);
db.insert(AppLocalDatabase.AVALIABLE_CATEGORY_TYPE, null, contentValues);
}
}
}
private void insertDescription(String id, List<String> cat_description) {
SQLiteDatabase db;
ContentValues contentValues;
if (id != null &&(!id.isEmpty()) && cat_description != null && (!cat_description.isEmpty())){
db = this.getWritableDatabase();
for (String strDescription : cat_description){
contentValues = new ContentValues();
contentValues.put(PROPERTY_CATEGORIES_ID, id);
contentValues.put(PROPERTY_CATEGORY_DESCRIPTION, strDescription);
db.insert(AppLocalDatabase.AVALIABLE_DESCRIPTION, null, contentValues);
}
}
}
private String[] listing = {PROPERTY_CATEGORIES_ID,
PROPERTY_IMAGES,PROPERTY_TYPE, PROPERTY_SHORT_DESCRIPT,PROPERTY_ID ,PROPERTY_DISPLAYER, PROPERTY_TITLE, PROPERTY_PRICE,PROPERTY_CATEGORIES};
public List<ListingItemModel> getAllListing(String propertyId){
List<ListingItemModel> listingItemModelList = new ArrayList<>();
ListingItemModel listingItemModel;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
cursor = db.query(PROPERTY_LISTINGS,listing, null,null,null,null,null);
if (cursor != null && (cursor.getCount()>0)){
cursor.moveToFirst();
do{
listingItemModel = new ListingItemModel();
listingItemModel.setCategoreyId(cursor.getString(0));
listingItemModel.setPhotos(getPhotos(propertyId));
listingItemModel.setTypeId(cursor.getLong(2));
listingItemModel.setAdditionalText(cursor.getString(3));
listingItemModel.setId(cursor.getString(4));
listingItemModel.setAuthorId(cursor.getString(5));
listingItemModel.setTitle(cursor.getString(6));
listingItemModel.setPrice(Long.valueOf(cursor.getString(7)));
listingItemModel.setCategoryName(cursor.getString(8));
listingItemModelList.add(listingItemModel);
}while (cursor.moveToNext());
}assert cursor != null;
cursor.close();
db.close();
return listingItemModelList;
}
private String[] photos ={PROPERTY_ID, PHOTO_URL};
//private String[] photos ={PROPERTY_ID};
private List<String> getPhotos(String propertyId) {
List<String> photoList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
// cursor = db.query(IMAGE_LISTINGS, photos, null, null, null, null, null);
cursor = db.query(IMAGE_LISTINGS, photos,
PROPERTY_ID +" = ?", new String[]{propertyId}, null, null, null, null);
if (cursor != null && (cursor.getCount() > 0)) {
cursor.moveToFirst();
do {
photoList.add(cursor.getString(0));
photoList.add(cursor.getString(1));
} while (cursor.moveToNext());
}            Log.i("CursorCountPHOTO", String.valueOf(cursor.getCount()));
assert cursor != null;
cursor.close();
// db.close();
return photoList;
}
private static final String LISTING_CATEGORY_JOIN = PROPERTY_LISTINGS
+" LEFT JOIN " + AVALIABLE_CATEGORY + " ON " + PROPERTY_LISTINGS+ "." +PROPERTY_CATEGORIES_ID + " = " + AVALIABLE_CATEGORY+"."+ PROPERTY_CATEGORIES_ID;
private String[] JOIN_PROJECTION = new String[]{PROPERTY_ID, PROPERTY_TITLE, PROPERTY_SHORT_DESCRIPT,
AVALIABLE_CATEGORY+"."+PROPERTY_CATEGORIES_ID, AVALIABLE_CATEGORY+"."+PROPERTY_CATEGORIES,
PROPERTY_PRICE, PROPERTY_DISPLAYER};
public ArrayList<PropertyModelItem> getFullTable( String propertyId) {
ArrayList<PropertyModelItem> propertyArrayList = new ArrayList<>();
PropertyModelItem propertyItem;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cr;
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(LISTING_CATEGORY_JOIN);
/*cr = qb.query(db, JOIN_PROJECTION, PROPERTY_LISTINGS + "." + PROPERTY_CATEGORIES_ID + " = ?",
new String[]{String.valueOf(categoryId)}, null, null, null, null);*/
cr = qb.query(db, JOIN_PROJECTION,null,null, null, null, null, null);
Log.i("JoinTable",cr.toString());
if (cr != null && (cr.getCount() > 0)) {
cr.moveToFirst();
do {
propertyItem = new PropertyModelItem();
propertyItem.setCategoryName(cr.getString(4));
propertyItem.setBuildingCategoryItems(getAllListing(propertyId));
propertyArrayList.add(propertyItem);
} while (cr.moveToNext());
} Log.i("CursorCountJOIN", String.valueOf(cr.getCount()));
assert cr != null;
cr.close();
db.close();
return propertyArrayList;
}
/* private String[] combineTable ={AVALIABLE_CATEGORY+"."+PROPERTY_CATEGORIES_ID,
AVALIABLE_CATEGORY+"."+PROPERTY_CATEGORIES,PROPERTY_LISTINGS+"."+PROPERTY_CATEGORIES_ID};
public List<PropertyModelItem> getFullTable(String categoryId){
List<PropertyModelItem> fullTable = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
String CATEGORY_TABLE_QUERY =""
String MY_QUERY = "SELECT * FROM "+ PROPERTY_LISTINGS +" p "
+ " INNER JOIN " + AVALIABLE_CATEGORY +" c " + " ON p."+ PROPERTY_CATEGORIES_ID + " = c." + PROPERTY_CATEGORIES_ID + " WHERE c." + PROPERTY_CATEGORIES +"=?";
Log.i("Combine TABLE",MY_QUERY);
cursor = db.rawQuery(MY_QUERY, new String[]{String.valueOf(categoryId)});
if (cursor != null && (cursor.getCount() > 0)) {
cursor.moveToFirst();
do {
PropertyModelItem propertyModelItem = new PropertyModelItem();
propertyModelItem.setCategoryName(cursor.getString(0));
propertyModelItem.setBuildingCategoryItems(getAllListing());
fullTable.add(propertyModelItem);
} while (cursor.moveToNext());
Log.i("CursorCount", String.valueOf(cursor.getCount()));
}
assert cursor != null;
cursor.close();
db.close();
return fullTable;
}
*/
private String[] category = {PROPERTY_CATEGORIES_ID,PROPERTY_CATEGORIES,
PROPERTY_CATEGORY_DESCRIPTION, PROPERTY_TYPE};
public List<BuildingCategoryItem> getAllCategory(){
List<BuildingCategoryItem> categoryItems = new ArrayList<>();
BuildingCategoryItem buildingCategoryItem;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
cursor = db.query(AVALIABLE_CATEGORY,category, null,null,null,null,null);
if (cursor != null && (cursor.getCount()>0)){
cursor.moveToFirst();
do{
buildingCategoryItem = new BuildingCategoryItem();
buildingCategoryItem.setId(cursor.getString(0));
buildingCategoryItem.setName(cursor.getString(1));
buildingCategoryItem.setTypes(getAllTypes(cursor.getString(0)));
buildingCategoryItem.setAllowedDescriptions(getAllDescription(cursor.getString(0)));
categoryItems.add(buildingCategoryItem);
}while (cursor.moveToNext());
}Log.i("CursorCount", String.valueOf(cursor.getCount()));
assert cursor != null;
cursor.close();
db.close();
return categoryItems;
}
String [] cat_descript = {PROPERTY_CATEGORY_DESCRIPTION};
private List<String> getAllDescription(String string) {
List<String> category_description = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
//cursor = db.query(AVALIABLE_DESCRIPTION, cat_descript,null,null,null,null,null);
cursor = db.query(AVALIABLE_DESCRIPTION, new String[]{PROPERTY_CATEGORY_DESCRIPTION},
PROPERTY_CATEGORIES_ID+" = ?", new String[]{string}, null, null, PROPERTY_CATEGORY_DESCRIPTION, null);
if (cursor != null && (cursor.getCount()>0)){
cursor.moveToFirst();
do{
category_description.add(cursor.getString(0));
}while (cursor.moveToNext());
}assert cursor != null;
cursor.close();
db.close();
return category_description;
}
String[] cat_type ={PROPERTY_TYPE};
private List<String> getAllTypes(String string) {
List<String> category_types = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
//cursor = db.query(AVALIABLE_DESCRIPTION, cat_type,null,null,null,null,null);
cursor = db.query(AVALIABLE_CATEGORY_TYPE, new String[]{PROPERTY_TYPE},
PROPERTY_CATEGORIES_ID+" = ?", new String[]{string}, null, null, PROPERTY_TYPE, null);
if (cursor != null && (cursor.getCount()>0)){
cursor.moveToFirst();
do{
category_types.add(cursor.getString(0));
}while (cursor.moveToNext());
}assert cursor != null;
cursor.close();
db.close();
return category_types;
}
public void  deleteCategory(){
SQLiteDatabase db = this.getWritableDatabase();
int deleteCat = db.delete(AVALIABLE_CATEGORY, null, null);
db.close();
}
public void  deletePhoto(){
SQLiteDatabase db = this.getWritableDatabase();
int deletePhotos = db.delete(IMAGE_LISTINGS, null, null);
// db.close();
}
public void  deleteListing(){
SQLiteDatabase db = this.getWritableDatabase();
int deleteListing = db.delete(PROPERTY_LISTINGS, null, null);
db.close();
}
private boolean categoriesExists(String categoryId, SQLiteDatabase db){
boolean status = false;
Cursor cr = db.query(AVALIABLE_CATEGORY, new String[]{PROPERTY_CATEGORIES_ID},
PROPERTY_CATEGORIES_ID +" = ?", new String[]{categoryId}, null, null, null, null);
if(cr != null && (cr.getCount() > 0)) {
status = true;
}
assert cr != null;
cr.close();
return status;
}
public int categoriesCount(){
SQLiteDatabase db = this.getReadableDatabase();
int count = 0;
Cursor cr = db.query(AVALIABLE_CATEGORY, null,null,null, null, null, null, null);
if(cr != null && (cr.getCount() > 0)) {
count = cr.getCount();
}
assert cr != null;
cr.close();
return count;
}
private boolean listingExists(String propertyId, SQLiteDatabase db) {
boolean status = false;
Cursor cr = db.query(PROPERTY_LISTINGS, new String[]{PROPERTY_ID},
PROPERTY_ID +" = ?", new String[]{propertyId}, null, null, null, null);
if (cr != null && (cr.getCount() > 0)){
status = true;
}
assert  cr != null;
cr.close();
return status;
}
public int listingCount() {
SQLiteDatabase db = this.getReadableDatabase();
int count = 0;
Cursor cr = db.query(PROPERTY_LISTINGS, null,null, null, null, null, null, null);
if (cr != null && (cr.getCount() > 0)){
count = cr.getCount();
}
assert  cr != null;
cr.close();
return count;
}
private boolean photoExists(String image_url, SQLiteDatabase db){
boolean status = false;
Cursor cr = db.query(IMAGE_LISTINGS, new String[]{PHOTO_URL},
PHOTO_URL +" = ?", new String[]{image_url}, null, null, null, null);
if(cr != null && (cr.getCount() > 0)) {
status = true;
}
assert cr != null;
cr.close();
return status;
}
}
Answers: