Java 动态构建类并将它们添加到ArrayAdapter
我有一个填充数据“NeededReplData”的列表,正在尝试将此列表添加到适配器中,但出现了问题。下面是我的Reps类和我的方法(来自另一个类)来迭代所需的repdataJava 动态构建类并将它们添加到ArrayAdapter,java,android,class,android-arrayadapter,Java,Android,Class,Android Arrayadapter,我有一个填充数据“NeededReplData”的列表,正在尝试将此列表添加到适配器中,但出现了问题。下面是我的Reps类和我的方法(来自另一个类)来迭代所需的repdata public class Reps { public int icon; public String title; public Reps() { super(); } public Reps(int icon, String title) { s
public class Reps {
public int icon;
public String title;
public Reps() {
super();
}
public Reps(int icon, String title) {
super();
this.icon = icon;
this.title = title;
}
}
List<Reps> listOfReps = new ArrayList<Reps>();
for (int i = 0; i < neededRepData.size(); i++) {
String currentRep = neededRepData.get(i);
listOfReps.add(new Reps(R.drawable.unknown_representative, currentRep));
}
我想将动态创建的customRepData[]对象传递到适配器中,我看不到在customRepData[]的构造中循环的方法,也许有更好的方法
我的扩展ArrayAdapter类如下所示:
public class LocalRepAdapter extends ArrayAdapter<Reps> {
Context context;
int layoutResourceId;
Reps data[] = null;
public LocalRepAdapter(Context context, int layoutResourceId, Reps[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
} ......
public类LocalRepAdapter扩展了ArrayAdapter{
语境;
国际布局资源;
重复数据[]=null;
公共LocalRepAdapter(上下文上下文、int-layoutResourceId、Reps[]数据){
超级(上下文、布局资源ID、数据);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
这个数据=数据;
} ......
谢谢。您被迫创建类数组
Reps customRepData[]
,因为适配器的构造函数接受类数组,但您可以轻松地将其更改为
public LocalRepAdapter(Context context, int layoutResourceId, ArrayList<Reps> list)
是的,你是对的。更改构造函数成功了,谢谢!
public LocalRepAdapter(Context context, int layoutResourceId, ArrayList<Reps> list)
LocalRepAdapter adapter = new LocalRepAdapter(this, R.layout.mylist, listOfReps);