user1850936
user1850936

Reputation: 129

Image Switcher Issue in Android

I am using image switcher to display several picture.

I have initialized the final Integer[] imageIDs = {} and use declare elements in it in If-Else statement which locates in the OnCreate. I do so because for each different places I would like to input different pictures into the imageIDs. In this example is "UK". So if another places was chosen, the imageIDs should have different pictures refer to.

Unfortunately there was nothing in the imageswitcher when I run the apps.

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;
    }   
}

Upvotes: 0

Views: 238

Answers (1)

Chinmoy Debnath
Chinmoy Debnath

Reputation: 2824

Your global imageIDs array length is ZERO so its displayed nothing. You can't use final array in your case as the objects can varies depending on your selectedSubArea

Else

You can change your ImageAdapter like this then it will work

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;
    }   
}

then set this adapter as

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

And remove the global imageIDs

Upvotes: 2

Related Questions