[英]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 + "';";
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。
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.