2015-03-02 83 views
0

在我的活動我有一個ImageView的ImageView的不正確加載

XML代碼:

<!-- FOTO LATERAL! --> 

        <ImageView 
         android:id="@+id/simbolo_raca" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 

         /> 
        <Button 
         android:id="@+id/button_save" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Ok. Seguinte Pergunta" 
         android:layout_gravity="right" 
         android:gravity="right" 
         android:paddingRight="30dip"> 
      </LinearLayout> 

這是活動時間:

public class QuestionarioActivityRaca extends Activity{ 
ImageView simbolo; 
int position; 
Button B_Save; 
List<Drawable> List_imagens = new ArrayList<Drawable>(); 
@Override 
public void onCreate(Bundle savedInstanceState) { 
simbolo = (ImageView) findViewById(R.id.simbolo_raca); 
B_Save = (Button) findViewById(R.id.button_save); 
position = 0; 
List_imagens.add(getResources().getDrawable(R.drawable.p1)); 
List_imagens.add(getResources().getDrawable(R.drawable.p2)); 
List_imagens.add(getResources().getDrawable(R.drawable.p3)); 
List_imagens.add(getResources().getDrawable(R.drawable.p4)); 
loadNewImage(position); 
B_Save.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
     loadNewImage(position); 
     position++; 
     } 
    }); 
} 
// I Use this method to load the Image 
public loadNewImage(int Page) 
{  
new Thread(new Runnable(){ 
      @Override 
      public void run(){ 
       simbolo.post(new Runnable(){ 
        @Override 
        public void run() 
        { 
         simbolo.setImageDrawable(List_imagens.get(Page)); 
        } 
       }); 
      } 
     }).start(); 
} 

我第一次調用這個方法,(位置= 0)圖像不加載。之後,圖像加載正確。

第一次加載圖像我該怎麼做?

(我無法使用android:src="@drawable/x"加載XML中的圖像),因爲圖像可能隨時不同。 EDITED!

回答

1

您發佈的代碼示例有許多問題。

就像一個資本一

Int position; 

在參數不發送與你在onclick方法寫INT:

public void onClick(View v) { 
    loadNewImage(); 
    } 

而在你的XML和代碼都幾個。

是否有一個特定的原因,你想每次運行一個新的線程完成這項任務? 如果你真的需要一個線程,你必須在方法中聲明你的int爲final。

但是,您應該在下面的代碼示例中獲得所需的結果。 您必須修改onClick以發送適合您要使用的drawable的int。

祝你好運。

public class testactivity extends ActionBarActivity { 

    ImageView simbolo; 
    int position; 
    Button B_Save; 
    List<Drawable> List_imagens = new ArrayList<>(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_testactivity); 
     simbolo = (ImageView) findViewById(R.id.simbolo_raca); 
     B_Save = (Button) findViewById(R.id.button_save); 
     position = 0; 
     List_imagens.add(getResources().getDrawable(R.drawable.ic_drawer)); 
     List_imagens.add(getResources().getDrawable(R.drawable.ic_check)); 
     List_imagens.add(getResources().getDrawable(R.drawable.ic_action_add_package)); 
     List_imagens.add(getResources().getDrawable(R.drawable.ic_action_exception)); 
     loadNewImage(position); 
     final Random rand = new Random(); 
     B_Save.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       loadNewImage(rand.nextInt(4)); 
      } 
     }); 
    } 

    public void loadNewImage(final int page) 
    { 
     simbolo.setImageDrawable(List_imagens.get(page)); 
    } 
} 
1

首先前的按鈕被點擊調用此函數與loadNewImage(position);加載初始圖像當按鈕被點擊調用此函數loadNewImage();我猜測它的工作單擊按鈕時,因爲這loadNewImage();方法沒問題,loadNewImage(int page);不好,因爲它們是兩種不同的功能。

解決你的問題,如果你想要一個整數對象使用Integer position或者與int position去,請放開線程。現在你的代碼將工作