1

I want to make a custom adaper which will contain multiple String and an 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);

Here is my custom 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;
}

I can see everything on geofencesArrayList. Here I am printing data of 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());
}

But the problem is Adapter doesn't display any data. I think something went wrong in my custom adapter but I couldn't figure out it.

I appreciate you advice. Let me know How can I create a custom adapter to show multiple strings and ArrayList<ArrayList<>>.

Thanks in advance.

anuradha
  • 692
  • 1
  • 9
  • 22
  • 1
    Two things on code quality: A) naming ... there is no point in putting the exact type into your names B) you also do **not** want to use the specific implementation class on your type; you better write `private List geofences = new ArrayList<>();` directly. Much easier to read and use! – GhostCat Mar 02 '17 at 08:19
  • No data is showing because you are sending wrong arraylist in your adapter. You defined the GeofenceNameListAdapter constructor with ArrayList whereas initializing the adapter with ArrayList>(). – Alvi Mar 02 '17 at 08:20
  • Thanks @GhostCat. I will do that. – anuradha Mar 02 '17 at 08:22
  • I don't see `setAdapter` method call. Do you have it? – Ircover Mar 02 '17 at 08:22
  • @Alvi I didn't get you. I am sending geofencesArrayList in my adapter which is the exact one. – anuradha Mar 02 '17 at 08:23
  • @Ircover Yes I have geoListView.setAdapter(adapter); But I didn't add it to the code here. – anuradha Mar 02 '17 at 08:25
  • @anuradha Glad to hear that ;-) – GhostCat Mar 02 '17 at 08:32
  • try removing the line adapter.notifyDataSetChanged(); notifyDataSetChanged() is mainly used when BaseAdapter class in extended. Check this link for more : http://stackoverflow.com/a/5092426/2632867 – Alvi Mar 02 '17 at 08:33

1 Answers1

0

You are using ArrayAdapter constructor that gets 3-rd parameter as view resource ID, but you are putting there array size.

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

First of all, change constructor call (place 3-rd parameter something like R.id.list_item or just use 2-parameter constructor).

Then you should override getCount method in your GeofenceNameListAdapter class.

@Override
public int getCount() {
    return geofences.size();
}
Ircover
  • 2,406
  • 2
  • 22
  • 42