2016-04-21 66 views
-1

我想顯示一個圖像陣列作爲位圖ListView,有人可以幫助我嗎?Android Studio:如何將ImageBitmap轉換爲ImageId?

這裏是我的CustomList.java

public class CustomList extends ArrayAdapter<String>{ 

private final Activity context; 
private final String[] web; 
private final Integer[] imageId; 
public CustomList(Activity context, String[] web, Integer[] imageId) { 
    super(context, R.layout.mylist, web); 
    this.context = context; 
    this.web = web; 
    this.imageId = imageId; 

} 
@Override 
public View getView(int position, View view, ViewGroup parent) { 
    LayoutInflater inflater = context.getLayoutInflater(); 
    View rowView= inflater.inflate(R.layout.mylist, null, true); 
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt); 

    ImageView imageView = (ImageView) rowView.findViewById(R.id.img); 
    txtTitle.setText(web[position]); 
    imageView.setImageResource(imageId[position]); 
    return rowView; 
} 

}

,這裏是從mainActivity.java

一些代碼
ListView list; 
ImageView image; 
String[] web = { 
     "Google Plus", 
     "Twitter", 
     "Windows", 
     "Bing", 
     "Itunes", 
     "Wordpress", 
     "Drupal" 
} ; 
Integer[] imageId = { 
     R.drawable.image1, 
     R.drawable.image2, 
     R.drawable.image3, 
     R.drawable.image4, 
     R.drawable.image5, 
     R.drawable.image6, 
     R.drawable.image7 

}; 

CustomList adapter = new CustomList(this, web, imageId); 
     list=(ListView)findViewById(R.id.list); 
     list.setAdapter(adapter); 

我所有的圖片都是以位圖的類型,我想對那些置於imageId數組上以顯示在自定義ListView上

+1

imageView.setImageBitmap()?你將不得不解釋一下你的問題 – Nanoc

+0

@Nanoc我已經粘貼了相關代碼。 – ERJ

回答

0

使用INT []陣列代替整數[]陣列,

int[] imageId = { 
     R.drawable.image1, 
     R.drawable.image2, 
     R.drawable.image3, 
     R.drawable.image4, 
     R.drawable.image5, 
     R.drawable.image6, 
     R.drawable.image7 

也作出相應的變化你CustomList.java文件

+0

如何實現ImageBitmap而不是繪製?您需要從自定義位置顯示 – ERJ

+0

。對? –

+0

Bitmap image = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); imageView.setImageBitmap(image); –

相關問題