0

I am trying to populate a navigationDrawer Android menu with simpleadapter build with hashmap.

Here is my code : I don't know how to retrieve value of txt and img for each item to build each item of my navigation drawer menu.

Could you have a look on my code please ?

I know that is possible to do this using these lines :

SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), listinfo, R.layout.drawer_list_item, from, to);
mDrawerList.setAdapter(adapter); 

But i need to implement getView in order to set TypeFace for each item.

    String[] names = new String[]{
                "Home",
                "International",
                "National",
                "High Tech",
                "Sports",
                };

        /*Array of Images*/
        int[] image = new int[] {
                R.drawable.ic_action_home,
                R.drawable.ic_action_globe,
                R.drawable.ic_action_feed, 
                R.drawable.ic_action_tv,
                R.drawable.ic_action_ball,
        };

        List<HashMap<String, String>> listinfo = new ArrayList<HashMap<String, String>>();
        listinfo.clear();
        for(int i=0;i<names.length;i++){
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("name", names[i]);
            hm.put("image", Integer.toString(image[i]));
            listinfo.add(hm);
        }

        // Keys used in Hashmap
        final String[] from = { "image", "name" };
        int[] to = { R.id.img, R.id.txt };

// The 2 followings lines works fine but i want to implement getView in order to set Typeface on my text
//      SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), listinfo, R.layout.drawer_list_item, from, to);
//      mDrawerList.setAdapter(adapter);


        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), listinfo, R.layout.drawer_list_item, from, to){
            @Override
            public View getView(int pos, View convertView, ViewGroup parent){
                View v = convertView;
                if(v== null){
                    LayoutInflater vi = (LayoutInflater)getSystemService(getBaseContext().LAYOUT_INFLATER_SERVICE);
                    v=vi.inflate(R.layout.drawer_list_item, null);
                }
                TextView tv = (TextView)v.findViewById(R.id.txt);
                tv.setText(??????????);
                tv.setTypeface(faceBold);
                return v;
            }
        };
        mDrawerList.setAdapter(adapter);
wawanopoulos
  • 9,614
  • 31
  • 111
  • 166

1 Answers1

1

The solution it's really simple (this is why you don't get any answer) and Raghunandan is right: you have to do code your own Custom Adapter.

Take a look at this guide. You have to override this method:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ...
    // Your code for set different TypeFace go here
}

By doing this, you can set a TypeFace for each item.

JJ86
  • 5,055
  • 2
  • 35
  • 64
  • Yes, it is what i am done. See my code please. But i don't know what i can do for setting text. (by replacing ??????? in my code ) – wawanopoulos Oct 16 '13 at 10:06
  • @wawanopoulos unfortunately not: you are doing this wrong. By Custom Adapter i mean to create a class like: public class YourAdapter extends SimpleAdapter {...} . – JJ86 Oct 16 '13 at 10:12