Here I need to pass images into a gallery.
In one class I have 10 buttons, here when click 1 button I need to pass 1 button related images into the gallery, when click 2 button I need to pass 2 button related images into gallery.
I tried using the below code but here when click 1 button it is working fine means images displayed into gallery, but when I click 2,3 …buttons I got black screen.
Please any one suggest me how to pass different arrays into same activity.
HomeDemo.class:
public class HomeDemo extends Activity implements OnTouchListener{
Button home1,home2,home3,home4;
ImageButton back;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.homedemo);
home1=(Button)findViewById(R.id.button1);
home2=(Button)findViewById(R.id.button2);
home3=(Button)findViewById(R.id.button3);
home4=(Button)findViewById(R.id.button4);
home1.setOnTouchListener(this);
home2.setOnTouchListener(this);
home3.setOnTouchListener(this);
home4.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(v==home1)
{
Intent i=new Intent(HomeDemo.this,Home1.class);
i.putExtra("k1", 1);
startActivity(i);
}
if(v==home2)
{
Intent i=new Intent(HomeDemo.this,Home1.class);
i.putExtra("k2", 2);
startActivity(i);
}
if(v==home3)
{
Intent i=new Intent(HomeDemo.this,Home1.class);
i.putExtra("k3", 3);
startActivity(i);
}
if(v==home4)
{
Intent i=new Intent(HomeDemo.this,Home1.class);
i.putExtra("k4", 4);
startActivity(i);
}
return false;
}
}
Home1 .class
public class Home1 extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory{
private TextSwitcher mSwitcher;
ImageButton home,sound;
Gallery g;
static int nextbtn = 0;
static int prebtn = 0;
ImageAdapter iadapter;
int galpos;
int countk1=0;
int countk2=0;
int countk3=0;
int countk4=0;
public int ht;
public int wt;
public static int motiongal = 3000;
RelativeLayout l1,l2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.home1);
home=(ImageButton)findViewById(R.id.homeimage);
home.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Intent i=new Intent(Home1.this,HomeDemo.class);
startActivity(i);
return false;
}
});
sound=(ImageButton)findViewById(R.id.soundimage);
sound.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
ht = displaymetrics.heightPixels;
wt = displaymetrics.widthPixels;
l1=(RelativeLayout)findViewById(R.id.linear);
l2=(RelativeLayout)findViewById(R.id.linear1);
l1.setVisibility(View.INVISIBLE);
l2.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
l1.setVisibility(View.VISIBLE);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//Do something after 100ms
l1.setVisibility(View.INVISIBLE);
}
}, 1500);
return false;
}
});
mSwitcher = (TextSwitcher) 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));
mSwitcher.startLayoutAnimation();
try
{
g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setHorizontalScrollBarEnabled(false);
Bundle bundle = getIntent().getExtras();
galpos = bundle.getInt("GALPOS");
countk1 = getIntent().getIntExtra("k1", 0);
countk2 = getIntent().getIntExtra("k2", 0);
countk3 = getIntent().getIntExtra("k3", 0);
countk4 = getIntent().getIntExtra("k4", 0);
g.setSelection(galpos);
g.setAnimationDuration(motiongal);
} catch(Exception e){
}
g.setOnItemSelectedListener(this);
ImageButton nextButton = (ImageButton) findViewById(R.id.nextimage);
nextButton.setOnClickListener(nextButtonOnClick);
ImageButton preButton = (ImageButton) findViewById(R.id.preimage);
preButton.setOnClickListener(preButtonOnClick);
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Toast.makeText(GalleryActivity.this, "" + mTextNames[position] , Toast.LENGTH_SHORT).show();
if(position==12)
{
Intent i=new Intent(Home1.this, HomeDemo.class);
startActivity(i);
}
l1.setVisibility(View.VISIBLE);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//Do something after 100ms
l1.setVisibility(View.INVISIBLE);
}
}, 1500);
}
});
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
mSwitcher.setText(mTextNames[position]);
nextbtn = position;
}
public OnClickListener nextButtonOnClick = new OnClickListener(){
public void onClick(View v) {
if(nextbtn < (mTextNames.length-1))
{
try
{
mSwitcher.setText(mTextNames[nextbtn+1]);
mSwitcher.startLayoutAnimation();
mSwitcher.getDrawingTime();
g.setSelection(nextbtn+1);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
}
catch(Exception e)
{}
}else
{
try
{
mSwitcher.setText(mTextNames[0]);
mSwitcher.startLayoutAnimation();
mSwitcher.getDrawingTime();
g.setSelection(0);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
}
catch(Exception e)
{}
}
}
};
public OnClickListener preButtonOnClick = new OnClickListener(){
public void onClick(View v) {
if(nextbtn > 0)
{
try
{
mSwitcher.startLayoutAnimation();
mSwitcher.getDrawingTime();
g.setSelection(nextbtn-1);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
}
catch(Exception e)
{}
}else
{
try
{
mSwitcher.setText(mTextNames[mTextNames.length-1]);
mSwitcher.startLayoutAnimation();
mSwitcher.getDrawingTime();
g.setSelection(mTextNames.length-1);
Thread.sleep(300);
g.scheduleLayoutAnimation();
g.startLayoutAnimation();
Thread.sleep(300);
g.setAnimationDuration(motiongal);
}
catch(Exception e)
{}
}
}
};
public void onNothingSelected(AdapterView<?> parent) {
}
public View makeView() {
TextView i = new TextView(this);
i.setTextSize(18);
i.setGravity(1);
return i;
}
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getView() {
// TODO Auto-generated method stub
return 0;
}
public int getCount() {
return mPhotos.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
if(countk1==1)
{
i.setImageResource(mPhotos.get(position));
}
if(countk2==1)
{
i.setImageResource(mPhotos1.get(position));
}
if(countk2==3)
{
i.setImageResource(mPhotos1.get(position));
}
if(countk2==4)
{
i.setImageResource(mPhotos1.get(position));
}
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return i;
}
private Context mContext;
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
return e2.getX() > e1.getX();
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
int kEvent;
if(isScrollingLeft(e1, e2)){ //Check if scrolling left
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
}
else{ //Otherwise scrolling right
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(kEvent, null);
return true;
}
private Integer[] mImageIds = {
R.drawable.bokstavslottet01,
R.drawable.bokstavslottet02,
R.drawable.bokstavslottet03,
R.drawable.bokstavslottet04,
R.drawable.bokstavslottet05,
R.drawable.bokstavslottet06,
R.drawable.bokstavslottet07,
R.drawable.bokstavslottet08,
R.drawable.bokstavslottet09,
R.drawable.bokstavslottet10,
R.drawable.bokstavslottet11,
R.drawable.bokstavslottet12,
R.drawable.bokstavslottet13
};
private Integer[] mImageIds1 = {
R.drawable.i1,
R.drawable.i2,
R.drawable.i3,
R.drawable.i4,
R.drawable.i5,
R.drawable.i6,
R.drawable.i7,
R.drawable.i8,
R.drawable.i9,
R.drawable.i10,
R.drawable.i11,
R.drawable.i12,
R.drawable.i13
};
private static String[] mTextNames = {
"BOOKSTOVEN1", "BOOKSTOVEN2", "BOOKSTOVEN3", "BOOKSTOVEN4", "BOOKSTOVEN5", "BOOKSTOVEN6", "BOOKSTOVEN7", "BOOKSTOVEN8", "BOOKSTOVEN9", "BOOKSTOVEN10", "BOOKSTOVEN11", "BOOKSTOVEN12", "BOOKSTOVEN13"};
private ArrayList<Integer> mPhotos = new ArrayList<Integer>(Arrays.asList(mImageIds));
private ArrayList<Integer> mPhotos1 = new ArrayList<Integer>(Arrays.asList(mImageIds1));
}
1.First thing i wish to ask with you is, You mentioned here for 1
mPhotosarray and for remaining threemPhotos1. If you went right in this code means why you need unnecessaryifconditions, you can put else for the firstifand supply themPhotos1array for remaining (Or other wise you dont need to supply the 2,3,4 values).Next one is you did a mistake here at
if(countk2==1). how it will be possible to compare, because you only pass2for thek2extra.Then, i think you dont need to put Extra values with different names in the following code.
Here you are receiving
k1,k2,k3,k4values independently. I feel that you dont need to do this.Instead of doing the above, you can
putExtrafor the intent like the following code.And then you can receive like the following code,
And in the
Adapteryou can follow this,I hope that you have understand the above explanation.
And here are some useful solutions.
For receiving the
ExtrasYou can do like the following also,
getIntent.getIntExtrta("k",0)without usingBundle.