我正在使用图像切换器来显示多张图片。
我初始化了最终的Integer [] imageIDs = {},并在位于OnCreate中的If-Else语句中使用了declare元素。我这样做是因为对于每个不同的地方,我想将不同的图片输入到imageID中。在这个例子中是“英国”。因此,如果选择了其他地方,则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;
}
}
答案 0 :(得分: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