2017-04-26 43 views
1

將簡要解釋發生了什麼。我想更新一個imageView,將其更改爲不同名稱的可繪製圖像。這裏只有四種選擇,這是因爲我正在從一個微調器中提取數據,微調器可以從其他活動中選擇4個國家。然後這將imageView更改爲所述國家的自定義可繪製圖像。根據字符串中的數據更改圖像視圖的顯示圖像(如單詞)

到目前爲止,我不確定我要如何做到這一點,嘗試了幾個非常基本的if else語句,但他們是非常不正確的。

我會後一些什麼,我有如下:

public class WorldImage extends AppCompatActivity { 

    TextView textCountry; 

    private String stringCountry; 

    ImageView imageCountry; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_worldimage); 

     Intent aIntent = getIntent(); 
     stringCountry = aIntent.getStringExtra("movetoImageActivity"); 

     textCountry= (TextView) findViewById(R.id.movetoviewCountry); 
     imageCountry = (ImageView) findViewById(R.id.imageCountry); 

這是在這裏我迷路了,一些很基本的,如果& else if語句,如:

 if (textCountry.getText().toString().equals("Europian Union")) 
{ 
    imageCountry.setImageResource(R.drawable.eu_flag); 
} 

會是什麼在這裏建議嗎?

回答

1

如何打印日誌?

Intent aIntent = getIntent(); 
    stringCountry = aIntent.getStringExtra("movetoImageActivity"); 

    textCountry= (TextView) findViewById(R.id.movetoviewCountry); 
    imageCountry = (ImageView) findViewById(R.id.imageCountry); 



    Log.e("TEST", "stringCountry : " + stringCountry); 
    Log.e("TEST", "textCountry: " + textCountry.getText().toString()); 

    if (stringCountry.equals("Europian Union")){ 
     Log.e("TEST", "Europian Union"); 
     imageCountry.setImageResource(R.drawable.eu_flag); 
    }else if (stringCountry.equals("Something else")){ 
     Log.e("TEST", "Something else"); 
     imageCountry.setImageResource(R.drawable.something); 
    } 
+0

嗯,這似乎並沒有任何工作,死機,甚至沒有給一個調試錯誤 – Hervel

+0

好吧,這確實在-工作其實,很好,謝謝! – Hervel

0

您可以使用switch語句是這樣的:

switch(stringCountry){ 
    case "Europian Union": 
      imageCountry.setImageResource(R.drawable.eu_flag); 
      break; 
    case "United States": 
      imageCountry.setImageResource(R.drawable.us_flag); 
      break; 
    default: 
      // optional. Use if all comparisons above fail and you want to handle it 
     break; 
}