2012-12-10 41 views
1

我使用SimpleCursorAdapter的子類作爲適配器,將ListFragment擴展到support lib中。所有的數據加載部分是好的(通過調試讀取),列表只是不顯示,我有一個空白頁上的活動。這裏是我的代碼:android.support.v4.app.ListFragment只是不顯示

import android.support.v4.app.FragmentActivity; 

public class HostActivity extends FragmentActivity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_view_with_fragment); 
} 
} 

佈局文件(activity_view_with_fragment.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical" > 
    <fragment 
      android:id="@+id/lsStopTimetableFragment" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_marginTop="?android:attr/actionBarSize" 
      android:layout_weight="1" 
      class="com.package.Fragment.MyFragment"> 
    </fragment> 
</LinearLayout> 

這是ListFragment代碼:

import android.support.v4.app.ListFragment; 
import android.support.v4.app.LoaderManager; 
import android.support.v4.content.CursorLoader; 
public class MyFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{ 
    public void onActivityCreated(Bundle savedInstanceState){ 
     super.onActivityCreated(savedInstanceState); 
     String [] from = new String[] {"col_1", "col_2"}; 
     int [] to = new int [] {R.id.view1, R.id.view2}; 
     //..put value in args 
     Loader l = getLoaderManager().restartLoader(0, args, this); 
     if(l != null) 
      l.forceLoad(); 

     adapter = new CustomCursorAdapter(this.getActivity() 
       , R.layout.row_entry, null, from, 
       to, 0); 
     setListAdapter(adapter); 
    } 
    @Override 
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) { 
     adapter.swapCursor(cursor); 
    } 
    public void onLoaderReset(Loader<Cursor> cursorLoader) { 
     adapter.swapCursor(null); 
    } 

} 

而在CustomCursorAdapter,我不落實

getView(INT POS,查看convertView,V iewGroup父)

方法。任何提示?提前致謝。

回答

1

您是否嘗試將片段項目中的layout_width和layout_height參數從0dp更改爲match_parent?如果你正在試圖與只列出片段,充滿整個活動的活動,你並不需要創建佈局,如:

<LinearLayout> 
<fragment/> 
</LinearLayout> 

您可以直接與片段標籤爲根,只有標籤創建佈局:

<fragment/> 
+0

肯定的答案! (來自用戶評分爲1的真棒迴應),歡呼聲:) –

相關問題