Java Android-创建新项目时从初始化图像
我需要你的帮助在数组中的任何对象中添加图像。 我的主要活动是创建项目对象的ArrayList。Item类将添加到每个对象名称和id。现在,我需要您的帮助来应用添加到此图像并连接到布局的任何函数 创建新对象的主活动-Java Android-创建新项目时从初始化图像,java,android,xml,android-imageview,Java,Android,Xml,Android Imageview,我需要你的帮助在数组中的任何对象中添加图像。 我的主要活动是创建项目对象的ArrayList。Item类将添加到每个对象名称和id。现在,我需要您的帮助来应用添加到此图像并连接到布局的任何函数 创建新对象的主活动- ArrayList<Item> arrayList = new ArrayList<>(); arrayList.add(new Item("a", 0)); arrayList.add(new Item("b", 1)); arrayList.add(new
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));
我的布局-
<?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>
谢谢你的帮助 这样修改您的item类
您想从资源文件夹或url显示该图像吗?@Sandeepdhiman从url显示但我如何更改图像?如何更改布局中的默认图像?谢谢否,在结果中,它需要显示在as CardView中。请遵循此链接
<?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>
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;
}
}