2017-05-22 2 views
-3

Ich brauche Ihre Hilfe, um ein Bild innerhalb eines Objekts in Array hinzuzufügen. Ich habe meine MainActivity, die ArrayList Item Objekte erstellen. Die Item-Klasse wird für jeden Objektnamen und jede ID angegeben. Jetzt brauche ich deine Hilfe, um irgendeine Funktion anzuwenden, die zu diesem Bild hinzufügt, die mit dem Layout verbinden.Android - initialisieren Bild von beim Erstellen neuer Artikel

MainActivity, die neue Objekte erstellen -

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-Klasse (In dieser Klasse muss ich das Bild auf das Objekt initialisieren) -

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; 
    } 
} 

Mein Layout -

<?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> 

Danke für jede Hilfe!

+0

tun Sie das Bild von Ressourcenordner oder von einer URL angezeigt werden soll? –

+0

@Sandeepdhiman von einer URL – ironto99

Antwort

0

Ändern Sie bitte Ihre Artikel Klasse wie die

 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

Aber wie ändere ich das Bild? Wie ändere ich das Standardbild vom Layout? Vielen Dank ! – ironto99

+0

wo möchten Sie diese Elemente in Listview anzeigen? –

+0

Nein, im Ergebnis muss es in der als cardview – ironto99

Verwandte Themen