2013-04-08 79 views
0

我在Android中編寫圖庫視圖時遇到此問題。這些圖像出現在屏幕的頂部屏幕(如預期的那樣),當它們被點擊時,它們會說出選擇了哪個圖像(如預期的那樣)。但是,所選圖像不會顯示在imageView中。我的代碼如下:當圖像選擇時,圖庫視圖將不會顯示完整的ImageView

public class MainActivity extends Activity { 

Integer[] imageIDs = { 
     R.drawable.fourfromunimon, 
     R.drawable.fourfromunisat, 
     R.drawable.fourfromunisun, 

}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Gallery gallery = (Gallery) findViewById(R.id.gallery1); 

    gallery.setAdapter(new ImageAdapter(this)); 
    gallery.setOnItemClickListener(new OnItemClickListener() 
    { 
     @SuppressWarnings("rawtypes") 
     public void onItemClick(AdapterView parent, View v, 
       int position, long id) 
     { 
      Toast.makeText(getBaseContext(), 
        "pic" + (position + 1) + " selected", 
        Toast.LENGTH_SHORT).show(); 

      // Display the image 
      ImageView imageView = 
        (ImageView) findViewById(R.id.image1); 
      imageView.setImageResource(imageIDs[position]); 
     } 
    }); 
} 

public class ImageAdapter extends BaseAdapter { 

    Context context; 

    int itemBackground; 

    public ImageAdapter(Context c) 
    { 
     context = c; 

     TypedArray a = obtainStyledAttributes(
       R.styleable.Gallery1); 
     itemBackground = a.getResourceId(
     R.styleable.Gallery1_android_galleryItemBackground, 
     0); 
     a.recycle(); 
    } 


public int getCount(){ 
    return imageIDs.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 imageView; 
    if (convertView == null){ 
     imageView = new ImageView(context); 
     imageView.setImageResource(imageIDs[position]); 
     imageView.setScaleType(
       ImageView.ScaleType.FIT_XY); 
     imageView.setLayoutParams(
       new Gallery.LayoutParams(450, 200)); 
    }else{ 
     imageView = (ImageView) convertView; 
    } 
    imageView.setBackgroundResource(itemBackground); 
    return imageView; 
    } 
} 

}

任何幫助將不勝感激,因爲我沒有在那裏我可以去錯了foggiest想法。

+0

是ImageView與android:id =「image1」目前在activity_main.xml? – Akhil 2013-04-08 19:11:02

+0

爲什麼每次點擊圖庫項目時都會發現視圖?檢查:'ImageView imageView =(ImageView)findViewById(R.id.image1);' – 2013-04-08 19:13:52

回答

0

使用

imageView.invalidate(); 

設置新的圖像後。 (看看the Drawing section of View in here)。

或者

imageView.invalidateDrawable(imageView.getDrawable()); 
+0

我已經嘗試了兩種方式,正如你所描述的。但它似乎並不奏效。 。 – bez91 2013-04-08 19:38:31

0

我的Android導師(SIR)使用.png格式,請嘗試使用僅.png格式,並把他們所有的一切繪製的文件夾中的項目在Eclipse中,如果它的工作原理還是不做迴應

也初始化imageIDs

Integer[] imageIDs = { 
    R.drawable.fourfromunimon, 
    R.drawable.fourfromunisat, 
    R.drawable.fourfromunisun, 
}; 
declare gallery and imageview here 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    you can pass the find view by id here of gallery and imageview 

,並嘗試使用此之後...

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    // TODO Auto-generated method stub 
    ImageView ivv=new ImageView(context); 
    ivv.setImageResource(Garr[position]); 
    ivv.setScaleType(ImageView.ScaleType.FIT_XY); 
    ivv.setLayoutParams(new Gallery.LayoutParams(450, 200)); 
    ivv.setBackgroundResource(itembackground); 
    return ivv; 
} 

如果它工作或沒有響應