2

How can we switch the pictures with some information? When we switch the picture then information should also be get switched in Gallery view in android.

  public class ImageAdapter extends BaseAdapter {
        String[] Merchantname=null,Cardname=null,Points=null,Expirydate=null,status=null;
        private Context ctx;
        int imageBackground;
        Bitmap[] image_data;

        public ImageAdapter(Context c, Bitmap []card_image,String [] Merchantname,String [] Cardname,String []points,String[] Expirydate, String []status) {
            ctx = c;
            image_data = card_image;
            TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
            imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
            ta.recycle();
            this.Merchantname=Merchantname;
            this.Cardname=Cardname;
            this.Points=points;
            this.Expirydate=Expirydate;
            this.status=status;
        }

        public int getCount() {

            return image_data.length;
        }

        public Object getItem(int arg0) {

            return arg0;
        }

        public long getItemId(int arg0) {

            return arg0;
        }

        public View getView(int position, View arg1, ViewGroup arg2) {
            TextView tv1,tv2,tv3,tv4,tv5;
            ImageView i ;//= new ImageView(this.ctx);
            if (arg1 == null) { 
                i = new ImageView(this.ctx); 
            } else { 
                i = (ImageView) arg1; 
            } 

            tv1=(TextView)findViewById(R.id.Merchantname);
            tv2=(TextView)findViewById(R.id.Cardname);
            tv3=(TextView)findViewById(R.id.Expirydate);
            tv4=(TextView)findViewById(R.id.status);

            tv1.setText(Merchantname[position]);
            tv2.setText(Cardname[position]);
            tv3.setText(Expirydate[position]);
            tv4.setText(status[position]);

//          ImageView iv = new ImageView(ctx);
//          Drawable drawable = new BitmapDrawable(getResources(), image_data[position]);
            i.setImageBitmap(image_data[position]);
            i.setImageDrawable(new BitmapDrawable(getResources(), image_data[position]));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setLayoutParams(new Gallery.LayoutParams(300,200));
            i.setBackgroundResource(imageBackground);
            return i;
        }

    }
Yury
  • 20,618
  • 7
  • 58
  • 86
user1196969
  • 335
  • 2
  • 5
  • 14
  • add in getView method layout = ()c.getLayoutInflater().inflate(R.layout., null); and change tv1=(TextView)layout.findViewById(R.id.Merchantname);... Also add your ImageView to this layout and return this layour object. – Natali Mar 05 '12 at 13:12

3 Answers3

0

You simple need to use an ArrayList whose Index would be linked with the Gallery views individual views, and the lenth of the ArrayList would match the Length of Gallery View, on the setOnItemClickListener use the position variable to change the Content from the Array List matching the same index

   // Reference the Gallery view
    Gallery g = (Gallery) findViewById(R.id.gallery);
    // Set the adapter to our custom adapter (below)
    g.setAdapter(new ImageAdapter(this));

    // Set a item click listener, and just Toast the clicked position
    g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
Some one Some where
  • 777
  • 1
  • 8
  • 26
  • I dont want to set ClickListener, As we scroll the information related to that picture should be displayed dynamically – user1196969 Mar 05 '12 at 12:54
0

Create layout with ImageView and TextView. Then write your custom adapter for Gallery and there in getView method put to ImageView and TextView your values. Them set this adapter to your Gallery.

Natali
  • 2,934
  • 4
  • 39
  • 53
0

The Problem Lies here,

    tv1=(TextView)findViewById(R.id.Merchantname);
    tv2=(TextView)findViewById(R.id.Cardname);
    tv3=(TextView)findViewById(R.id.Expirydate);
    tv4=(TextView)findViewById(R.id.status);

You forgot to mention the Parent View before asking for the LayoutFile, add (TextView)arg1.findViewById() for all the TextViews and let us know. Hope it helps

Some one Some where
  • 777
  • 1
  • 8
  • 26