Search code examples
androidhashmapadapter

custom adapter with ArrayList<HashMap<String, List<String>>>


Its a really complicated question, but I hope someone can help me.

I want to make a custom adapter that can handle

Array List mentioned in the title

Currently I am doing this, but its not even going into getView(...) method.

public class EventsAdapter extends BaseAdapter{

ArrayList<HashMap<String, List<String>>> eventList;

private Context context;
private int resource;

   private static final String TAG_TITLE = "e_title";


public EventsAdapter(Context context, int resId,ArrayList<HashMap<String, List<String>>> eventList)
{

    this.context = context;
    this.resource = resId;
    this.eventList = eventList;
    Log.v("chk", "1");

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    View event = convertView;

    TextView title, desc, date, time, venue;

    HashMap<String, List<String>> hm = eventList.get(position);



    List<String> items = hm.get(TAG_TITLE);

    Typeface font = Typeface.createFromAsset(context.getAssets(), "Ubahn.ttf"); 

    if( event == null )
    {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        event = inflater.inflate( resource , parent, true  );
        event.setTag(items.get(position));
    }

    title = (TextView) event.findViewById( R.id.etitle);
    desc = (TextView) event.findViewById( R.id.edesc );
    date = (TextView)event.findViewById(R.id.edate);
    time = (TextView)event.findViewById(R.id.etiming);
    venue = (TextView)event.findViewById(R.id.elocation);

    title.setTypeface(font);



    System.out.print(items.get(0).toString());

    title.setText(items.get(0).toString());
    desc.setText(items.get(1).toString());
    date.setText(items.get(2).toString());
    time.setText(items.get(3).toString());
    venue.setText(items.get(4).toString());

    return event;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 5;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return eventList.get(position).get(position).get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

}

Any help would be greatly appreciated.

Here is how I am filling the data in the Array List

    for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                    String title = c.getString(TAG_TITLE);
                    String description = c.getString(TAG_DESC);
                    String date = c.getString(TAG_DATE);
                    String time = c.getString(TAG_TIME);
                    String venue = c.getString(TAG_VENUE);

                    // creating new HashMap

                    HashMap<String, List<String>> map = new HashMap<String, List<String>>();

                    List<String> el = new ArrayList<String>();
                    el.add(title);
                    el.add(description);
                    el.add(date);
                    el.add(time);
                    el.add(venue);

                    // adding each child node to HashMap key => value
                    map.put(TAG_TITLE, el);
                   // map.put(TAG_DESC, description);
                    //muap.put(TAG_DATE, date);

                    // adding HashList to ArrayList
                    eventList.add(map);
                }

and then here I am setting the adapter

EventsAdapter adapter = new EventsAdapter(getActivity(), R.layout.events_list_item, eventList);



                lv.setAdapter( adapter);

Solution

  • I think it would be useful to consider when you are filling that list.

    a good design pattern is to create the data as an empty hashmap, establish the adapter with that map, declare the listview( or whatever ) and then assign the adapter with the empty data set. later, fill the hashmap, and then adapter.notifydatasetchanged

    I declare these feilds:

        //array of places filtered by keyword
    List<Place> places = new ArrayList<Place>( );
        //spinner of places filtered by keyword
    Spinner placesSpinner;
    //adapter for spinner
    private PlacesSpinnerAdapter placesSpinnerAdapter;
    

    in onCreate:

    //this is for a spinner, but same difference
    placesSpinner = (Spinner)findViewById( R.id.placesSpinner );
    placesSpinnerAdapter = new PlacesSpinnerAdapter( this, 
                               android.R.layout.simple_spinner_dropdown_item, places );
    placesSpinner.setAdapter( placesSpinnerAdapter );
    

    and somewhere in the onPostExecute method of an AsyncTask you have no interest in

    places.clear( );
    places.addAll( result.results );
    placesSpinnerAdapter.notifyDataSetChanged( );
    placesSpinner.performClick( );
    

    and lastly, here's a difference I see: adapters do Lists, I've never had a good time passing them hashmaps... do a map.keySet() or map.values(); as you hand the data over to the adapter; I know for certain the very standard pattern I just described does not work if the data set is hashmaps

    gl hf