片段未显示的Android ListView项目

时间:2017-11-09 05:27:04

标签: android listview android-fragments

我正在关注Bucky的教程(我不确定你们现在是他们)关于ListView,还编辑了我的代码,因为它在片段中。我设法消除错误,但列表项没有显示。

这是片段代码:

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_master,container,false);

    String[] foods = {"Shanghai","Spaghetti"};
    ListAdapter lAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,foods);

    ListView masterListView = (ListView) view.findViewById(R.id.masterListView);
    masterListView.setAdapter(lAdapter);

    masterListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String doof = String.valueOf(parent.getItemAtPosition(position));
                    Toast.makeText(getActivity(),doof,Toast.LENGTH_LONG).show();
                }
            });

    return view;
}

这是xml:

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/masterListView" />

由于

我忘了提到我使用抽屉和标签布局。我不知道这是否重要,但这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    ViewPager vp_pages= (ViewPager) findViewById(R.id.vp_pages);
    PagerAdapter pagerAdapter=new FragmentAdapter(getSupportFragmentManager());
    vp_pages.setAdapter(pagerAdapter);

    TabLayout tab_pages= (TabLayout) findViewById(R.id.tab_pages);
    tab_pages.setupWithViewPager(vp_pages);

}

public class FragmentAdapter extends FragmentPagerAdapter {
    public FragmentAdapter(FragmentManager supportFragmentManager) {
        super(supportFragmentManager);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return new tab1();
            case 1:
                return new tab2();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            //
            //Your tab titles
            //
            case 0:return "Tab1";
            case 1:return "Tab2";
            default:return null;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

而不是:

ListAdapter lAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,foods);

使用:

ListAdapter lAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,foods);

因为getActivity()提供了片段所在活动的上下文,而getContext()获取了listview适配器所在片段的上下文。

答案 1 :(得分:0)

放置:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();
    }
});

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

ViewPager vp_pages= (ViewPager) findViewById(R.id.vp_pages);
PagerAdapter pagerAdapter=new FragmentAdapter(getSupportFragmentManager());
vp_pages.setAdapter(pagerAdapter);

TabLayout tab_pages= (TabLayout) findViewById(R.id.tab_pages);
tab_pages.setupWithViewPager(vp_pages);

之间:

    @Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    super.onViewCreated(view, savedInstanceState);

//  Put it here.

}