2017-04-26 2 views
1

何が起こっているかを簡単に説明します。 1つのimageViewを更新して別の名前の描画可能イメージに変更したい4つの選択肢があります。これは、別のアクティビティから選択した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); 

それは私が迷子にこの辺りだ、のようないくつかの本当に基本的な他の場合& 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

[OK]を、これはイン実際の作業を行うを与えることなくクラッシュ非常によく、ありがとう! – 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; 
}