0
我真的需要你的幫助來解決一個可能對你很多人來說很愚蠢的問題。用從數據庫中提取的圖像填充GrindView
我需要創建一個簡單的Grindview並填充圖像。我嘗試了android開發人員網站提供的示例代碼,它很好地工作。
問題是示例代碼提供了drawable的圖像。我需要用存儲在我的sqlite Db中的位圖填充爲BLOB。
數據庫已測試和工作,我能夠從BLOB獲得位圖沒有問題。
在我的代碼中,當活動開始時,沒有任何反應,我得到一個沒有圖像的白色屏幕。我花了很多時間在這個,我希望你能給我一些提示!預先感謝您的答覆! :d
這是我的類代碼:
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;
}
}
}
而且這裏的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"
/>
這是我的第一個問題在這裏StackOverflow的,所以給我一些提示,如果我做錯了什麼用我的問題!!!謝謝!
爲什麼你不使用GridView自定義行項目? – 2013-04-23 11:55:26
問題是gridview是空白的,並且看起來像活動開始時它是空的......自定義行項目如何解決問題? – Razinar 2013-04-28 22:12:58