2015-10-03 116 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]); 

     } 
    });