演示ListFragment错误

时间:2014-04-09 16:10:42

标签: android android-listfragment

我做了一个关于ListFragment的演示但是它的错误。我能做什么?谁帮我解决了我学习安卓一个月,所以不是很好的技能

ListFragment.Class

onCreateView{
    MenuAdapter adapter = new MenuAdapter(getActivity());
    menu = new Menu(R.drawable.ic_home, "Trang Chu");
    adapter.add(menu);
    menu = new Menu(R.drawable.ic_people, "Khuyen Mai");
    adapter.add(menu);
    menu = new Menu(R.drawable.ic_photos, "Cam Nang");
    adapter.add(menu);
    menu = new Menu(R.drawable.ic_communities, "Gan Toi");
    adapter.add(menu);
    menu = new Menu(R.drawable.ic_pages, "Video");
    adapter.add(menu);
    menu = new Menu(R.drawable.ic_whats_hot, "Gioi Thieu RedSun");
    adapter.add(menu);
    setListAdapter(adapter);
    return inflater.inflate(R.layout.list, null);
}

在我的适配器中,

ArrayAdapter<Menu> {

    Context context;

    public MenuAdapter(Context context) {
        super(context, 0);
        this.context = context;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(getContext()).inflate(
                    R.layout.menulist_layout, null);
        }

        ImageView icon = (ImageView) view.findViewById(R.id.menuItem);
        icon.setImageResource(getItem(position).getImageRes());
        TextView title = (TextView) view.findViewById(R.id.menuText);
        title.setText(getItem(position).getTitle());

        return view;
    }

}

我不知道此代码中的错误。

1 个答案:

答案 0 :(得分:0)

主要问题是您在自定义适配器类中为资源ID传递0。

您需要传递布局文件的有效资源ID

 super(context, 0);

 super(context, android.R.layout.simple_list_item_1);  // or your own layout for the list item