【发布时间】:2017-07-21 20:40:58
【问题描述】:
我想制作一个包含多个String 和一个ArrayList<ArrayList<GeofenceDetails>> 的自定义适配器。
private ArrayList<Geofences> geofencesArrayList;
geofencesArrayList = new ArrayList<Geofences>();
assetNameArrayList = new ArrayList<ArrayList<GeofenceDetails>>();
geofencesArrayList.add(new Geofences(id,name,type,assetNameArrayList));
adapter = new GeofenceNameListAdapter(getActivity(),geofencesArrayList);
adapter.notifyDataSetChanged();
geoListView.setAdapter(adapter);
这是我的自定义适配器
public class GeofenceNameListAdapter extends ArrayAdapter<ArrayList<Geofences>> {
private final Activity context;
private final ArrayList<Geofences> geofences;
//song list and layout
private LayoutInflater view;
// private static LayoutInflater inflater=null;
public static final String PREFERENCES = "RPREFS";
SharedPreferences sharedprefs = null;
private String ord_id = null;
private String email = null;
public GeofenceNameListAdapter(Activity context,
ArrayList<Geofences> geofences) {
super(context, R.layout.layout_geofence, geofences.size());
// TODO Auto-generated constructor stub
finder_sharedprefs = getContext().getSharedPreferences(PREFERENCES, getContext().MODE_PRIVATE);
finder_ord_id = finder_sharedprefs.getString("org_id", null);
finder_email = finder_sharedprefs.getString("email", null);
this.context = context;
this.geofences = geofences;
}
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.layout_geofence, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.name);
TextView txtType = (TextView) rowView.findViewById(R.id.type);rowView.findViewById(R.id.delete);
txtTitle.setText(geofences.get(position).getTitle());
txtType.setText(geofences.get(position).getType());
return rowView;
}
我可以在geofencesArrayList 上看到所有内容。这里我打印geofencesArrayList的数据
for (int l = 0; l<geofencesArrayList.size(); l++) {
Log.d("GEOFENCE_DETAILS", geofencesArrayList.get(l).getId().toString());
Log.d("GEOFENCE_DETAILS", geofencesArrayList.get(l).getTitle().toString());
Log.d("GEOFENCE_DETAILS", geofencesArrayList.get(l).getType().toString());
// Log.d("GEOFENCE_DETAILS", geofencesArrayList.get(l).getPoints().toString());
Log.d("GEOFENCE_DETAILS", geofencesArrayList.get(l).getEnforcedData().toString());
}
但问题是适配器不显示任何数据。我认为我的自定义适配器出了点问题,但我无法弄清楚。
感谢您的建议。告诉我如何创建自定义适配器来显示多个字符串和ArrayList<ArrayList<>>。
提前致谢。
【问题讨论】:
-
关于代码质量的两件事:A) 命名......在你的名字中输入确切的类型是没有意义的 B) 你也确实不想要使用特定的您的类型的实现类;你最好直接写
private List<Geofences> geofences = new ArrayList<>();。更容易阅读和使用! -
没有数据显示,因为您在适配器中发送了错误的数组列表。您使用 ArrayList
定义了 GeofenceNameListAdapter 构造函数,而使用 ArrayList >() 初始化适配器。 -
感谢@GhostCat。我会这样做的。
-
我没有看到
setAdapter方法调用。你有吗? -
@Alvi 我没听懂你。我正在我的适配器中发送 geofencesArrayList ,这是确切的。
标签: android arraylist android-arrayadapter