Android 自定义Listview项目布局
我是Android开发新手。我正在尝试使我的自定义listview项目布局。我想知道如果我在五月的布局中有一个ImageView,我将如何给出图像路径 填充此ListView的活动文件Android 自定义Listview项目布局,android,layout,Android,Layout,我是Android开发新手。我正在尝试使我的自定义listview项目布局。我想知道如果我在五月的布局中有一个ImageView,我将如何给出图像路径 填充此ListView的活动文件 ListView lstItems = (ListView) findViewById(R.id.lstItems); String[] from = new String[]{"itemname", "productimage"}; int[] to = new int[]{R.id.itemname, R.
ListView lstItems = (ListView) findViewById(R.id.lstItems);
String[] from = new String[]{"itemname", "productimage"};
int[] to = new int[]{R.id.itemname, R.id.productimage};
List<HashMap<String, Object>> fillMaps = new ArrayList<HashMap<String, Object>>();
for (Item item : objects) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("itemname", item.getName());
map.put("productimage", item.getImages());
fillMaps.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.shop_listitem_view, from, to);
lstItems.setAdapter(adapter);
ListView lstItems=(ListView)findViewById(R.id.lstItems);
String[]from=新字符串[]{“itemname”,“productimage”};
int[]to=newint[]{R.id.itemname,R.id.productimage};
List fillMaps=new ArrayList();
用于(项目:对象){
HashMap=newHashMap();
put(“itemname”,item.getName());
put(“productimage”,item.getImages());
fillMaps.add(map);
}
SimpleAdapter=new SimpleAdapter(此、fillMaps、R.layout.shop\u列表项\u视图、from、to);
lstItems.setAdapter(适配器);
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="19dp"
android:layout_marginTop="22dp"
android:text="TextView"
android:textSize="14sp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/productimage" />
<ImageView
android:id="@+id/productimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="21dp"
app:srcCompat="@drawable/common_full_open_on_phone"
android:layout_alignTop="@+id/itemname"
android:layout_alignParentStart="true" />
</RelativeLayout>
在这种情况下,
simpledapter
将不适用于您。首先,您必须创建一个表示业务逻辑的类:
public class Product {
public String productName;
public String productImage;
}
其次,您必须将Glide
库添加到build.gradle
(模块应用程序)文件中。在依赖项块中添加此行:
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
}
之后,您必须创建布局文件,该文件将由适配器充气:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/your_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/your_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
最后,在您的活动中,您可以使用它:
//Create a product:
List<Product> p = new ArrayList<>();
Product prod = new Product();
prod.productName = "Name of your product";
prod.productImage = "http://your_image_url";
prod.add(prod);
YourAdapter adapter = new YourAdapter(this, p);
//and finally, set it into your listview:
listview.setAdapter(adater);
//创建一个产品:
列表p=新的ArrayList();
产品生产=新产品();
prod.productName=“产品名称”;
prod.productImage=”http://your_image_url";
产品添加(产品);
YourAdapter=新的YourAdapter(这个,p);
//最后,将其设置到listview中:
setAdapter(adater);
那么,错误是什么?我如何填写ImageView的图像路径/url?您的图像在某个服务器或项目中?在某个服务器上,我有图像的绝对路径。您可以输入一些url,以便我可以为您做一个示例吗
//Create a product:
List<Product> p = new ArrayList<>();
Product prod = new Product();
prod.productName = "Name of your product";
prod.productImage = "http://your_image_url";
prod.add(prod);
YourAdapter adapter = new YourAdapter(this, p);
//and finally, set it into your listview:
listview.setAdapter(adater);