2017-04-12 33 views
-1

我創建了一個連接到列表視圖的片段來顯示一個簡單的表格視圖。ListView不顯示在我的應用程序

這裏是我的片段:

public class ConfRoomListFragment extends Fragment { 
    ApplicationController activity; 
    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry", 
      "WebOS","Ubuntu","Windows7","Max OS X"}; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_conf_room_list, container, false); 
     activity = (ApplicationController) getActivity(); 
     ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), 
       R.layout.activity_listview, mobileArray); 

     ListView listView = (ListView)view.findViewById(R.id.conf_room_directory); 
     listView.setAdapter(adapter); 

     return view; 
    } 
} 

這裏是我的fragment_conf_room_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

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

</LinearLayout> 

這裏是我的activity_listview.xml

<?xml version="1.0" encoding="utf-8"?> 
<!-- Single List Item Design --> 

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/label" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dip" 
    android:textSize="16dip" 
    android:textStyle="bold" > 
</TextView> 

當我運行我的應用程序,都顯示是一個空白的屏幕。我創建了列表視圖並添加了適配器。似乎沒有任何可變的問題,所以我不確定問題是什麼。

編輯 我已經將我的片段活動: ApplicationController.java

/* Instantiate all Fragments needed*/ 
    directoryViewFragment = new DirectoryViewFragment(); 
    directoryListFragment = new DirectoryListFragment(); 
    officeViewFragment = new OfficeViewFragment(); 
    officeListFragment = new OfficeListFragment(); 
    confRoomListFragment = new ConfRoomListFragment(); 
    settingsFragment = new SettingsFragment(); 
    directionsFragment = new DirectionsFragment(); 
    airportInfoFragment = new AirportInfoFragment(); 
    publicationsFragment = new PublicationsFragment(); 
    practiceGroupListFragment = new PracticeGroupListFragment(); 
    practiceGroupFragment = new PracticeGroupFragment(); 

正如你可以看到我實例化我的片段。

這是在選擇發生:

private class SlideMenuClickListener implements ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
          long id) { 
     // display view for selected nav drawer item 
     displayView(position, false); 
     System.out.println(position); 
     current_position = position; 
     invalidateOptionsMenu(); 
    } 
} 

/** 
* Diplaying fragment view for selected nav drawer list item 
*/ 
private void displayView(int position, boolean firstClick) { 
    // update the main content by replacing fragments 
    if (firstClick) { 
     Fragment fragment = directoryViewFragment; 
     currentPanel = Panel.HomeView; 
     getFragmentManager().beginTransaction() 
       .replace(R.id.fragmentHolder, fragment) 
       .addToBackStack(null) 
       .commit(); 
     getFragmentManager().executePendingTransactions(); 
    } else { 
     new ViewSelectionListener(this).onItemClick(null, null, position, 0); 
    } 

    // update selected item and title, then close the drawer 
    setTitle(navMenuTitles[position]); 
    drawerLayout.closeDrawer(navList); 
} 

我知道它的工作原理,因爲setTitle功能的工作原理和片段的標題是Conference Rooms

