使用ListView
填充SimpleCursorAdapter
时遇到一个奇怪的问题,我的ListFragment
源代码如下所示。
* 问题:* 通过我的addEditTaskActivity
在列表中添加一些数据后(我已经测试过插入没有任何问题。){{1}的内容不显示。但是当我点击/点击列表项时,会显示正确的数据。
我不知道我在这里做错了什么。
的 TasksListFragment.java 的
ListView
我的布局的源代码是:
*的 course_task_fragment.xml *
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);
}
}
答案 0 :(得分:2)
Fragment
已经引用了Context
(使用的Activity
),可以通过调用getActivity()
来检索。您应该使用该方法初始化适配器实例。
与您的实施相关:
你应该避免像你一样实现一个参数构造函数。这样做的原因是Android系统可能会在稍后(在ViewPager
中,在配置更改之后等)尝试重新创建您的片段,并且它将通过调用无参数构造函数(未实现)来执行此操作在你的情况下)。您还将丢失传入的数据值。
你不需要传递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");
获取值。