如何在Imageswitcher视图中获取arraylist

时间:2011-07-26 06:53:22

标签: android

我试图获取Arraylist(这里是albumArrayList)的图像来设置ImageSwitcher中的视图。但是无法获取图像。请帮我解决这个问题。谢谢..

public class ImageViewActivity extends Activity{

    public static Gallery iGllry;
    public static ImageSwitcher imageSwitcher;
    public static ImageLoader imageLoader;

    public static ArrayList<AddAlbumDetailBean> album_photos1;
    public static AddAlbumDetailBean album_photo1;

    public static ArrayList<Integer> albumArrayList;
    public static Integer albumarrayob;

    public static ArrayList<Integer> albumArrayList2;
    public static Integer albumarrayob2;

    public static ArrayList<Bitmap> ablum_photosbit;
    public static Bitmap albmphtbit;


    public static ImageView imgvwfrgallery;
    public static GridActivity gridActivity;
    //public static int position = gridActivity.posi;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imageviewactivity);

    iGllry = (Gallery) findViewById(R.id.img_glry);

    album_photos1 = GridActivity.albumPhotos;
    int count1 = album_photos1.size();
    Log.i("array of images", ""+ count1);
    albumArrayList= new ArrayList<Integer>();
    for(int i=0;i<album_photos1.size();i++){
        albumArrayList.add(albumarrayob);
    }
    int count = albumArrayList.size();
    Log.i("Array of images in integer array",""+count);
    imgvwfrgallery= (ImageView)findViewById(R.id.imgvwfrgllry);
    iGllry.setAdapter(new EfficientAdapter1(this));
    iGllry.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
            // TODO Auto-generated method stub
            Log.e("Enters in click event of gallery","GALLERY");
            imgvwfrgallery.setImageResource(albumArrayList.indexOf(album_photo1));
        Toast.makeText(getApplicationContext(), "Position "+(position+1), Toast.LENGTH_SHORT).show();
        } 
    });
}

public class EfficientAdapter1 extends BaseAdapter{
    private Context mContext1;
    private LayoutInflater mInflater1;
    ImageLoader imageLoader;
    int count;

    public EfficientAdapter1(ImageViewActivity imageViewActivity) {
        // TODO Auto-generated constructor stub
        mInflater1 = LayoutInflater.from(imageViewActivity);
        mContext1 = imageViewActivity;
        imageLoader = new ImageLoader(mContext1);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        Log.e("no. of photos in arraylist COUNT", ""+album_photos1.size());
        return album_photos1.size(); 
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
        //return 0;
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
        //return 0;
    }
    @Override
    public View getView(int position, View convertView1, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView imageView = new ImageView(mContext1);
        album_photo1 = album_photos1.get(position);
        Log.i("album photos get view ", ""+album_photo1);
        Log.i("Position in getview ", ""+position);
        imageView.setTag(album_photo1.getUrl());
        imageLoader.DisplayImage(album_photo1.getUrl(),false,imageView);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        return imageView;
        } 
}

LOG CAT:

07-27 11:32:01.478: WARN/dalvikvm(4539): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539): FATAL EXCEPTION: main
07-27 11:32:01.508: ERROR/AndroidRuntime(4539): android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.content.res.Resources.getValue(Resources.java:892)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.content.res.Resources.getDrawable(Resources.java:580)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.view.View.setBackgroundResource(View.java:7533)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at com.ijoomer.ImageViewActivity$1.onItemClick(ImageViewActivity.java:142)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.widget.Gallery.onSingleTapUp(Gallery.java:867)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.view.GestureDetector.onTouchEvent(GestureDetector.java:557)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.widget.Gallery.onTouchEvent(Gallery.java:842)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.view.View.dispatchTouchEvent(View.java:3885)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.os.Looper.loop(Looper.java:123)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at android.app.ActivityThread.main(ActivityThread.java:3647)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at java.lang.reflect.Method.invokeNative(Native Method)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at java.lang.reflect.Method.invoke(Method.java:507)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-27 11:32:01.508: ERROR/AndroidRuntime(4539):     at dalvik.system.NativeStart.main(Native Method)
07-27 11:32:01.538: WARN/ActivityManager(61):   Force finishing activity com.ijoomer/.ImageViewActivity

1 个答案:

答案 0 :(得分:2)

试试这个:

这将对你有所帮助。

public  Integer pics[] = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,R.drawable.e };


@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imgswitcher);

        iSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
        iSwitcher.setFactory(this);
        iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
        iSwitcher.setImageResource(pics[0]);

        iSwitcher.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {               


            }
        });

        Gallery gallery = (Gallery) findViewById(R.id.gallery);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
                iSwitcher.setImageResource(pics[position]);

                try {
                    WallpaperManager.getInstance(WallpaperActivity.this).setResource(pics[position]);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public class ImageAdapter extends BaseAdapter {

        private Context ctx;

        public ImageAdapter(Context c) {
            ctx = c; 
        }

        @Override
        public int getCount() {

            return pics.length;
        }

        @Override
        public Object getItem(int arg0) {

            return arg0;
        }

        @Override
        public long getItemId(int arg0) {

            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {

            ImageView iView = new ImageView(ctx);
            iView.setImageResource(pics[arg0]);
            iView.setScaleType(ImageView.ScaleType.FIT_XY);
            iView.setLayoutParams(new Gallery.LayoutParams(150, 250));
            return iView;
        }

    }

    @Override
    public View makeView() {
        ImageView iView = new ImageView(this);
        iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        iView.setLayoutParams(new 
                ImageSwitcher.LayoutParams(
                        LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        iView.setBackgroundColor(0xFF000000);
        return iView;
    }