我想用圖標動態地填充gridview。我遵循gridview hello教程。然而,圖像陣列並不總是完全一樣的。根據之前的動作,給出由圖標名稱構成的不同的image array
(從肥皂響應中提取),例如, agenda => agenda.png。我想通過在數組中循環並將其與R.drawable + icon_name
相加來創建該數組。但是R.drawable
無法解析到所請求的Integer數組。解析錯誤 - Android java
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
final ArrayList<String> image = getIntent().getStringArrayListExtra("image");
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return icoontjes.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public ArrayList<Integer> icoontjes;
{
for (int i=0; i<image.size(); i++){
Integer icon= Integer.valueOf("R.drawable."+image.get(i));
icoontjes.add(icon);
}
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
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.setImageResource(icoontjes.get(position));
return imageView;
}
}
這種方法看起來更好 – Selvin 2011-04-27 13:39:40
thx爲我工作! – Swellwave 2011-05-03 11:20:02