我開發了一個應用程序,我想從網絡視圖的特定文件夾中顯示圖像。該應用程序正在工作,但只有來自文件夾的第一張圖像出現在每個網格中,而我希望顯示所有圖像。我沒有得到我錯誤的地方。 下面,我張貼我的代碼:只有一個圖像出現在gridview的每個網格
專輯活動:
public class Album3Activity extends Activity {
static File [] mediaFiles;
static File imageDir;
GridView gridView;
ImageAdapter adapter;
Intent in;
public static final String TAG = "Album3Activity";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
prepareList();
adapter = new ImageAdapter(this, mediaFiles);
gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
in = new Intent(getApplicationContext(), FullScreen.class);
in.putExtra("id", position);
startActivity(in);
}
});
}//onCreate
public static Bitmap prepareList() {
imageDir = new File(Environment.getExternalStorageDirectory().toString()+
"/diplomat");
mediaFiles = imageDir.listFiles();
Bitmap bmp = null;
for(File imagePath:mediaFiles){
try{
bmp = BitmapFactory.decodeStream(imagePath.toURL().openStream());
}catch(Exception e){
Log.d(TAG, "Exception: "+e.toString());
}//catch
}//for
Log.d(TAG, "prepareList() called");
return bmp;
}//prepareList
} //類
圖片適配器:
public class ImageAdapter extends BaseAdapter{
Activity act;
File[] mFiles;
public static final String TAG = "ImageAdapter";
public ImageAdapter(Activity act, File[] mFiles){
super();
this.act = act;
this.mFiles = mFiles;
}//ImageAdapter
public int getCount() {
return mFiles.length;
}//getCount
public Object getItem(int postion) {
return mFiles[postion];
}//getItem
public long getItemId(int position) {
return 0;
}//getItemId
public static class ViewHolder{
ImageView iv;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater li = act.getLayoutInflater();
if(convertView == null){
view = new ViewHolder();
convertView = li.inflate(R.layout.gridview_row, null);
view.iv = (ImageView)convertView.findViewById(R.id.imageView1);
convertView.setTag(view);
}//if
else{
view = (ViewHolder)convertView.getTag();
}//else
Bitmap bmp = Album3Activity.prepareList();
view.iv.setImageBitmap(bmp);
Log.d(TAG, "getView called");
return convertView;
}//getView
} // ImageAdapter
對不起迪帕,可以請你重新發布,你是打算做的鏈接不會出現在這裏的鏈接。 – Nitish