I’m developing an app which downloads a json objects into database,this is done so that the users can interact with the data without the knowladge of the data source.
I have managed to download the required fields from the json API into my database as string and I can display all fileds using textfields however I have 100 images which I want to present as images and not as string.
I have tried to implement several methods so that I can display the images.And followed several tutorails to see if I could find a solution to my problem.
1,2 and more.
Below is my DBHelper.
@Override
public void onCreate(SQLiteDatabase db) {
String sql = String.format( "create table %s ( %s int primary key, %s
TEXT, %s TEXT, %s TEXT, %S TEXT, %S TEXT, %S TEXT)",
TABLE,C_ID
,C_IDENTIFIER,C_PRICE,C_BEDROOMS,C_ADDRESS,C_PROPERTYTYPE,C_PHOTOS);
Log.d(TAG, "OnCreate sql :"+sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists "+TABLE);
Log.d(TAG, "onUpdate dropped table" + TABLE);
this.onCreate(db);
}
Here is the class which gets the fields from the database,which works fine other than the image.
private SQLiteDatabase database;
private CursorAdapter dataSource;
private static final String fields[] = { "identifier", "price", "bedrooms",
"ADDRESS", "PROPERTYTYPE", "PHOTOTHUMBNAILURL", BaseColumns._ID };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.database);
DBHelper helper = new DBHelper(this);
database = helper.getReadableDatabase();
database = helper.getWritableDatabase();
Cursor data = database.query(DBHelper.TABLE, fields, null, null, null,
null, null);
dataSource = new SimpleCursorAdapter(
this,
R.layout.list,
data,
fields,
new int[] { R.id.identifier2, R.id.price2, R.id.bedrooms2,
R.id.address2, R.id.propertytype2, R.id.propertyPhoto2 });
setListAdapter(dataSource);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String identifier = ((TextView) view
.findViewById(R.id.identifier2)).getText().toString();
String price = ((TextView) view.findViewById(R.id.price2))
.getText().toString();
String bedrooms = ((TextView) view.findViewById(R.id.bedrooms2))
.getText().toString();
String address = ((TextView) view.findViewById(R.id.address2))
.getText().toString();
String propertyType = ((TextView) view
.findViewById(R.id.propertytype2)).getText().toString();
String photoThumbnailUrl = ((TextView)
view.findViewById(R.id.propertyPhoto2)).getText()
.toString();
}
});
database.close();
helper.close();
}
When I try this( String photoThumbnailUrl = ((TextView)
view.findViewById(R.id.propertyPhoto2)).getText()
.toString();)It works and displays the images as string however when I try to use this
(ImageView image =(ImageView)findViewById(R.id.image1);
ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray);
Bitmap Image = BitmapFactory.decodeStream(imageStream);) it does not work.
Any solution or advice, thanks in advance.
Ok, so you have the URLs to your photos… so what’s the problem? Make web requests for the photos using the URLs your first request returned to you.
I’d use this, it’s pretty awesome:
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html