根据条件使数组工作

时间:2015-08-04 08:01:02

标签: android

我想只根据条件加载特定图像。 但现在每当相同的图像被加载时,就会发生wat。 例如:如果变量列表取值为1,则只需加载R.drawable.shiva和R.drawable.iskon 我的java文件如下

public class ImageAdapter extends PagerAdapter {




    Context context;


    int[] GalImages = new int[]{
            R.drawable.chokkanatha1,
            R.drawable.chokkanatha2,
            R.drawable.chokkanatha3,
            R.drawable.chokkanatha4,
            R.drawable.chokkanatha5,
            R.drawable.chokkanatha6};



   public ImageAdapter(Context context,String list1){


        this.context=context;
       String list =list1;


       if(list.equals("0"))

       {
          int[] GalImages = new int[]{
                   R.drawable.chokkanatha1,
                   R.drawable.chokkanatha2,
                   R.drawable.chokkanatha3,
                   R.drawable.chokkanatha4,
                   R.drawable.chokkanatha5,
                   R.drawable.chokkanatha6
           };
       }

       if(list.equals("1"))

       {

           int[] GalImages = new int[]{
                   R.drawable.shiva,
                   R.drawable.iskon

           };

           Log.d("message", list.toString());

       }

    }



//    private int position;
//    ImageAdapter(Context context, int position) {
//        this.context = context;
//        this.position = position;
//
//        Log.d("message", "test");
//    }




    @Override
    public int getCount() {
        Log.d("message", "test");
        return GalImages.length;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {




        // Log.d("message", "position");



        ImageView imageView = new ImageView(context);

        //imageView.getItem(myViewPager.getCurrentItem());

        // int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
        // 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);
    }
}

2 个答案:

答案 0 :(得分:2)

逻辑错误,您正在创建数组的新实例,而不是更新现有实例。这里:

public ImageAdapter(Context context,String list1){


        this.context=context;
       String list =list1;


       if(list.equals("0"))

       {
          //Do not specify the int[] type before the variable name
          GalImages = new int[]{
                   R.drawable.chokkanatha1,
                   R.drawable.chokkanatha2,
                   R.drawable.chokkanatha3,
                   R.drawable.chokkanatha4,
                   R.drawable.chokkanatha5,
                   R.drawable.chokkanatha6
           };
       }

       if(list.equals("1"))

       {
           //Same here
           GalImages = new int[]{
                   R.drawable.shiva,
                   R.drawable.iskon

           };

           Log.d("message", list.toString());

       }

    }

此外,Java中标准的是在开头用小写字母命名字段和变量,然后用普通的camelcase命名,所以galImages

答案 1 :(得分:1)

您正在重新声明每个if块中的变量GalImages,因此它的范围是本地的,实例变量保持不变。

你应该这样做:

   if(list.equals("0"))
   {
      GalImages = new int[]{
               R.drawable.chokkanatha1,
               R.drawable.chokkanatha2,
               R.drawable.chokkanatha3,
               R.drawable.chokkanatha4,
               R.drawable.chokkanatha5,
               R.drawable.chokkanatha6
       };
   }

   if(list.equals("1"))
   {
       GalImages = new int[]{
               R.drawable.shiva,
               R.drawable.iskon

       };
       Log.d("message", list.toString());
   }