我正在制作一个生物医学应用程序,它提供信息并在此处被引用。我试图利用从应用程序中的数据库(.db文件)中提取信息的搜索功能。搜索功能似乎正在工作,但搜索引导用户到空白页面。我希望结果以列表视图呈现。我需要在我的showResults方法中使用搜索数据创建/填充列表视图? (我有一个空白列表视图的布局文件(search_results))。在列表视图中显示搜索结果
package com.joe.biomarker;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
import android.widget.ListAdapter;
public class SearchResultsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
}
private void showResults(String query) {
// Query your data set and show results
// ...
Cursor results = mDataTableInstance(query, mColumnsArray);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getContext(), R.layout.search_results, results, mFromArray, mToArray);
mListView.setAdapter(adapter);
}
}
你想使用'SearchView'吗?如果是这样的话,首先使用'setOnQueryTextListener(SearchView.OnQueryTextListener listener)'在侦听器中你可以使用像'adapter.getFilter()。filter(newText)' – pskink
这样的简单方法(约20行真实代码):https:/ /codeshare.io/aFQT7 – pskink