Android:Fragment in NavigationDrawer

时间:2015-07-31 20:26:00

标签: android listview android-fragments

I have a strange problem I created a navigation drawer and a fragment. when I insert a list view in the fragment it appears normally but if I remove that list view and insert anything else like text view or image view nothing appears in the fragment when I tap its item in the navigation drawer.

Note: I know that the list view need to be initialized in the java file too and I did that. is the text view or image view needs something like that? may be the error here... any help please....

public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    List<String> content = new ArrayList<>();
    content.add("list");
    content.add("list");
    content.add("list");
    content.add("list");
    content.add("list");
    content.add("list");
    content.add("list");
    ListView listView = (ListView) getActivity().findViewById(R.id.listView);
    listView.setAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, content));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main by replacing fragments
    Fragment fragment;
    switch(position){
        case 0: //search//todo
            break;
        case 1: //stats
            break;
        case 2: //my account //todo
            break;
        case 3: //settings //todo
            break;
        case 4: //location //todo
            fragment = getFragmentManager().findFragmentByTag(LocationFragment.TAG);
            if (fragment == null) {
                fragment = new LocationFragment();
            }
            getFragmentManager().beginTransaction().replace(R.id.container, fragment, LocationFragment.TAG).commit();
            break;
    }

}

0 个答案:

没有答案