绑定ArrayList <arraylist <>&gt;在Android中的自定义适配器中

时间:2017-03-02 08:16:57

标签: android arraylist android-arrayadapter

我想制作一个包含多个StringArrayList<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<>>

提前致谢。

1 个答案:

答案 0 :(得分:0)

您正在使用ArrayAdapter构造函数获取第3个参数作为视图资源ID,但是您正在使用数组大小​​。

super(context, R.layout.layout_geofence, geofences.size());

首先,更改构造函数调用(将第3个参数放在R.id.list_item之类,或者只使用2参数构造函数)。

然后,您应该覆盖getCount课程中的GeofenceNameListAdapter方法。

@Override
public int getCount() {
    return geofences.size();
}
相关问题