2014-03-19 63 views
0

我正在使用圖像切換器顯示多張圖片。Android中的圖像切換器問題

我初始化了最終的Integer [] imageIDs = {},並在位於OnCreate中的If-Else語句中使用declare元素。我這樣做是因爲對於每個不同的地方我都想將不同的圖片輸入到圖像ID中。在這個例子中是「英國」。所以如果選擇了另一個地方,imageID應該有不同的圖片參考。

不幸的是,當我運行應用程序時,imageswitcher中沒有任何東西。

final Integer[] imageIDs = {}; 

private ImageSwitcher imageSwitcher; 
DBAdapter dbAdapter; 
final Context context = this; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.attraction_details); 

    Intent intent = getIntent(); 
    String selectedSubArea = intent.getStringExtra("SelectedSubArea"); 

    Button btnAddToTrip = (Button) findViewById(R.id.btnAddToTrip); 
    Button btnAddToFav = (Button) findViewById(R.id.btnAddToFav); 
    Button btnShowAttractionLocation = (Button) findViewById(R.id.btnShowAttractionLocation); 
    TextView description = (TextView) findViewById(R.id.description); 
    TextView address = (TextView) findViewById(R.id.address); 
    TextView openingHours = (TextView) findViewById(R.id.openingHours); 
    TextView contactNo = (TextView) findViewById(R.id.contactNo); 

    if (selectedSubArea.equals("UK")) 
    { 
     this.setTitle(selectedSubArea); 
     description.setText("desc"); 
     address.setText("add"); 
     latitude = 2.0057378; 
     longitude = 103.3760577; 
     openingHours.setText("n/a"); 
     contactNo.setText("n/a"); 

     final Integer[] imageIDs = { 
       R.drawable.uk_1, 
       R.drawable.uk_2, 
       R.drawable.uk_3}; 

     name = selectedSubArea; 
     desc = description.toString(); 
     add = address.toString(); 
     opening = openingHours.toString(); 
     contact = contactNo.toString(); 

     dbAdapter = new DBAdapter(context); 

     imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1); 
     imageSwitcher.setFactory(this); 
     imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); 
     imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); 
     Gallery gallery = (Gallery) findViewById(R.id.gallery1); 
     gallery.setAdapter(new ImageAdapter(this)); 
     gallery.setOnItemClickListener(new OnItemClickListener() 
     { 
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
      { 
       imageSwitcher.setImageResource(imageIDs[position]); 
      } 
     }); 
    } 
} 

public View makeView() 
{ 
    ImageView imageView = new ImageView(this); 
    imageView.setBackgroundColor(0x00000000); 
    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
    imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    return imageView; 
} 

public class ImageAdapter extends BaseAdapter 
{ 
    private Context context; 
    private int itemBackground; 
    public ImageAdapter(Context c) 
    { 
     context = c; 
     //---setting the style--- 
     TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); 
     itemBackground = a.getResourceId(
     R.styleable.Gallery1_android_galleryItemBackground, 0); 
     a.recycle(); 
    } 

    //---returns the number of images--- 
    public int getCount() 
    { 
     return imageIDs.length; 
    } 

    //---returns the ID of an item--- 
    public Object getItem(int position) 
    { 
     return position; 
    } 

    public long getItemId(int position) 
    { 
     return position; 
    } 

    //---returns an ImageView view--- 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     ImageView imageView = new ImageView(context); 
     imageView.setImageResource(imageIDs[position]); 
     imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imageView.setLayoutParams(new Gallery.LayoutParams(150, 120)); 
     imageView.setBackgroundResource(itemBackground); 
     return imageView; 
    } 
} 

回答

2

您的全球imageIDs數組的長度ZERO所以其顯示任何內容。作爲對象可以取決於你的selectedSubArea

否則

你不能在你的情況下使用最終陣列您可以更改ImageAdapter這樣便,將工作

public class ImageAdapter extends BaseAdapter 
{ 
    private Context context; 
    private int itemBackground; 
    Integer[] local_imageIDs 
    public ImageAdapter(Context c, Integer[] local_imageIDs) 
    { 
     context = c; 
     this.local_imageIDs = local_imageIDs; 
     //---setting the style--- 
     TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); 
     itemBackground = a.getResourceId(
     R.styleable.Gallery1_android_galleryItemBackground, 0); 
     a.recycle(); 
    } 

    //---returns the number of images--- 
    public int getCount() 
    { 
     return local_imageIDs.length; 
    } 

    //---returns the ID of an item--- 
    public Object getItem(int position) 
    { 
     return position; 
    } 

    public long getItemId(int position) 
    { 
     return position; 
    } 

    //---returns an ImageView view--- 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     ImageView imageView = new ImageView(context); 
     imageView.setImageResource(local_imageIDs[position]); 
     imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imageView.setLayoutParams(new Gallery.LayoutParams(150, 120)); 
     imageView.setBackgroundResource(itemBackground); 
     return imageView; 
    } 
} 

然後將此適配器設置爲

gallery.setAdapter(new ImageAdapter(this, imageIDs)); 

而且刪除全球imageIDs