我面臨一個奇怪的問題,同時使用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>
* ListView的內容不顯示* - 您看到沒有內容的行或根本沒有內容? – Luksprog
你的'onClickListener'代碼在哪裏? – Sean
@Luksprog列表項的內容僅在處於選定狀態時才顯示。即,當我點擊它的內容顯示... –