2013-07-06 81 views
1

我面臨一個奇怪的問題,同時使用SimpleCursorAdapter填充ListView,我的源代碼ListFragment在下面給出。ListView內容不顯示使用SimpleCursorAdapter除非我點擊/點擊列表項目

* 問題是:*通過添加列表中的一些數據後,我的addEditTaskActivity(我已經測試過,有什麼錯插入。)的ListView的內容不會顯示。但是當我點擊/點擊列表項時,顯示正確的數據。

我不知道我在這裏做錯了什麼。

TasksListFragment.java

public class CourseTaskListFragment extends SherlockListFragment implements 
     LoaderCallbacks<Cursor>, OnItemClickListener { 

private LoaderManager mLoaderManager; 
private ListView mListView; 
protected int dialogOkCancel; 
protected long selectedTID; 
private CursorLoader mCursorLoader; 
private AsyncQueryHandler mAsyncQueryHandler; 
private Cursor mCursor; 
private Context context; 
private long semesterID_FK; 
private SimpleCursorAdapter mSimpleTaskListAdapter; 

// --------------------------------------------------------------------- 

public CourseTaskListFragment(Context context, long currentCourseId, 
     long semesterID_FK) { 
    super(); 
    this.currentCourseId = currentCourseId; 
    this.semesterID_FK = semesterID_FK; 
    this.context = context; 
} 

// --------------------------------------------------------------------- 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.i(StudyManagerDataSource.LOG_TAG, 
      "TaskListFragment onCreate called!"); 
    // Enable the option menu for the Fragment 
    setHasOptionsMenu(true); 

    setRetainInstance(true); 


    mSimpleTaskListAdapter = new SimpleCursorAdapter(context, 
      android.R.layout.simple_list_item_1, null, 
      new String[] { Task.COL_NAME }, 
      new int[] { android.R.id.text1 }, 
      SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); 
    setListAdapter(mSimpleTaskListAdapter); 

    mLoaderManager = getLoaderManager(); 
    mLoaderManager.initLoader(1, null, this); 

} // end onCreate 

// --------------------------------------------------------------------- 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    return inflater 
      .inflate(R.layout.course_task_fragment, container, false); 
} 

// --------------------------------------------------------------------- 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    mListView = getListView(); 
    mListView.setLongClickable(true); 

    registerForContextMenu(mListView); 

    mListView.setOnItemClickListener(this); 
    mListView.setOnItemLongClickListener(this); 
} 

// ----------------------------------------------------------------- 

@Override 
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { 

    mCursorLoader = new CursorLoader(getActivity(), Task.CONTENT_URI, 
      Task.allColumns, Task.COL_COURSE_ID_FK + " = ? and " 
        + Task.COL_SEMESTER_ID_FK + " = ? ", new String[] { 
        String.valueOf(currentCourseId), 
        String.valueOf(semesterID_FK) }, null); 

    return mCursorLoader; 
} 

// ----------------------------------------------------------------- 

@Override 
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) { 
    // TODO Auto-generated method stub 
    if (mSimpleTaskListAdapter != null && cursor.moveToFirst()) { 
     mSimpleTaskListAdapter.swapCursor(cursor); 

    } else { 

    } 

} 

// ----------------------------------------------------------------- 
@Override 
public void onLoaderReset(Loader<Cursor> arg0) { 
    if (mSimpleTaskListAdapter != null) 
     mSimpleTaskListAdapter.swapCursor(null); 

    } 


} 

和我佈局的源代碼是:

* course_task_fragment.xml *

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal|center_vertical" 
    android:orientation="vertical" > 

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

    <TextView 
     android:id="@android:id/empty" 
     style="@style/emptyListMessage" 
     android:text="@string/no_homework" /> 

</RelativeLayout> 
+0

* ListView的內容不顯示* - 您看到沒有內容的行或根本沒有內容? – Luksprog

+0

你的'onClickListener'代碼在哪裏? – Sean

+0

@Luksprog列表項的內容僅在處於選定狀態時才顯示。即,當我點擊它的內容顯示... –

回答

2

一個Fragment已經有一個參考a Context(使用Activity)可以通過調用getActivity()進行檢索。您應該使用該方法初始化適配器實例。

與您實現:

你應該避免像你這樣實現一個參數的構造函數。原因是Android系統可能會在稍後嘗試重新創建您的片段(在ViewPager中,在配置更改後等),它將通過調用no參數構造函數(在您的情況下未實現) 。你也將失去你傳遞的數據值

你並不需要通過Context,您可以在Bundle作爲參數傳遞的休息。

Bundle args = new Bundle(); 
args.put("course_id", long currentCourseId); 
args.put("semester", long semesterID_FK); 
// pass this to the fragment instance instead of the constructor 
CourseTaskListFragment fragment = new CourseTaskListFragment(); // your current constructor would be removed 
fragment.setArguments(args); 

你可以在片段然後使用:

getArguments().getLong("couser_id"); 
getArguments().getLong("semester"); 

獲取值。