這是我ViewSelectionListener.java,我在那裏更新片段經理展示我的片段:

} else if (position == 2) { 

     //Conference Rooms 
     activity.setCurrentPanel(Panel.ConfRoomDirectory); 
     FragmentTransaction transaction = activity.getFragmentManager().beginTransaction() 
       .setCustomAnimations(Tags.TRANSITION_IN, Tags.TRANSITION_OUT) 
       // TODO If there are other fragments to hide, put them here! 
       .hide(activity.getDirectoryViewFragment()) 
       .hide(activity.getPublicationsFragment()) 
       .hide(activity.getSettingsFragment()) 
       .hide(activity.getAirportInfoFragment()) 
       .hide(activity.getDirectionsFragment()) 
       .hide(activity.getPracticeGroupListFragment()) 
       .hide(activity.getPracticeGroupFragment()) 
       .hide(activity.getOfficeViewFragment()) 
       .show(activity.getConfRoomListFragment()); 

     if (activity.isPhone) { 
      transaction.hide(activity.getDirectoryListFragment()); 
      transaction.hide(activity.getOfficeListFragment()); 
     } else if (activity.isPortrait()) { 
      transaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_left); 
      if (DirectoryViewFragment.leftPanelIsShowing) { 
       transaction.remove(activity.getDirectoryListFragment()); 
       DirectoryViewFragment.leftPanelIsShowing = false; 
      } 
      if (OfficeViewFragment.leftPanelIsShowing) { 
       transaction.remove(activity.getOfficeListFragment()); 
       OfficeViewFragment.leftPanelIsShowing = false; 
      } 
      if (PracticeGroupFragment.leftPanelIsShowing) { 
       transaction.remove(activity.getPracticeGroupListFragment()); 
       PracticeGroupFragment.leftPanelIsShowing = false; 
      } 
     } 
     transaction.commit(); 
    } 

正如你所看到的,我隱藏所有其他的片段,並表明我想要的片段。但是,只有標題得到更新,片段不會顯示。所以問題不在於活動,還有一些我沒有看到的錯誤。

+0

,你誇大你的片段?發佈該代碼也 –

+0

@AnandSavjani它在我附加的第一個代碼塊 –

+0

中添加一些日誌到ConfRoomListFragment的onCreateView()方法,例如, :Log.i(「Tag」,「onCreateView called」)並查看它是否被調用。如果沒有,那麼你有一些邏輯錯誤。 – Sergey

回答

0

檢查您的片段onCreateView()方法真的叫。如果沒有,那麼你的片段不會被添加到活動中。確保在調用FragmentTransaction的show(ConfRoomListFragment)方法之前,請致電

getFragmentManager().beginTransaction() .add(R.id.fragmentHolder, ConfRoomListFragment) .addToBackStack(null) .commit(); 

如果ConfRoomListFragment的onCreate()方法被調用嘗試使用ArrayAdapter的下一個構造函數:

ArrayAdapter adapter = = new ArrayAdapter<>(getContext(), R.layout.activity_listview, mobileArray); 

而變化列表項(activity_listview。XML)一點點:

<?xml version="1.0" encoding="utf-8"?> 
<!-- Single List Item Design --> 

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dip" 
    android:textSize="16sp" 
    android:textStyle="bold" > 
</TextView> 
+0

我更新了我的問題 –

0
activity = (ApplicationController) getActivity(); 

刪除上面的行,這行永遠不會在這個片段中使用。

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

    ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), 
       android.R.layout.simple_list_item_1, mobileArray); 

    ListView listView = (ListView)view.findViewById(R.id.conf_room_directory); 
    listView.setAdapter(adapter); 

    return view; 
} 

fragment_conf_room_list.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

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

</LinearLayout> 
+0

沒有工作。仍然沒有顯示 –

+0

看到我編輯的答案,如果還有錯誤,那麼我確信,你的片段不是從Activity調用的。 –

+0

它沒有工作。如何將此片段添加到活動 –

0

你需要,如果你想使用自己的佈局,以指定適配器TextView佈局。

public class ConfRoomListFragment extends Fragment { 
    ApplicationController activity; 
    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry", 
     "WebOS","Ubuntu","Windows7","Max OS X"}; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_conf_room_list, container, false); 
     activity = (ApplicationController) getActivity(); 
     ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), 
      R.layout.activity_listview, R.id.label, mobileArray); 

     ListView listView = (ListView)view.findViewById(R.id.conf_room_directory); 
     listView.setAdapter(adapter); 

     return view; 
    } 
} 

併爲您的ListView高度必須設置爲match_parent

<ListView 
    android:id="@+id/conf_room_directory" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 
+0

我更新了我的問題 –

相關問題