【发布时间】:2016-08-08 11:13:52
【问题描述】:
我正在关注this tutorial 在我的工具栏中创建一个搜索视图。我想要的是当我点击其中一个搜索结果项时,我想在详细信息活动中显示其相应的数据,但我似乎无法实现。
这是我的代码:
MainActivity 类:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(
new ComponentName(this, SearchableActivity.class)));
searchView.setIconifiedByDefault(false);
return true;
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Toast.makeText(this, "Searching by: "+ query, Toast.LENGTH_SHORT).show();
} else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
String uri = intent.getDataString();
Toast.makeText(this, "Suggestion: "+ uri, Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onQueryTextSubmit(String query) {
// User pressed the search button
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// User changed the text
return false;
}
SearchableActivity 类:
public class SearchableActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
TextView txt = (TextView)findViewById(R.id.textView);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
txt.setText("Searching by: "+ query);
} else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
String uri = intent.getDataString();
txt.setText("Suggestion: "+ uri);
}
}
CitySuggestionProvider 类:
public class CitySuggestionProvider extends ContentProvider {
List<String> cities;
@Override
public boolean onCreate() {
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
if (cities == null || cities.isEmpty()){
Log.d("NGVL", "WEB");
List<CityPojo> pojos = new ArrayList<>();
cities = new ArrayList<>();
DatabaseHelper db = new DatabaseHelper(getContext());
try {
try {
db.create();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
if (db.open()) {
pojos = db.getCities();
}
db.close();
int lenght = pojos.size();
for (int i = 0; i < lenght; i++) {
String city = pojos.get(i).getCity();
cities.add(city);
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.d("NGVL", "Cache!");
}
MatrixCursor cursor = new MatrixCursor(
new String[] {
BaseColumns._ID,
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID
}
);
if (cities != null) {
String query = uri.getLastPathSegment().toUpperCase();
int limit = Integer.parseInt(uri.getQueryParameter(SearchManager.SUGGEST_PARAMETER_LIMIT));
int lenght = cities.size();
for (int i = 0; i < lenght && cursor.getCount() < limit; i++) {
String city = cities.get(i);
if (city.toUpperCase().contains(query)){
cursor.addRow(new Object[]{ i, city, i });
}
}
}
return cursor;
}
}
清单:
<activity
android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<provider
android:name=".CitySuggestionProvider"
android:authorities="ngvl.android.demosearch.citysuggestion"
android:enabled="true"
android:exported="true"/>
<activity android:name=".SearchableActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
可搜索的.xml:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/hint_search"
android:label="@string/app_name"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:searchSuggestAuthority="ngvl.android.demosearch.citysuggestion"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestIntentData="content://ngvl.android.demosearch.city"/>
一切正常,我得到的结果就像这张图片:
但问题是当我单击一个建议时,我想在另一个活动中显示其详细信息。我有 2 个文本字段:城市和国家/地区,因此当用户单击建议中的城市时,我想在详细信息活动中显示其对应的国家/地区。我不确定如何将国家数据传递给详细信息活动。我正在使用 SQLite 来获取内容提供程序类中的数据
【问题讨论】:
-
见stackoverflow.com/a/2091482/4706693 只需在用户点击建议时传递您需要的任何信息,例如国家和城市
-
@NicolasSimon 我知道如何在活动之间传递数据..但是在搜索中我不知道在哪里添加用于传递数据的代码,而且我正在使用内容提供程序,所以我可以搞不清楚
标签: android sqlite search android-contentprovider