我創建了一個連接到列表視圖的片段來顯示一個簡單的表格視圖。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();
}
正如你所看到的,我隱藏所有其他的片段,並表明我想要的片段。但是,只有標題得到更新,片段不會顯示。所以問題不在於活動,還有一些我沒有看到的錯誤。
,你誇大你的片段?發佈該代碼也 –
@AnandSavjani它在我附加的第一個代碼塊 –
中添加一些日誌到ConfRoomListFragment的onCreateView()方法,例如, :Log.i(「Tag」,「onCreateView called」)並查看它是否被調用。如果沒有,那麼你有一些邏輯錯誤。 – Sergey