java - 如何动态地将 android xml 布局作为参数传递给自定义数组适配器类

标签 java android

我们正在为我们的 Android 项目制作一个动态自定义数组适配器。现在我们在初始化这个自定义适配器类时需要传递动态android布局对象和其他数据源作为参数。之后一切都将是动态的。

public class CustomAdapterView extends ArrayAdapter<String> {
private Activity context;
private String[] googleProducts;
private String[] productDescription;
private Integer[] productImage;
private Layout listItemLayout;

public CustomAdapterView(Activity context, Layout listItemLayout, String[] googleProducts, String[] productDescription, Integer[] productImage) {

    super(context, R.layout.content_custom_listview, googleProducts);
    // Tring to do this
    //super(context, listItemLayout, googleProducts);
    //Error: Cannot resolve method 'super(android.app.Activity, android.text.Layout, java.lang.String[])'

    // Set Local Property
    this.context = context;
    this.googleProducts = googleProducts;
    this.productDescription = productDescription;
    this.productImage = productImage;
}

现在在 super() 方法中,我们要传递动态布局来显示自定义 ListView 。

super(context, listItemLayout, googleProducts);

//Error: Cannot resolve method 'super(android.app.Activity, android.text.Layout, java.lang.String[])'

比从任何 Activity 调用它

// Initialize
    ListView lv = (ListView) findViewById(R.id.custom_listview);
    Layout dynamicLayout = null;

    // Our Custom Adapter Object
    CustomAdapterView cav = new CustomAdapterView(this, dynamicLayout, googleProducts, productDescription, productImage);
    //Set Adapter
    lv.setAdapter(cav);

最佳答案

R.layout.content_custom_listview 是一个 int,因此该签名不会有 super 方法。您可以将 int 类型直接传递给适配器,而不是 Layout 类型。

关于java - 如何动态地将 android xml 布局作为参数传递给自定义数组适配器类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46291348/

相关文章:

java - aSmack ReconnectionManager 问题

java - 将字符串值从没有构建器的 Activity 发送到正常 Activity

java - 在 Java 中使用从一个类到另一个类的列表中的查询结果

java - 使用强制类型转换和泛型实现泛型方法

java - 如何在 RxJava 中正确转换多播可观察对象

android - OnResume 在 fragment 中多次调用

java - 如何删除布局中的底部分隔线(如线条)?

Java,当一个对象被反序列化并包含其他对象时,每个包含的对象都是一个唯一的实例吗?

java - 如何验证 lambda 函数 Mockito

android - 微调器项目未显示在微调器中,微调器 onClick 监听器无法正常工作