【问题标题】:How to populate a navigationDrawer android using simpleadapter?如何使用 simpleadapter 填充 navigationDrawer android?
【发布时间】:2013-10-16 07:51:32
【问题描述】:

我正在尝试使用带有 hashmap 的 simpleadapter 构建 navigationDrawer Android 菜单。

这是我的代码:我不知道如何为每个项目检索 txt 和 img 的值以构建导航抽屉菜单的每个项目。

你能看看我的代码吗?

我知道可以使用这些行来做到这一点:

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

但我需要实现 getView 以便为每个项目设置 TypeFace。

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

【问题讨论】:

    标签: android navigation-drawer simpleadapter android-navigation


    【解决方案1】:

    解决方案非常简单(这就是您没有得到任何答案的原因),Raghunandan 是对的:您必须编写自己的自定义适配器

    看看this guide。你必须重写这个方法:

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

    通过这样做,您可以为每个项目设置一个字体。

    【讨论】:

    • 是的,这就是我所做的。请看我的代码。但我不知道我能做些什么来设置文本。 (通过替换 ????????? 在我的代码中)
    • @wawanopoulos 不幸的是:你做错了。通过自定义适配器,我的意思是创建一个类,如: public class YourAdapter extends SimpleAdapter {...} .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多