我想到两种解决方法,但是在网上找不到任何示例,希望这里的某个人能够帮助我...
方法一/ Q1:如何直接访问和修改包含"SUGGEST_COLUMN_TEXT_1"和“SUGGEST_COLUMN_TEXT_2”文本的TextView的外观?
方法二 / Q2:另外,SearchView有一个 "setSuggestionsAdapter(CursorAdapter adapter)" 方法,看起来可能比方法一更合适。虽然我已经在我的应用中实现了CursorAdapter,并且已经阅读了相关内容,但我不确定如何为SearchView配置它(尤其是在访问游标方面),所以是否有人可以给我一些一般性的指导或骨架示例?
以下是我的SearchViewFragment类的现有代码:
public class SearchViewFragment extends Fragment {
public SearchViewFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragmentView = inflater.inflate(R.layout.fragment_search_view, container, false);
// Use the Search Manager to find the SearchableInfo related to this Activity
SearchManager searchManager = (SearchManager)getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getActivity().getComponentName());
// Bind the Activity's SearchableInfo to the Search View
SearchView searchView = (SearchView)fragmentView.findViewById(R.id.searchView);
searchView.setSearchableInfo(searchableInfo);
searchView.setIconifiedByDefault(false);
searchView.setSubmitButtonEnabled(true);
//searchView.setQueryRefinementEnabled(true);
return fragmentView;
}
更新:问题已解决!
感谢接受的答案,我创建了这段代码,它非常清晰,并且很好地完成了工作...
public class SearchViewFragment extends Fragment {
private static final String LOG_TAG = SearchViewFragment.class.getSimpleName();
public SearchViewFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
setRetainInstance(true); //todo - not working - Remember search term
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragmentView = inflater.inflate(R.layout.fragment_search_view, container, false);
// Use the Search Manager to find the SearchableInfo related to this Activity
SearchManager searchManager = (SearchManager)getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getActivity().getComponentName());
// Bind the Activity's SearchableInfo to the Search View
final SearchView searchView = (SearchView)fragmentView.findViewById(R.id.searchView);
searchView.setSearchableInfo(searchableInfo);
searchView.setIconifiedByDefault(false);
searchView.setSubmitButtonEnabled(true);
//searchView.setQueryRefinementEnabled(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
//DO whatever you want here on text submit in the search View
Log.d(LOG_TAG, "onQueryTextSubmit(" + s + ")");
return true;
}
@Override
public boolean onQueryTextChange(String textChange) {
Log.d(LOG_TAG, "onQueryTextChange(" + textChange + ")");
ContentResolver cr = getActivity().getContentResolver();
Uri uri = DbContentProvider.CONTENT_URI_REAL_PRODUCTS;
String[] projection = DbContentProvider.getProjectionIn(DbContentProvider.REAL_PRODUCTS_SUGGEST);
String selection = DbContentProvider.getSelection(false);
String[] selectionArgs = {Utilities.formatQueryString(textChange)};
String sortOrder = null;
Cursor cursor = cr.query(uri, projection, selection, selectionArgs, sortOrder);
Log.d(LOG_TAG, "Setting setSuggestionsAdapter. cursor: " + cursor);
searchView.setSuggestionsAdapter(new SearchSuggestionsAdapter(getActivity(), cursor));
return true;
}
});
return fragmentView;
}
private static class SearchSuggestionsAdapter extends SimpleCursorAdapter {
private static final String[] mVisible = {SearchManager.SUGGEST_COLUMN_TEXT_1, SearchManager.SUGGEST_COLUMN_TEXT_2};
private static final int[] mViewIds = {R.id.product_name, R.id.product_shelf};
public SearchSuggestionsAdapter(Context context, Cursor cursor) {
super(context, R.layout.search_view_suggestions, cursor, mVisible, mViewIds, 0);
}
/*
@Override
public void bindView(View view, Context context, Cursor cursor) {
Log.d(LOG_TAG, "bindView(" + view + ", " + context + ", " + cursor + ")");
super.bindView(view, context, cursor);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.d(LOG_TAG, "newView(" + context + ", " + cursor + ", " + parent + ")");
return super.newView(context, cursor, parent);
}
*/
}
}
以下是我的search_view_suggestions.xml文件...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/product_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Name placeholder"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/product_shelf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Shelf placeholder" />
</LinearLayout>
结果就是没有文本截断。:-)
SearchableInfo
的文档说它应该用于在不同的应用程序之间进行搜索。如果您已经使用游标适配器并显示建议,那么更改getView
中使用的布局就很简单了。也许您目前正在使用默认的android.R.layout...
布局。 - darnmason