I am using an ImageSwitcher to display a list of pictures. When the user selects an item from a Listview in A.class, the ImageSwitcher is called and the user views the pictures. If item 1 is clicked on in the list, then a certain set of drawables should be passed to the imageswitcher. If item 2 on the list is clicked, a different set of pictures should be shown. I have been struggling with how to pass the drawables from A.class to the ImageSwitcher.
In Summary: I want to pass a class of drawables from A.class to ImageSwitcher such that if item 1 is clicked, in ImageSwitcher:
private Integer[] mThumb = {R.Drawable.pic1, R.Drawable.pic2, R.Drawable.pic3}
and if item 2 is clicked, I want in ImageSwitcher for: private Integer[] mThumb = [R.Drawable.pic6, R.Drawable.pic7, R.Drawable.pic8}
From the sending Activity, so far I have tried many permutations of using putExtra putIntegralArrayExtra, etc etc. I'm sure the answer to this is pretty basic. I am new to java, and am still (slowly) learning. Thanks for your help!
Here is the code for the imageswitcher, so you can see what I mean by int[] mThumb:
public class ImageSwitch1 extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory, OnTouchListener {
Matrix matrix = new Matrix();
Matrix eventMatrix = new Matrix();
final static int NONE = 0;
final static int DRAG = 1;
final static int ZOOM = 2;
int touchState = NONE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.imageswitcher);
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
mSwitcher.setImageResource(mImage[position]);
final TextView tv = (TextView)findViewById(R.id.SwitcherText);
tv.setText(mText[position]);
}
public void onNothingSelected(AdapterView parent) {
}
public View makeView() {
ImageView i = new ImageView(this);
i.setScaleType(ScaleType.MATRIX);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
i.setOnTouchListener(this);
return i;
}
final static float MIN_DIST = 50;
static float eventDistance = 0;
static float centerX =0, centerY = 0;
public boolean onTouch(View v, MotionEvent event) {
ImageView i = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//primary touch event starts: remember touch down location
touchState = DRAG;
centerX = event.getX(0);
centerY = event.getY(0);
eventMatrix.set(matrix);
break;
case MotionEvent.ACTION_POINTER_DOWN:
//secondary touch event starts: remember distance and center
eventDistance = calcDistance(event);
calcMidpoint(centerX, centerY, event);
if (eventDistance > MIN_DIST) {
eventMatrix.set(matrix);
touchState = ZOOM;
}
break;
case MotionEvent.ACTION_MOVE:
if (touchState == DRAG) {
//single finger drag, translate accordingly
matrix.set(eventMatrix);
matrix.setTranslate(event.getX(0) - centerX,
event.getY(0) - centerY);
} else if (touchState == ZOOM) {
//multi-finger zoom, scale accordingly around center
float dist = calcDistance(event);
if (dist > MIN_DIST) {
matrix.set(eventMatrix);
float scale = dist / eventDistance;
matrix.postScale(scale, scale, centerX, centerY);
}
}
// Perform the transformation
i.setImageMatrix(matrix);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
touchState = NONE;
break;
}
return true;
}
private float calcDistance(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
private void calcMidpoint(float centerX, float centerY,
MotionEvent event) {
centerX = (event.getX(0) + event.getX(1))/2;
centerY = (event.getY(0) + event.getY(1))/2;
}
private ImageSwitcher mSwitcher;
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumb.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 im = new ImageView(mContext);
im.setImageResource(mThumb[position]);
im.setAdjustViewBounds(true);
im.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
im.setBackgroundResource(R.drawable.picture_frame);
return im;
}
private Context mContext;
}
//these are the little pictures
private Integer[] mThumb = {
R.drawable.lp_image1_thumb, R.drawable.lp_image2_thumb, R.drawable.lp_image3_thumb,
R.drawable.lp_image4_thumb, R.drawable.lp_image5_thumb, R.drawable.lp_image6_thumb,
R.drawable.lp_image7_thumb, R.drawable.lp_image8_thumb, R.drawable.lp_image9_thumb,
R.drawable.lp_image10_thumb};
Here's how I did it, and it works, though it may be rather clunky:
sending activity:
if(position == 0)
{
int[] mThumb =
{
R.drawable.lp_image1,R.drawable.lp_image2, R.drawable.lp_image3, R.drawable.lp_image4,R.drawable.lp_image5, R.drawable.lp_image6, R.drawable.lp_image7,R.drawable.lp_image8,R.drawable.lp_image9
};
myIntent = new Intent(view.getContext(), LP.class);
b.putIntArray("mImage", mImage);
b.putInt("i", 0);
}
Receiving Activity:
Bundle b=this.getIntent().getExtras();
int[] mThumb = b.getIntArray("mThumbSent");
ImageAdapter adapter = new ImageAdapter(this, mThumb);
And this code can be repeated wherever necessary to bring in the int[] from the sending activity