我在FragmentActivity中使用ListFragment以及SimpleCursorAdapter和修改过的CursorLoader.修改后的CursorLoader只发出rawQueries - 没有其他更改.
在FragmentActivity中的某个时刻,我需要重新获取在ListFragment中提供ListView的数据/光标.
我怎样才能做到这一点?
提前谢谢了.
这是FragmentActivity调用ListFragment中的方法:
public class ActivityList extends FragmentActivity {
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
...
processUpdateList();
}
...
private void processUpdateList() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentlist);
if (fragment != null) {
((FragmentList) fragment).requeryList();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里是ListFragment,该方法应该启动对ListView的重新查询,重新加载或重新绘制.ListView.invalidate()没有帮助 - 它没有改变显示的数据.
public class FragmentList extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
private SimpleCursorAdapter adapter;
private Context context;
private ListView listView;
public void requeryList() {
// listView.invalidate(); didn't re-query
// TODO: How???
} …Run Code Online (Sandbox Code Playgroud) android listview simplecursoradapter android-fragments android-fragmentactivity
我用SimpleCursorAdapter送我读出的数据database来ListView.
SimpleCursorAdapter 有2个标志,其中一个已被弃用.
我应该经常使用FLAG_CONTENT_OBSERVER吗?
或者使用其他东西而不是更好SimpleCursorAdapter?
我的应用程序中有一个sqlite数据库.我想用它做一个可扩展的列表视图.
我已经采取了我应采取的方法.
尝试了很多相同的教程,却找不到一个教程,其中一个用本地数据库填充Expandable列表.
在android站点中有一个教程,他们用手机中的联系人详细信息填充可扩展列表.他们是从内容提供商 ExpandableList2.java这样做的
所以我的问题是我是否应该为我自己的应用程序内的数据库创建一个内容提供程序?
这是唯一的方法还是还有其他更好的方法吗?
sqlite android expandablelistview android-contentprovider simplecursoradapter
你有一个带有列表视图的行imageview和textview一行的布局SimpleCursorAdapter吗?
这将是布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/bowler_txt"
android:paddingLeft="25dp"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bowler"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
可以使用listview在SimpleCursorAdapter中完成吗?当我在listview中需要图像时,我总是使用自定义arrayadapter但从不使用光标.
如果可以的话,我该如何设置图像?
我的问题:每当我通过其(自定义的)SimpleCursorAdapter更新其内容时,我的ListView会将其滚动位置重置为顶部.我希望ListView在更新时保持其滚动位置.
我开始时每次都使用创建一个新的适配器实例ListView.setAdapter(),但根据这个讨论,我应该更新适配器而不是重置它.我无法找到一种方法来为SimpleCursorAdapter正常工作.
以下是我尝试过的一些事情,省略了方法参数:
- SimpleCursorAdapter.changeCursorAndColumns()使用新光标成功更新适配器但仍然重置滚动位置.
- SimpleCursorAdapter.changeCursor()行为方式相同.
- SimpleCursorAdapter.swapCursor()直到api 11才可用,我正在为api 8开发.
- SimpleCursorAdapter.notifyDataSetChanged()不更新ListView.(也许我还缺少另一个步骤.)
- ListView.invalidate()不更新ListView.
- SimpleCursorAdapter.requery()使ListView为空白,不推荐使用.
- 我对ListView.setSelection()解决方案很熟悉,但我不希望我的ListView在更新时完全移动.这会将其捕捉到列表中的某个位置.
这似乎应该是一个普通而简单的问题.有人处理过吗?
这可能是一个noob问题,但我对所有这些SQLite-Database-Cursor-Adapter-ListView-Do-It-Properly-Stuff都很新.
是)我有的:
在我,MainActivity我有一个ListView.我使用an SQLite database并使用ListView自定义适配器扩展填充SimpleCursorAdapter.点击ActionBar我激活的项目Contextual Action Mode.到目前为止一切正常.
我想要的是:
通过单击我ListView item的相应数据库中的某个图标,应删除该行,并ListView应刷新.
我的问题:
如何刷新我Cursor和我的ListView正确?当我不使用cursor.requery()我OnClickListener而使用时,cursor = dbm.getIOIOSensorsCursor()我会在CursorIndexOutOfBoundsException该行下方输入几行
int state = cursor.getInt(cursor.getColumnIndex(IOIOSensorSchema.STATE));
Run Code Online (Sandbox Code Playgroud)
我的应用程序崩溃,但重新加载后,数据库已被删除,相应ListView item的消失.
我想崩溃必须与_positionget getView方法有关,因为_position是final.但是,当我使用cursor.requery()一切正常的时候.
但是这种方法已被弃用,它的文档说"不要使用它......".我是正确编码的朋友(我还是一个初学者,想要学习正确的编码而不是快速和肮脏),并想知道如何正确编码.我不知道它是否重要,但我只在我的(非常快)Nexus 4上测试我的应用程序.刷新Cursor速度似乎没有问题,但我想知道它是否适用于速度较慢的设备.如果它对您很重要,我的数据库将包含大约10-20行,大约12列.我想这是一个非常小的数据库.
这是我的自定义适配器的相关代码:
public class IOIOSensorCursorAdapterCam extends SimpleCursorAdapter
{
static class ViewHolder
{
ImageView …Run Code Online (Sandbox Code Playgroud) 我在应用程序的列表中设置了一个简单的游标适配器,如下所示:
private static final String fields[] = {"GenreLabel", "Colour", BaseColumns._ID};
datasource = new SimpleCursorAdapter(this, R.layout.row, data, fields, new int[]{R.id.genreBox, R.id.colourBox});
Run Code Online (Sandbox Code Playgroud)
R.layout.row由两个TextViews(genreBox和colourBox)组成.我不想将TextView的内容设置为"Color"的值,而是将其背景颜色设置为该值.
我需要做些什么来实现这一目标?
Android问题中讨论了这个问题:过滤listview时检查了错误的项目.总结该问题,当使用带有CursorAdapter和过滤器的列表视图时,在过滤后的列表中选择的项目将在删除过滤器后丢失其选择,而是选择未过滤列表中该位置的项目.
使用上面链接问题中的代码示例,我们应该在哪里放置代码来标记复选框.我相信它应该在CustomCursorAdapter的getView()方法中,但我不确定.另外,我们如何访问包含自定义适配器类中所有selectedIds的HashSet,因为它将在保存列表的主活动中初始化和修改.
我实现ListView的活动
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectfriends);
Log.v(TAG, "onCreate called") ;
selectedIds = new ArrayList<String>() ;
selectedLines = new ArrayList<Integer>() ;
mDbHelper = new FriendsDbAdapter(this);
mDbHelper.open() ;
Log.v(TAG, "database opened") ;
Cursor c = mDbHelper.fetchAllFriends();
startManagingCursor(c);
Log.v(TAG, "fetchAllFriends Over") ;
String[] from = new String[] {mDbHelper.KEY_NAME};
int[] to = new int[] { R.id.text1 };
final ListView listView = getListView();
Log.d(TAG, "Got listView");
// Now initialize the adapter and set it to display using …Run Code Online (Sandbox Code Playgroud) 我使用CursorLoader查询结果,这不是我想在ListFramgenet中显示的顺序.怎么排序呢?
我用它来设置适配器:
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_2, null,
new String[] { "name", "distance"},
new int[] { android.R.id.text1, android.R.id.text2 }, 0);
setListAdapter(mAdapter);
// Start out with a progress indicator.
setListShown(false);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
Run Code Online (Sandbox Code Playgroud)
创建加载器:
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getActivity(),
Uri.withAppendedPath(TraceTable.CONTENT_URI, "latest"),
MEMBERS_PROJECTION,
null,
null,
null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.changeCursor(data);
// The list should now be …Run Code Online (Sandbox Code Playgroud) 我有一个光标检索应用程序上有街道地址的所有联系人.然后将此光标传递到适配器.到现在为止还挺好.除了我还得到一堆只有州/国家信息的低价值联系人(主要来自Skype).是否有一种简单的方法来修改URI以跳过这些?
public Cursor getDirectoryList (CharSequence constraint) {
String[] selectionArguments = { "%"+constraint.toString()+"%" };
String selection = ContactsContract.CommonDataKinds.StructuredPostal.DISPLAY_NAME + " like ?";
Uri uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
String sortOrder = ContactsContract.CommonDataKinds.StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cr = getContentResolver().query(uri, null, selection, selectionArguments, sortOrder);
return cr;
}
Run Code Online (Sandbox Code Playgroud) android android-contentresolver street-address simplecursoradapter