i really need your help to solve a problem that might appear stupid for a lot of you.
I need to create a simple Grindview and fill it with images. I tried the sample code provided by the android developers site and it works nicely.
The problem is that the sample code provides images from drawable. I need to fill it with bitmaps that are stored in my sqlite Db as BLOB.
The db is tested and working, and i'm able to obtain the bitmap from the BLOB without problem.
In my code, when the activity starts, nothing happens and i get a white screen without images. I spent many hours on this, i hope that you can give me some tips!!! Thank you in advance for the replies!!! :D
This is my class code:
package cover.me;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import cover.me.Game.SimulationView.Cursor;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class Gallery extends Activity{
private LinearLayout myGallery;
private android.database.Cursor cursor;
private DbAdapter MyDb;
private int x,y;
private Display display;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter{
private Context mContext;
private DbAdapter MyDb;
private int Counter;
private ArrayList<Bitmap> Images = new ArrayList<Bitmap>();
private boolean ImagesLoaded = false;
public ImageAdapter(Context c){
mContext = c;
}
public int getCount() {
return 0;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent){
if(ImagesLoaded == false){ //Add bitmap to Images ArrayList the first time that getView is called
MyDb = new DbAdapter(Gallery.this);
MyDb.open();
cursor = MyDb.fetchAllLevels();
cursor.moveToFirst();
for(int i=0;i<cursor.getCount()-1;i++){
byte[] image = cursor.getBlob(cursor.getColumnIndex(DbAdapter.LvL_Bitmap));
ByteArrayInputStream inputStream = new ByteArrayInputStream(image);
Bitmap mBitmap = BitmapFactory.decodeStream(inputStream);
Images.add(mBitmap);
cursor.moveToNext();
}
cursor.close();
MyDb.close();
ImagesLoaded = true;
}
ImageView imageView;
if (convertView == null){
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else{
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(Images.get(Counter));
Counter++;
return imageView;
}
}
}
And here the XML
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
This is my first question here on StackOverflow, so give me some tips if i did something wrong with my question!!! Thank you!