2016-02-29 93 views
0

我想显示由点击从我的文件夹绘制的三幅图像的列表。但是,images.get(i)部分给我一个错误。我不明白。如果我更换与R.drawable.handbag它的工作原理,但我的ArrayList它不工作(即使它有同样的事情)设置ImageView的新ImageView的

public void addListenerOnButton() { 

    images.add(R.drawable.handbag); // these images have been added 
    images.add(R.drawable.shoes); 
    images.add(R.drawable.tie); 

    image = (ImageView) findViewById(R.id.imageView1); 


    nextOne = (Button) findViewById(R.id.btnChangeImage);     
    nextOne.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      i++; 
      if (i > 2){ 
       i = 0; 
      } 
       image.setImageResource((images.get(i))); 

     } 

    }); 
+0

什么是你所得到的错误?发布你的logcat。 –

+0

也请你展示如何创建我和图像 –

+0

请检查您的图片在ImageView的 –

回答

3

创建图像阵列如下面

private static int images[]={R.drawable.handbag,R.drawable.shoes,R.drawabletie.tie}; 

然后如下

image.setImageResource((images[i])); 

使用或整型创建的ArrayList如下

List<Integer> images = new ArrayList<Integer>(); 
+0

嘿这个工作。你能告诉我为什么数组工作,但不是ArrayList? – qaispak

+0

数组的工作原理是简单地添加int值并检索int值。 ArrayList的doenot工作,因为某处有一个类型不匹配 –

+0

第一个 - 用一个普通阵列 – qaispak

2

刚刚尝试像这样

int images[]={R.drawable.handbag,R.drawable.shoes,R.drawabletie.tie}; 

同方式

List<Integer> images = new ArrayList<Integer>(); 
images.add(R.drawable.handbag); // these images have been added 
images.add(R.drawable.shoes); 
images.add(R.drawable.tie); 
2

改变你的ArrayList的类型,就像这样:

ArrayList<Integer> images = new ArrayList<>(); 
+1

这并没有工作,因为它仍然把它看作“对象”,而不是整数 – qaispak

+0

声明像这样 –

+0

@ankitagrawal Integer是一个对象。 – user5716019