- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
出现在我的 SearchView
下方的默认 ListView
中的建议包含被截断的文本。我希望文本完整显示(必要时分多行显示)。
我想出了两种可能的方法来解决这个问题,但是网上没有找到示例,我希望这里有人可以提供帮助...
方法 #1/Q1:如何直接访问和修改包含 SUGGEST_COLUMN_TEXT_1
和 SUGGEST_COLUMN_TEXT_1
文本的 TextView 的外观?
方法 #2/Q2:或者,SearchView 有一个 setSuggestionsAdapter(CursorAdapter adapter)
方法,看起来它可能(更?)比方法 #1 更合适。虽然我已经阅读了 CursorAdapters 并且已经在我的应用程序中实现了一个,但我不确定如何为 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>
...结果是没有文本截断。 :-)
最佳答案
您似乎想要为您的搜索 View 结果自定义布局。我将尝试在下面概述一些明确的步骤:
要使搜索 View 正常工作,我们需要在 res/xml 文件夹
中有一个 searchable.xml
和一个位于以下位置的 content provider
所有时间。
searchable.xml 示例:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:searchSuggestAuthority="com.example.searchProvider" ></searchable>
label
和 searchSuggestAuthority
是必需的。 searchSuggestAuthority
应该指向内容提供者的位置
将 intent-filter 元数据添加到 list 中的 Activity 。示例:
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
在 Activity/fragment 中,获取您的 searchView 对象并将其传递给自定义适配器以显示自定义布局。
searchView.setSearchableInfo(manager.getSearchableInfo(getActivity().getComponentName()));
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
//DO whatever you want here on text submit in the search View
return true;
}
@Override
public boolean onQueryTextChange(String textChange) {
searchView.setSuggestionsAdapter(new ExampleAdapter(context,yourData));
}
};
确保自定义适配器扩展了 cursorAdapter。
public class ExampleAdapter extends CursorAdapter {
private Cursor cursor;
private TextView text;
public ExampleAdapter(Context context, Cursor cursor) {
super(context, cursor);
this.cursor= cursor;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
text.setText((cursor.getString(cursor.getColumnIndex(YOUR_STRING_KEY));));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item, parent, false);
text = (TextView) view.findViewById(R.id.text);
return view;
}
}
在上面的代码中,R.layout.item
指的是您的自定义 xml
文件,您可以使用该文件在搜索 View 结果中进行扩充。
但请确保您使用的是 content provider
。 searchView
没有它就无法工作。即使您只是临时缓存数据也没关系。
希望这对您有所帮助!
关于Android - 防止 SearchView 建议中的文本截断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27671954/
我有一个 1850-2005 年月地表气温的 netCDF 文件。如何在 unix 中截断文件,以便新文件的时间维度从 1855 年到 2005 年?反之亦然,截断文件,使其改为 1850-2000?
我收到此错误 Bulk load data conversion error (truncation) for row 1, column 12 (is_download) 这是 csv...它只有一
我有一个列表,它是电子邮件正文,每行末尾都有一个日期和时间戳。邮票的格式是一致的,所以可以从右边计算表达式。以下是示例数据: Dear Volunteer2018-05-21 19:59:15 You
我正在使用内置 truncatewords_html Django 的过滤器,它在最后添加了“...”,相反,我想用“查看更多”链接替换它。 我怎样才能做到这一点? 最佳答案 最好编写自己的过滤器。您
我正在使用 SQL 加载器将我的数据加载到数据库中。 在插入数据之前,我需要删除表中的现有数据: options(skip=1,load=250000,errors=0,ROWS=30000,BIND
我正在尝试掌握消息队列的窍门。由于某种原因,当显示我在控制台中输入的消息时,字符串有时会被截断或更改。有谁知道为什么会发生这种情况? void *readFromQueue() { int r
我正在使用mediawiki API(例如http://en.wikipedia.org/w/api.php),我希望能够“截断”mysql表以便重置本地安装,同时保留一些表(用户,?...) 。SQ
想要截断一个存在的表: IF EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'mytable') TRUNCATE
我正在实现一个使用 Python Numpy 包的程序。我正在尝试修改数组的元素,以便我只需采用 elem[i][j] 并将其设置为 elem[i][j]/10。但是,我不断收到某种截断,其中元素在操
我有一个 64 位 long int,其中包含一些位域。我需要将存储在第二个和第三个字节中的 16 位带符号整数添加到一个 32 位值中。我正在使用这样的东西: u32 Function( s32 v
我有这样的文字:“我的文字是 blabla blabla, lala lala”。 我希望在我的 UILabel 中有这样的文本:“My text is ...lala”。 如何配置我的 UILabe
Umbraco Truncate 似乎不适合我,当我使用正确的代码(根据互联网)时,它会不断出错。我不知道它有什么问题。 错误代码: Compiler Error Message: CS1502: T
早些时候,我是使用 JS 动态地完成它的。但是我们遇到了一些性能问题,因为我们必须提供一个替代选项。 我现在使用文本溢出样式截断选项卡名称上的长文本。 但我有一个小问题,如果有人能解决的话 目前这是我
使用Rspec时截断、事务和删除数据库策略有什么区别?我找不到任何资源来解释这一点。我阅读了 Database Cleaner 自述文件,但它没有解释它们各自的作用。 为什么我们必须对 capybar
当然, 尚有诸位前辈也曾把以上三种方案结合一二, 以达到更广泛的适应度. 不过, 这厢先前在网路上搜索许久, 却未曾寻到三种方案合为一体的尝试, 于是只好自己动手写一下了:) 没有dem
有没有办法截断HSQLDB中的所有表? 如果这不可能,是否有任何方法可以级联删除具有外键引用的表? 最佳答案 可以截断模式中的所有表: 截断模式并提交 此命令有一些选项在指南中有描述: http://
我有一个要截断的时间戳。我正在使用 trunc oracle中的函数。这似乎做了我想要的但是从文档中它应该只接受日期而不是时间戳 select TRUNC(TO_DATE('22-AUG-13'),
我读到一旦你耗尽了一个节点,你就可以删除文件然后重新启动。它工作正常, 但我只是通过排空所有节点,关闭整个集群,删除文件并重新启动来尝试它。 如果我一次只重启一个节点会怎样?据我了解有风险 重新启动的
我想截断 d3 中超过预定义限制的文本。 我不知道该怎么做。 这是我现在所拥有的: node.append("text") .attr("dx", 20) .attr("dy", ".20
嗨 Guyz 我有一个固定宽度的 WPF TextBlock 说 100 ,如果字符串不适合宽度,则最后一个字符总是被截断,因为所有字符的大小都不相同。我不想剪切字符而是我想从那里跳过文本并只显示没有
我是一名优秀的程序员,十分优秀!