獲取資源的ID,這樣我有一個畫廊(從資源文件夾中的圖片製作),一個按鈕和一個ImageView的機器人 - 從畫廊
我想選擇從圖庫中的圖片之一的程序,按按鈕,並更改ImageView的位圖來顯示一個從畫廊selecected ...
我已經改變方法,從活動到活動的所有固定的...但我只是遇到了麻煩的ID挑選的圖像,整數,可繪製,長,字符串...或任何它他LPS ...
下面的代碼:
@覆蓋
公共無效的onCreate(捆綁savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
//CODE TO KNOW IF PICKED THE 1ST OR 2ND IMAGE
//Toast.makeText(Galeria.this, v.getId() + "", Toast.LENGTH_SHORT).show();
//Toast.makeText(Galeria.this, parent.getItemIdAtPosition(position) + "", Toast.LENGTH_SHORT).show();
//Toast.makeText(Galeria.this, id + "", Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageId = {
R.drawable.image1, //first image
R.drawable.image2, //second image
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount()
return mImageId.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageId[position]);
i.setLayoutParams(new Gallery.LayoutParams(400, 225));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
三個吐司那裏就OnItemClick空白是我的顯示嘗試該ID ...但只顯示畫廊的圖像的位置。
有沒有其他解決方案?我已閱讀了有關dinamics ID的一些信息......但可以很好理解...
我應該在哪裏放置代碼以獲取我想要的ID?
任何幫助?謝謝。
我喜歡你的解決方案謝謝!但我在這行得到這樣的錯誤: 'INT圖像標識=((ImageAdapter)母公司).getItem(位置);' **這行 多個標記 - 類型不匹配:不能從對象轉換爲int - 無法從AdapterView轉換到Galeria.ImageAdapter ** 即使試圖解決類型不匹配,我仍然得到第二個錯誤我不明白 – 2013-05-13 09:07:34
嘗試將其轉換爲整數 – Blackbelt 2013-05-13 09:11:56
仍然獲得** - 無法投射AdapterView to Galeria.ImageAdapter ** – 2013-05-13 09:15:25