このブログhttp://weblog.plexobject.com/?p=1689の検索アクティビティのカスタム提案リストをdoSearchQueryに実装していますが、彼が何をしたのか理解できません。もう1つは、たとえば月の名前を入力して検索し、「A」で始まると、それを比較して、8月、4月などの一致結果を返しますが、提案リストには「a」のみが表示され、すべての一致結果は「j」のようになります。 1月、6月、7月に含まれています。しかし、提案リストでは、今月の名前だけでなく、j、j、jを取得します。私が間違っているか、私が理解していない何かを見逃しているところ
これがSearchActivityの私のコードです
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.search_activity);
this.setDefaultKeyMode(Activity.DEFAULT_KEYS_SEARCH_LOCAL);
final Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction)) {
this.doSearchQuery(queryIntent);
} else if (Intent.ACTION_VIEW.equals(queryAction)) {
this.doView(queryIntent);
} else {
Log.d(TAG, "Create intent NOT from search");
}
}
@Override
public void onNewIntent(final Intent queryIntent) {
super.onNewIntent(queryIntent);
final String queryAction = queryIntent.getAction();
if (Intent.ACTION_SEARCH.equals(queryAction)) {
this.doSearchQuery(queryIntent);
} else if (Intent.ACTION_VIEW.equals(queryAction)) {
this.doView(queryIntent);
}
}
// here in this didn't under what he did one is getting intent and bundle but where he define it.
private void doSearchQuery(final Intent queryIntent) {
String queryString = queryIntent.getDataString(); // from suggestions
if (query == null) {
query = intent.getStringExtra(SearchManager.QUERY); // from search-bar
}
// display results here
bundle.putString("user_query", queryString);
intent.setData(Uri.fromParts("", "", queryString));
intent.setAction(Intent.ACTION_SEARCH);
queryIntent.putExtras(bundle);
startActivity(intent);
Log.e("query string", "query string "+queryString);
}
private void doView(final Intent queryIntent) {
Uri uri = queryIntent.getData();
String action = queryIntent.getAction();
Intent intent = new Intent(action);
intent.setData(uri);
startActivity(intent);
this.finish();
}
これが私のプロバイダーです。1行の提案が必要です。2列目にコメントします。
public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = MySuggestionProvider.class.getName();
public final static int MODE = DATABASE_MODE_QUERIES;
private final static String TAG = MySuggestionProvider.class.getSimpleName();
private static final String[] COLUMNS = {
"_id", // must include this column
SearchManager.SUGGEST_COLUMN_TEXT_1,
// SearchManager.SUGGEST_COLUMN_TEXT_2,
SearchManager.SUGGEST_COLUMN_INTENT_DATA,
SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
SearchManager.SUGGEST_COLUMN_SHORTCUT_ID };
public MySuggestionProvider() {
setupSuggestions(AUTHORITY, MODE);
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
String query = selectionArgs[0];
if (query == null || query.length() == 0) {
return null;
}
MatrixCursor cursor = new MatrixCursor(COLUMNS);
try {
List<String> list = callmyservice(query);
int n = 0;
for (String obj : list) {
cursor.addRow(createRow(Integer.valueOf(n), query, obj));
n++;
}
} catch (Exception e) {
Log.e(TAG, "Failed to lookup " + query, e);
}
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
throw new UnsupportedOperationException();
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException();
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
throw new UnsupportedOperationException();
}
private Object[] createRow(Integer id, String text1,
String name) {
return new Object[] { id, // _id
text1, // text1
//text2, // text2
text1, "android.intent.action.SEARCH", // action
SearchManager.SUGGEST_NEVER_MAKE_SHORTCUT };
}
private static final String[] months = {"January", "February","march","April","may","june","july","August","September","octobor",
"november","december"};
List<String> ls2 = new ArrayList<String>();
private List<String> callmyservice(String query){
List<String> ls = new ArrayList<String>();
for(int i=0;i<months.length;i++){
if(months[i].toLowerCase().contains(query.toLowerCase())){
//if(months[i].equalsIgnoreCase(query.toLowerCase())){
ls.add(months[i]);
}
}
ls2.clear();
ls2.addAll(ls);
return ls;
}
}