2012-05-12 57 views
2

我開發了一個應用程序,我想從網絡視圖的特定文件夾中顯示圖像。該應用程序正在工作,但只有來自文件夾的第一張圖像出現在每個網格中,而我希望顯示所有圖像。我沒有得到我錯誤的地方。 下面,我張貼我的代碼:只有一個圖像出現在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

回答

0

在getView中,每次調用album3Activity.prepareList()時,都會爲所有網格返回單個圖像。嘗試每次在mFiles [position]中傳遞特定的圖像路徑,並獲取位圖或一次獲取所有位圖並將其存儲在arrayList中,並將ArrayList傳遞到適配器並在getView中使用它。

嘗試this..it可以幫助你..

+1

對不起迪帕,可以請你重新發布,你是打算做的鏈接不會出現在這裏的鏈接。 – Nitish

2

注意prepareList方法,它沒有返回位圖根據ImageView的位置或索引,那麼它將返回相同的圖像的時候,將其更改爲接受索引參數,並返回相應的位圖,如下所示:

public static Bitmap prepareList(int index) { 
    imageDir = new File(Environment.getExternalStorageDirectory().toString()+ 
      "/diplomat"); 
    mediaFiles = imageDir.listFiles(); 
    File imagePath=imageDir[index]; 
    Bitmap bmp = null; 
     try{ 
      bmp = BitmapFactory.decodeStream(imagePath.toURL().openStream()); 
     }catch(Exception e){ 
      Log.d(TAG, "Exception: "+e.toString()); 
     }//catch 
    Log.d(TAG, "prepareList() called"); 
    return bmp; 
}//prepareList 
+0

根據你的建議我修改了我的代碼,但仍面臨同樣的問題。 – Nitish

+0

編輯您的代碼以反映更新的代碼。 – jeet

相關問題