2016-10-24 3 views
0

私はいくつかの画像と関連テキストを表示しようとしていますが、ImageButton onclickイベントを介して正しく循環させる方法を理解できないようです。私がしたいのは、ボタンをクリックするたびに次の画像に行くことだけです。終わりに達したら、やり直してください。ClickでLinkedHashMapのデータを循環させる方法

ここに私のコードです。おそらくもっともエレガントではないかもしれませんが、 "currentid"が何らかの形で変わってforeachループで現在のイメージが見つからないことを除いて、動作するようです。

public class Fundamentals extends Activity { 

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

    //Ojbects for forward and back buttons 
    ImageButton forwardButton = (ImageButton) findViewById(R.id.forward_button); 

    //image object 
    ImageView imageview = (ImageView) findViewById(R.id.fundamentals_image); 

    //Object for picture text 
    TextView textview = (TextView) findViewById(R.id.fundamentals_text); 

    //Images and associated text 
    Map<Integer, String> fundamentalPics = new LinkedHashMap<Integer, String>(); 
    fundamentalPics.put(R.drawable.fund1, "some interesting text"); 
    fundamentalPics.put(R.drawable.fund2, "more interesting text"); 
    fundamentalPics.put(R.drawable.fund3, "and more"); 

    //Set initial image 
    Map.Entry<Integer,String> pic = (Map.Entry<Integer, String>) fundamentalPics.entrySet().iterator().next(); 
    imageview.setImageResource(pic.getKey()); 
    textview.setText(pic.getValue()); 

    imageview.setTag(fundamentalPics); 
    } 

    public void setImage(View v) { 
    //Object for picture text 
    TextView textview = (TextView) findViewById(R.id.fundamentals_text); 

    //Object for picture image 
    ImageView imageview = (ImageView) findViewById(R.id.fundamentals_image); 


    //get current id 
    Integer currentid = imageview.getId(); 

    Map<Integer, String> fundamentalPics = (LinkedHashMap<Integer, String>) imageview.getTag(); 

    Boolean foundImage = false; 
    for (Map.Entry<Integer, String> pic : fundamentalPics.entrySet()) 
    { 
     if (foundImage.equals(true)) { 
      textview.setText(pic.getValue()); 
      imageview.setImageResource(pic.getKey()); 
      break; 
     } 
     //cycle until we find current image 
     if (pic.getKey().equals(currentid)) { 
      foundImage = true; 

      if (!fundamentalPics.entrySet().iterator().hasNext()) { 
       //we've reached the end. go to beginning.... 
       Map.Entry<Integer,String> item = (Map.Entry<Integer, String>) fundamentalPics.entrySet().toArray()[0]; 
       textview.setText(item.getValue()); 
       imageview.setImageResource(item.getKey()); 
      } 
     } 
    } 
} 
+0

image.getTagから取得するのではなく、コールバックメソッドにアクセスできるように、インスタンスレベルでそのオブジェクトを保持しないでください。インスタンスレベルで現在のイメージインデックスを保持することもできます。ロジック。ちょっとした提案。 – sampathpremarathna

答えて

0

は、私はあなたが間違って比較していると思う:あなたのマップでは

if (pic.getKey().equals(currentid)) 

は、あなたがイメージのidがある描画可能なIDを格納し、そう、のgetKey()は描画イメージのIDを返し、currentidがありimageviewのidで、そのimageviewのレイアウトxmlで定義されています(イメージのdrawable idではありません)。どちらも整数ですが、違いを考えてみましょう。一つはR.drawable。*から、もう一つはR.id. *からです。一つはそれぞれの画像のidで、もう一つはimageviewのIDです。

imageviewは画像がレンダリングされるコンテナで、このコンテナのIDと画像IDを比較していますが、明らかに一致しません。

残念ながら、イメージビューのgetDrawableId()はありません。もちろん

if(imageview.getDrawable()==getResources().getDrawable(R.drawable.fund1) || imageview.getDrawable()==getResources().getDrawable(R.drawable.fund2) || imageview.getDrawable()==getResources().getDrawable(R.drawable.fund3)){ 
    //do work here 
} 

、次のことができます。

あなたは

Drawable myDrawable = imageview.getDrawable(); 

のような描画可能なあなたのような描画可能リソースとそれを比較することができます得ることができます:回避策は、すべての描画可能なIDと比較することです比較するためのより良い方法を使用して、私は例を与えた。それが役に立てば幸い !

関連する問題