Listpager片段里面的Listview没有显示

时间:2014-12-04 20:57:36

标签: java android listview layout android-fragments

我试图在片段内添加listview,女巫也在tabhost中。应用程序没有崩溃或任何东西,但列表视图没有显示任何内容。 这是我的代码:

tabPager的类

public class TabPagerAdapter extends FragmentPagerAdapter {

    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
            case 0:
                return new FragmentHome();
            case 1:
                return new FragmentBadges();
            case 2:
                return new FragmentBenefits();
        }

        return null;
    }

以下是我想要listview的片段:

    public class FragmentBadges extends Fragment {

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {

                View rootView = inflater.inflate(R.layout.fragment_badges_list, null);

                BadgesListAdapter adapter = new BadgesListAdapter(this.getActivity(), getCurrentBadges());
                ListView badgeslist = (ListView) rootView.findViewById(R.id.badges_list);
                badgeslist.setAdapter(adapter);
                badgeslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        //TODO do some
                    }
                });

                return rootView;
            }

以下是适配器:

public class BadgesListAdapter extends ArrayAdapter<String> {
    private final Activity context;
    private List<Badge> badges;

    public BadgesListAdapter(Activity context, List<Badge> badges) {
        super(context, R.layout.fragment_badges_listrow);
        this.context = context;
        this.badges = badges;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.fragment_badges_listrow, null, true);

        TextView txtTitle = (TextView) rowView.findViewById(R.id.text);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);

        txtTitle.setText(badges.get(position).getName());

        imageView.setImageResource(badges.get(position).getDrawableValue());

        return rowView;
    }

调试时我发现badgesListAdapter中的getView方法从未被调用过。 我有另一个应用程序的相同代码,但没有tabpager,那里一切都很完美...

2 个答案:

答案 0 :(得分:0)

检查getCurrentBadges()方法是否不返回空列表。如果是,则ListView中没有数据显示。

答案 1 :(得分:0)

我能够通过在FragmentBadges中继承ListFragment来实现它,遵循本教程https://www.youtube.com/watch?v=heR4zhj_wV0