我们正在为我们的 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/