0

我想填充與simpleadapter與HashMap的建立navigationDrawer在Android的菜單。如何使用simpleadapter填充navigationDrawer android?

這裏是我的代碼:我不知道如何爲每個項目建立我的抽屜式導航菜單每個條目檢索TXT和IMG的價值。

你能對我的代碼,請看看嗎?

我知道這是可能做到這一點使用這些行:

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

但我需要爲了設置字體爲每個項目實施getView。

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); 
+0

使用自定義適配器 – Raghunandan

回答

1

這是非常簡單的解決方案(這就是爲什麼你沒有得到任何答案)和Raghunandan是正確的:你必須做自己編碼自定義適配器

看看this guide。您必須覆蓋此方法:

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

通過這樣做,您可以爲每個項目設置一個TypeFace。

+0

是的,這就是我做。請看我的代碼。但我不知道我能做些什麼來設置文本。 (通過替換在我的代碼???????)不幸 – wawanopoulos

+0

@wawanopoulos不能,你這樣做是不對的。通過自定義適配器我的意思是創建一個類,如:public class YourAdapter擴展SimpleAdapter {...}。 – JJ86