如何从String ArrayList获取图像的图像资源ID

时间:2013-07-03 04:46:27

标签: java android android-layout android-intent

在我的活动中,我得到了应该以幻灯片形式显示的图像列表,

now现在应该显示的图像列表来自JSON,所以我解析了json并获得了我需要从Appfolder / app_content中显示的图像列表

我有课程:

MainActivity:

  public class MainActivity extends Activity {

        ArrayList<String> imageSlideList = new ArrayList<String>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

             //Get Intent- Extra
        Intent intent = getIntent();
            String swpImgSImgs = intent.getStringExtra("swpImgS");

         try {
             JSONArray array = new JSONArray(swpImgSImgs);
             for (int i = 0 ; i< array.length(); i++){
                String imageList = array.getString(i);
                FileOperation fo= new FileOperation();
                File   dir=fo.createFileManagerInsidePackage(AppContext.getAppContext());
                String imagePath = dir+"/"+imageList;
                imageSlideList.add(imagePath);
             }
         } catch (JSONException e) {
             e.printStackTrace();
         }  
            ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
            ImageAdapter adapter = new ImageAdapter(this);
            viewPager.setAdapter(adapter);


        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            return false;
        }

    }

适配器:

    public class SwipingImageSlidesAdapter extends PagerAdapter {



        Context context;

//I need to use files that are in json 
        private int[] GalImages = new int[] {
            R.drawable.camera,
            R.drawable.camera
        };

        SwipingImageSlidesAdapter(Context context){
            this.context=context;
        }
        @Override
        public int getCount() {
            return GalImages.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((ImageView) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(context);
            int padding = context.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
            imageView.setPadding(padding, padding, padding, padding);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageView.setImageResource(GalImages[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
        }
    }

现在我需要做什么更改才能显示来自Application-folder(App / app_content)的图像而不是使用我在MainActivity中创建{Array}的ArrayList的R.id.imageName?

3 个答案:

答案 0 :(得分:1)

你可以像arraylist一样从

获取图像
imageView.setImageResource((int)aimageSlideList.get(position));

传递适配器类

中的arraylist
private ArrayList<String> aimageSlideList=null;
private LayoutInflater mInflater=null;


    public SwipingImageSlidesAdapter (Activity context,ArrayList<String> aimageSlideList) {
        super(context, 0);

        mInflater = context.getLayoutInflater();
        this.aimageSlideList=aimageSlideList;
        }

答案 1 :(得分:1)

将路径列表传递给适配器的构造函数。

     ImageAdapter adapter = new ImageAdapter(this,imageSlideList)

然后在Adapter

      ArrayList<String> mList;
      SwipingImageSlidesAdapter(Context context,ArrayList<String> list){
        this.context=context;
        mList = list; 
    } 

然后在getView

        Bitmap b = BitmapFactory.decodeFile(mlist.get(position));
        imageView.setImageBitmap(b);

public static Bitmap decodeFile (String pathName)

将文件路径解码为位图。如果指定的文件名为null,或者无法解码为位图,则该函数返回null。

<强>参数

pathName要解码的文件的完整路径名。

<强>返回

生成的解码位图,如果无法解码,则为null。

答案 2 :(得分:0)

  1. 使用getter Setter Of Bitmap / Drawable创建新类。

  2. 现在在MainActivity中设置你的图像Bitmap / Drawable。

  3. 在您的Adapter类中,按位置获取该图像。