我如何使用數組來顯示匹配的 Imageview? (How Could I use array to show match Imageview?)


問題描述

我如何使用數組來顯示匹配的 Imageview? (How Could I use array to show match Imageview?)

我得到一些像這樣的數據:{201,501,301,404,211}

我使用 ',' 來分割

      String[] messages = split(",");

所以我得到 201 501 301 404 211 ,現在我只需要三個值:201 501 301

      messages[0];  //201
      messages[1];  //501
      messages[2];  //301

但是,我有九個圖標,根據消息[0] ~ 消息[2]

只有三個圖標會顯示:

/p>

               <ImageView
                    android:id="@+id/ig_p1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="@dimen/precaution_top"
                    android:src="@mipmap/icon_101" />
               <ImageView
                    android:id="@+id/ig_p2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/precaution_top"
                    android:layout_toRightOf="@+id/ig_precaution_1"
                    android:src="@mipmap/icon_403" />
               <ImageView
                    android:id="@+id/ig_p3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/precaution_top"
                    android:layout_toRightOf="@+id/ig_precaution_2"
                    android:src="@mipmap/icon_401" />

根據我的xml,只有三個ImageView會顯示,我想顯示array[0] ~ array[2]來匹配imageView。

  ImageView p1,p2,p3;
  p1 = (ImageView)findViewById(R.id.ig_p1);
  p2 = (ImageView)findViewById(R.id.ig_p2);
  p3 = (ImageView)findViewById(R.id.ig_p3);

請幫助我,謝謝.


參考解法

方法 1:

You can set icons dynamically.

Create a function and call 3 times.

ig_p1ImageView, ig_p2ImageView, and ig_p3ImageView are image views that you use in Java side with findViewById().

void setIcon(String iconName, ImageView imageView ) {
    String fileName = "icon_" + iconName
    int iconResId = getResources().getIdentifier(fileName, "mipmap", getPackageName());
    imageView.setImageResource(iconResId);
}

setIcon(messages[0], ig_p1ImageView );
setIcon(messages[1], ig_p2ImageView );
setIcon(messages[2], ig_p3ImageView );

(by leona linEmreSURK)

參考文件

  1. How Could I use array to show match Imageview? (CC BY‑SA 2.5/3.0/4.0)

#imageview #Android #arrays






相關問題

Android imageview 未在 TableRow 中顯示 (Android imageview not displaying in TableRow)

Android中的手勢 (Gestures in Android)

GridView kéo dài ImageView không đồng nhất (GridView Stretching ImageView Non-Uniformly)

ios在表格視圖單元格中視覺調整圖像大小 (ios visually resize image in a table view cell)

IndoorAtlas SDK 2.0:使用 Picasso 和自定義 ImageView (IndoorAtlas SDK 2.0: Using Picasso with custom ImageView)

當圖像的寬度小於顯示器的寬度時,如何在視圖尋呼機內對齊圖像視圖的中心? (How to align center an image view inside a view pager when the image's width is less than the display's?)

正確旋轉 ImageView 和 FrameLayout (Rotate ImageView and FrameLayout correctly)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

Android:擴展 onTouchEvent 以進行拖動的 ImageView (Android: ImageView that extends onTouchEvent for Drag)

將旋轉的文本放在圖像視圖上 (Put rotated text on an imageview)

如何在底部設置圖像視圖 (how to set imageview in bottom)

我如何使用數組來顯示匹配的 Imageview? (How Could I use array to show match Imageview?)







留言討論