我在使用Android Studio创建SearchView小部件时遇到问题:
DataBaseHandler中的无法应用于(java.lang.CharSequence)
我的代码:
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="search"
android:searchSettingsDescription="search."
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestSelection=" ?"
android:searchSuggestThreshold="2" >
</searchable>
的AndroidManifest.xml
<activity
android:name="com.rifki.doaniatislam.AuteursActivity"
android:label="@string/title_activity_auteurs"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<!-- Points to searchable activity -->
<meta-data android:name="android.app.default_searchable"
android:value=".AuteursActivity" />
<meta-data android:name="android.app.default_searchable"
android:value=".QuotesActivity" />
<!-- Points to searchable meta data -->
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
menu_quotes.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.rifki.doaniatislam.QuotesActivity">
<item android:id="@+id/search"
android:title="@string/search"
android:orderInQuote="10"
android:icon="@mipmap/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
QuotesActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_quotes, menu);
MenuItem searchItem = menu.findItem(R.id.search);
// SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setQueryHint(getResources().getString(R.string.search));
// Configure the search info and add any event listeners
// searchView.setSearchableInfo(searchManager
// .getSearchableInfo(getComponentName()));
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
imageArry.clear();
List<Quote> quotes = db.getAllQuotes(searchView.getQuery());
for (Quote cn :quotes) {
imageArry.add(cn);
}
dataList.setAdapter(adapter);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
// Here u can get the value "query" which is entered in the
// search box.
return false;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
break;
}
return true;
DataBaseHandler.java
// Getting All Quotes
public List<Quote> getAllQuotes(String limit) {
List<Quote> quoteList = new ArrayList<Quote>();
// Select All Query
String selectQuery = "SELECT quote._id, quote.author_name, quote.qte, quote.category_name,fav, author.file_name FROM quote,author where author.name = quote.author_name ORDER BY quote.qte "+limit;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
try {
if (cursor.moveToFirst()) {
do {
Quote quote = new Quote();
quote.setID(Integer.parseInt(cursor.getString(0)));
quote.setName(cursor.getString(1));
quote.setQuote(cursor.getString(2));
quote.setCategory(cursor.getString(3));
// quote.setImage(cursor.getBlob(4));
quote.setFav(cursor.getString(4));
quote.setFileName(cursor.getString(5));
quoteList.add(quote);
} while (cursor.moveToNext());
}
} finally {
cursor.close();
db.close();
}
// return quote list
return quoteList;