Я пытаюсь отфильтровать Listview с помощью searchView, у меня есть listview, заполненный с помощью SimpleCursorAdapter, но когда я нажимаю на панель поиска и не нахожу ничего фильтров, вот что я сделал до сих пор.SearchView Filter не работает
Моя розыскную деятельность
package com.example.androidcontacts;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.CursorLoader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.SimpleCursorAdapter;
public class Search extends ListActivity {
ListView listView;
SearchView searchView;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
searchView=(SearchView)findViewById(R.id.searchView1);
listView=(ListView)findViewById(android.R.id.list);
Uri allContacts = Uri.parse("content://"+"com.example.androidcontacts.Books" + "/Contact");
Cursor c;
CursorLoader cursorLoader = new CursorLoader(
this,allContacts,null,null,null,null);
c = cursorLoader.loadInBackground();
// THE DESIRED COLUMNS TO BE BOUND*/
String[] columns = new String[] {Contact.TITLE};
int[] views = new int[] {R.id.code};
listView.setAdapter(adapter);
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listview_item_layout,
c, columns, views,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
this.setListAdapter(adapter);
searchView.setOnQueryTextListener(new OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
}
}
Содержимое Provider
package com.example.androidcontacts;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
public class Contact extends ContentProvider {
static final String PROVIDER_NAME = "com.example.androidcontacts.Books";
static final Uri CONTENT_URI = Uri.parse("content://"+ PROVIDER_NAME + "/Contact");
static final String _ID = "_id";
static final String TITLE = "title";
static final String AUTHOR = "author";
static final String YEAR = "year";
static final String IMAGE = "image";
static final int BOOKS = 1;
static final int BOOK_ID = 2;
private static final UriMatcher uriMatcher;
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "books", BOOKS);
uriMatcher.addURI(PROVIDER_NAME, "books/#", BOOK_ID);
}
//---for database use---
DatabaseHelper DBHelper;
SQLiteDatabase booksDB;
static final String DATABASE_NAME = "Books";
static final String DATABASE_TABLE = "Contact";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE = "create table " + DATABASE_TABLE +
" (_id integer primary key autoincrement, " + "title text not null, author text not null, year text not null"
+ ", image BLOB);";
private final class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion) {
Log.w("Content provider database","Upgrading database from version " + oldVersion + " to " + newVersion +
", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// arg0 = uri
// arg1 = selection
// arg2 = selectionArgs
int count=0;
switch (uriMatcher.match(arg0)){
case BOOKS:
count = booksDB.delete(DATABASE_TABLE,arg1,arg2);
break;
case BOOK_ID:
String id = arg0.getPathSegments().get(1);
count = booksDB.delete(DATABASE_TABLE,_ID + " = " + id +
(!TextUtils.isEmpty(arg1) ? " AND (" + arg1 + ')' : ""),arg2);
break;
default: throw new IllegalArgumentException("Unknown URI " + arg0);
}
getContext().getContentResolver().notifyChange(arg0, null);
return count;
}
@Override
public String getType(Uri uri){
switch (uriMatcher.match(uri)){
// Get all books
case BOOKS:
return "vnd.android.cursor.dir/vnd.androidcontacts.Books ";
// Get a particular book
case BOOK_ID:
return "vnd.android.cursor.item/vnd.androidcontacts.Books ";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// add a new winner
long rowID = booksDB.insert(DATABASE_TABLE,"",values);
// if winner record added successfully
if (rowID>0)
{
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
throw new SQLException("Failed to insert row into " + uri);
}
@Override
public boolean onCreate() {
Context context = getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
booksDB = dbHelper.getWritableDatabase();
return (booksDB == null)? false:true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
sqlBuilder.setTables(DATABASE_TABLE);
if (uriMatcher.match(uri) == BOOK_ID)
// If getting a particular winner
sqlBuilder.appendWhere(_ID + " = " + uri.getPathSegments().get(1));
if (sortOrder==null || sortOrder=="")
sortOrder = TITLE;
Cursor c = sqlBuilder.query(booksDB,projection,selection,selectionArgs,
null,null,sortOrder);
// Register to watch a content URI for changes
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)){
case BOOKS:
count = booksDB.update(DATABASE_TABLE,values,selection,selectionArgs);
break;
case BOOK_ID:
count = booksDB.update(DATABASE_TABLE,values,_ID + " = " + uri.getPathSegments().get(1) +
(!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""),selectionArgs);
break;
default: throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
}