2015-10-03 75 views
2

嗨,我有一个问题,同时创造的ImageView当我点击前面的(B1)和未来(B2)按钮,它显示了一个和上一个图片来自的img阵列,但有时它显示随机图像Android这样的版面图等的ImageButton显示随机图像

public class FullTable extends Activity{ 
    int[] img={ 
     R.drawable.t1,R.drawable.t2,R.drawable.t3, 
     R.drawable.t4,R.drawable.t5,R.drawable.t6, 
     R.drawable.t7,R.drawable.t8,R.drawable.t9, 
     R.drawable.t10,R.drawable.t11,R.drawable.t12, 
     R.drawable.t13,R.drawable.t14,R.drawable.t15, 
     R.drawable.t16,R.drawable.t17,R.drawable.t18, 
     R.drawable.t19,R.drawable.t20}; 
ImageButton b1,b2; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.full_table); 
     b1 =(ImageButton) findViewById(R.id.buttonback); 
     b2=(ImageButton) findViewById(R.id.buttonnext); 
     // get intent data 
     Intent i = getIntent(); 
     // Selected image id 
     final int position = i.getExtras().getInt("id"); 
     final ImageView imageView = (ImageView) findViewById(R.id.full_image_view); 
     //imageView.setImageResource(imageAdapter.mThumbIds[position]); 
     imageView.setImageResource(img[position]); 
     b1.setOnClickListener(new OnClickListener() { 



      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 

       imageView.setImageResource(img[position]-1); 
       img[position]--; 

      } 
     }); 
     b2.setOnClickListener(new OnClickListener() { 



      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 

       imageView.setImageResource(img[position]+1); 
       img[position]++; 


      } 
     }); 
    } 

} 
+0

使用iterator这一点。 –

+0

不要想随机显示图片,或者在一个和下一个序列。 –

回答

1

在你的代码上,你根本没有递增和递减你的position变量。你的逻辑看起来有点不对。这就是为什么你得到一些随机ID。

如果你已经有img阵列与你想要的Drawable小号ID,您可以简单地设置您的position变量设置为零,并从中取出final,然后增加它直接减它。

例如,更改此:

img[position]--; 

要这样:

img[--position]; 

并设置:

int position = 0; // Default value. 

后来,ImageView#setImageResource改变方法ImageView#setImageDrawable。像:

Drawable myDrawable = getResources().getDrawable(img[position]); // Get the Drawable Object. 

imageview.setImageDrawable(myDrawable); // Set the Drawable on your ImageView. 

而且,考虑使用ListArrayList,而不是一个简单的数组的。


编辑:当你问上的评论,你可以再次正在重置您的position变量,如果你就是最后的图像上,以零开始。你可以通过获取数组的length来实现。类似:

if (position >= img.length) { 
    position = 0; 
} 
+0

感谢它解决了我的问题,一个问题在列表的末尾它显示indexoutofbound例外,我想名单再次startover请帮助 – Sky

+0

到达结束重新开始后,您可以重置你的'position'变量。我会编辑我的答案。 – Mauker

0

内部onClick()b1imageView.setImageResource(img[position]-1);:这行代码是从img阵列获得的drawablesid和由1减小该值。 但你想要的是,所以你需要修改代码以显示一张图片:

imageView.setImageResource(img[**position-1**]); 
similary modify your code for b2 onCLick 
imageView.setImageResource(img[**position+1**]); 
0

创建一个变量positionclass scope,而不是内部onCreate()方法。然后在B1 onClick()方法递减position,然后显示图像,张贴一些代码来演示相同: - >

// at class level and not inside `onCreate()` 
ImageButton b1,b2; 
int position = 0; // Initializing with default value 

// then inside onCreate() 

position = i.getExtras().getInt("id"); 
b1.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      position--; 

      if(position < 0){ 
       position = img.length; 
      } 
      imageView.setImageResource(img[position]); 

     } 
    }); 
b2.setOnClickListener(new OnClickListener() { 
    @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      position++; 

      if(position > img.length - 1){ 
       position = 0; 
      } 
      imageView.setImageResource(img[position]); 

     } 
    });