带有 ContentProvider 的 SimpleCursorAdapter;列表视图为空
SimpleCursorAdapter with ContentProvider; ListView empty
我正在使用 SimpleCursorAdapter
尝试显示来自内容提供商的数据。但是 ListView
保持为空。一直在寻找答案 3 天。
这是我的代码:
IssuesFragment.java
public class IssuesFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private SimpleCursorAdapter issuesAdapter;
private static final int ISSUES_LOADER = 0;
private static final String[] ISSUES_COLUMNS = {
DbContract.issues._ID,
DbContract.issues.ID,
DbContract.issues.THUMBNAIL,
DbContract.issues.TEXT
};
public static final int COL_ISSUES__ID = 0;
public static final int COL_ISSUES_ID = 1;
public static final int COL_ISSUES_THUMBNAIL = 2;
public static final int COL_ISSUES_TEXT = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public IssuesFragment() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
getLoaderManager().initLoader(ISSUES_LOADER, null, this);
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_issues, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.issues_listview);
getLoaderManager().initLoader(ISSUES_LOADER, null, this);
issuesAdapter = new SimpleCursorAdapter (
getActivity(),
R.layout.issues_list_item,
null,
ISSUES_COLUMNS,
new int[]{
R.id.issues_list__id,
R.id.issues_list_id,
R.id.issues_list_thumbnail,
R.id.issues_list_text,
},
0
);
listView.setAdapter(issuesAdapter);
return rootView;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri issuesEntry = DbContract.issues.buildIssuesUri();
return new CursorLoader(
getActivity(),
issuesEntry,
ISSUES_COLUMNS,
null,
null,
null
);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
issuesAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
issuesAdapter.swapCursor(null);
}
}
对应的布局文件如下:
issues_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:padding="16dp">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/issues_list__id" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/issues_list_id" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/issues_list_thumbnail" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/issues_list_text" />
</LinearLayout>
fragment_issues.xml
<LinearLayout
....
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/issues_listview"
android:layout_weight="1" />
<TextView
android:id="@+id/emptyListElem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Keine Ausgaben vorhanden." />
</LinearLayout>
我的光标填满了数据。我通过在 onLoadFinished
方法中打印 DatabaseUtils.dumpCursorToString(cursor)
验证了这一点。但是数据并没有从那里到达 UI.
我发现了问题所在。来自 fragment_issues.xml 的元素 emptyListElem Textview 正在替换列表元素,即使我有
View empty = getActivity().findViewById(R.id.emptyListElem);
listView.setEmptyView(empty);
在我的 onCreateView 方法中,AFAIK 应该让 emptyListElem 仅在没有内容填充列表时显示。好像我错了。或者我把密码放错地方了。
编辑:我删除了 Element emptyListElem,现在可以使用了。
我正在使用 SimpleCursorAdapter
尝试显示来自内容提供商的数据。但是 ListView
保持为空。一直在寻找答案 3 天。
这是我的代码:
IssuesFragment.java
public class IssuesFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private SimpleCursorAdapter issuesAdapter;
private static final int ISSUES_LOADER = 0;
private static final String[] ISSUES_COLUMNS = {
DbContract.issues._ID,
DbContract.issues.ID,
DbContract.issues.THUMBNAIL,
DbContract.issues.TEXT
};
public static final int COL_ISSUES__ID = 0;
public static final int COL_ISSUES_ID = 1;
public static final int COL_ISSUES_THUMBNAIL = 2;
public static final int COL_ISSUES_TEXT = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public IssuesFragment() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
getLoaderManager().initLoader(ISSUES_LOADER, null, this);
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_issues, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.issues_listview);
getLoaderManager().initLoader(ISSUES_LOADER, null, this);
issuesAdapter = new SimpleCursorAdapter (
getActivity(),
R.layout.issues_list_item,
null,
ISSUES_COLUMNS,
new int[]{
R.id.issues_list__id,
R.id.issues_list_id,
R.id.issues_list_thumbnail,
R.id.issues_list_text,
},
0
);
listView.setAdapter(issuesAdapter);
return rootView;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri issuesEntry = DbContract.issues.buildIssuesUri();
return new CursorLoader(
getActivity(),
issuesEntry,
ISSUES_COLUMNS,
null,
null,
null
);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
issuesAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
issuesAdapter.swapCursor(null);
}
}
对应的布局文件如下:
issues_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:padding="16dp">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/issues_list__id" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/issues_list_id" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/issues_list_thumbnail" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/issues_list_text" />
</LinearLayout>
fragment_issues.xml
<LinearLayout
....
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/issues_listview"
android:layout_weight="1" />
<TextView
android:id="@+id/emptyListElem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Keine Ausgaben vorhanden." />
</LinearLayout>
我的光标填满了数据。我通过在 onLoadFinished
方法中打印 DatabaseUtils.dumpCursorToString(cursor)
验证了这一点。但是数据并没有从那里到达 UI.
我发现了问题所在。来自 fragment_issues.xml 的元素 emptyListElem Textview 正在替换列表元素,即使我有
View empty = getActivity().findViewById(R.id.emptyListElem);
listView.setEmptyView(empty);
在我的 onCreateView 方法中,AFAIK 应该让 emptyListElem 仅在没有内容填充列表时显示。好像我错了。或者我把密码放错地方了。
编辑:我删除了 Element emptyListElem,现在可以使用了。