2

我们如何切换带有一些信息的图片?当我们切换图片时,信息也应该在 android 的 Gallery 视图中切换。

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

    }
4

3 回答 3

0

问题出在这里,

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

您在要求 LayoutFile 之前忘记提及父视图,添加(TextView)arg1.findViewById()所有 TextView 并让我们知道。希望能帮助到你

于 2012-03-05T14:09:39.963 回答
0

ImageView使用和创建布局TextViewGallery然后在getView方法 put toImageViewTextView您的值中编写您的自定义适配器。他们将此适配器设置为您的Gallery.

于 2012-03-05T12:55:54.743 回答
0

您只需使用ArrayList其索引将与Gallery views各个视图链接,并且 ArrayList 的长度将与 Gallery View 的长度匹配,setOnItemClickListener使用位置变量从匹配相同索引的数组列表中更改内容

   // 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();
        }
    });
于 2012-03-05T12:48:57.860 回答