2017-05-22 10 views
-3

配列内のオブジェクト内にイメージを追加するには、あなたの助けが必要です。 私はMainActivityアイテムオブジェクトのArrayListを作成しています。 Itemクラスは、すべてのオブジェクト名とIDに配置されます。レイアウトに接続するこの画像に追加する機能を適用するには、あなたの助けが必要です。Android - 新しいアイテムを作成するときにイメージを初期化する

新しいオブジェクトを作成MainActivity -

ArrayList<Item> arrayList = new ArrayList<>(); 
arrayList.add(new Item("a", 0)); 
arrayList.add(new Item("b", 1)); 
arrayList.add(new Item("c", 2)); 
arrayList.add(new Item("d", 3)); 

Itemクラス(このクラスでは、私は、オブジェクトに画像を初期化する必要があります) -

public class Item { 

    private final String name; 
    private final int id; 

    public Item(String name, int id) { 
     this.name = name; 
     this.id = id; 
    } 

    public int getId() { 
     return id; 
    } 

    public String getName() { 
     return name; 
    } 
} 

マイレイアウト -

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
app:elevation="5dp" 
android:layout_margin="@dimen/item_margin" 
android:layout_width="wrap_content" 
android:layout_height="@dimen/item_height"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:id="@+id/text_item" 
    android:gravity="center" 
    android:layout_gravity="center" /> 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:src="@mipmap/ic_launcher" /> 

</android.support.v7.widget.CardView> 

助けてくれてありがとう!

+0

のようなあなたのアイテムのクラスを変更しますが、リソースフォルダからまたはURLからその画像を表示したいですか? –

+0

URLから@Sandeepdhiman – ironto99

答えて

0

はその

 public class Item { 

    private final String name,imageUrl; 
    private final int id; 

    public Item(String name, int id,String url) { 
     this.name = name; 
     this.id = id; 
     this.imageUrl = url; 
    } 

    public int getId() { 
     return id; 
    } 

    public String getName() { 
     return name; 
     } 
    public String getImageUrl(){ 
     return imageUrl; 
    } 

} 
+0

しかし、私はどのように画像を変更するのですか?レイアウトからデフォルトイメージをどのように変更するのですか?ありがとう! – ironto99

+0

これらのアイテムをlistviewに表示したい場所はありますか? –

+0

いいえ、その結果、それはasのカードビューで表示する必要があります – ironto99

関連する問題