简体   繁体   English

使用SimpleCursorAdapter基于SQLite查询更新列表视图

[英]Updating listview based on sqlite query with SimpleCursorAdapter



I am having a listview with one profile imageview, two text views and a favorite imageview. 我有一个带有一个个人资料imageview,两个文本视图和一个收藏夹imageview的listview。
In search box which I have provided above list view I want to query sqlite DB and get updated results populated in my listview using SimpleCursorAdapter. 在我在列表视图上方提供的搜索框中,我想查询sqlite DB并使用SimpleCursorAdapter获取在列表视图中填充的更新结果。

The following is my create table statement which shows all columns involved. 以下是我的create table语句,其中显示了所有涉及的列。

public static String CREATE_TABLE_STMT="create table "
            +CONTACT_TABLE+" ("
            +ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
            +NAME+" varchar(100) NOT NULL UNIQUE ,"
            +PHONE+" varchar(100) ,"
            +EMAIL+" varchar(100) ,"
            +JOBTITLE+" varchar(100) ,"
            +WEBPAGE+" varchar(100) ,"
            +PICTUREURL+" varchar(100) ,"
            +THUMBNAILURL+" varchar(100) ,"
            +PICTUREPATH+" varchar(100) ,"
            +THUMBNAILPATH+" varchar(100))";

I am trying to run the following query to search rows which has "NAME" matching with my search text. 我正在尝试运行以下查询来搜索具有与我的搜索文本匹配的“ NAME”的行。 To be specific I am trying to retrieve rows who's name column value starts with search query . 具体来说,我正在尝试检索名称列值以search query开头的行。

String searchQuery = "SELECT ID as _id," + AppConstants.NAME+","+AppConstants.JOBTITLE+","+AppConstants.THUMBNAILURL +  " from " + AppConstants.CONTACT_TABLE + " where " + AppConstants.NAME + " LIKE '" + query + "';";


  • Searching for "al" should return back full row having name as "Alan" 搜索“ al”应返回名称为“ Alan”的完整行
  • Searching for "tho" should return back full row having name as "Thomas" 搜索“ tho”应返回名称为“ Thomas”的整行

After running the query I am checking the cursor values and trying to update my list view 运行查询后,我正在检查游标值并尝试更新我的列表视图

if (cursor != null) {
            Log.i(AppConstants.APPUILOG,"cursor not null");
            cursor.moveToFirst();
            String[] from = new String[] new String[] {NAME,JOBTITLE,THUMBNAILURL};
            int[] to = new int[] {R.id.textViewMain,R.id.textViewSub,R.id.contact_icon};
            SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.contact_list_item, cursor, from, to);
            mainListView.setAdapter(cursorAdapter);
        }else {
            Log.i(AppConstants.APPUILOG,"cursor is null");
        }

But this code is always giving back blank listview. 但是这段代码总是返回空白的listview。

  • Do I have to customize SimpleCursorAdapter to implement the same? 我是否必须自定义SimpleCursorAdapter才能实现相同的功能?
  • What modification I need to do in my query? 我需要在查询中做哪些修改?
  • How to create a custom SimpleCursorAdapter and display the updated results? 如何创建自定义的SimpleCursorAdapter并显示更新的结果?

As I am getting ,You want to search in ListView . 当我得到时,您想在ListView中搜索。 Am I right ? 我对吗 ? If yes then you need change your approach , You don't need to interact with DB while searching in ListView . 如果是,那么您需要改变方法,在ListView中搜索时不需要与数据库交互。 You need to implement Filters in ListView . 您需要在ListView中实现过滤器。 You can see this link for complete solution . 您可以看到链接以获取完整的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 simplecursoraapter/listview 到 sqlite 数据库 - simplecursoradapter/listview to sqlite database 来自SimpleCursorAdapter的ListView数据的查询结果 - ListView Data from SimpleCursorAdapter result of a query ListView的SimpleCursorAdapter? - SimpleCursorAdapter for ListView? 具有SimpleCursorAdapter的ListView - ListView with SimpleCursorAdapter 使用ViewBinder,SimpleCursorAdapter,SQLite在ListView上显示相机拍摄的图片 - Using ViewBinder, SimpleCursorAdapter, SQLite, to show pictures taken by a camera on ListView 安卓。 在 Fragment 中使用 SimpleCursorAdapter、SQLite 和 ListView:NullPointerException - Android. Using SimpleCursorAdapter, SQLite and ListView in Fragment: NullPointerException 使用SimpleCursorAdapter更新ListView(无需重新初始化适配器) - Updating ListView using SimpleCursorAdapter ( without re-initializing adapter ) 更新sqlite数据库并使用SimpleCursorAdapter导致UI挂起 - Updating sqlite database and using SimpleCursorAdapter causing UI to hang 使用扩展的SimpleCursorAdapter和备用LiteItem样式动态更新ListView - Dynamically updating ListView with extended SimpleCursorAdapter and Alternate LiteItem Styles 更改基于SimpleCursorAdapter字段的Lis​​tView项目的背景颜色? - Change background colour of ListView item based on SimpleCursorAdapter field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM