Android的 - 在搜索查看建议prevent文本截断? [英] Android - Prevent text truncation in SearchView suggestions?

查看:158
本文介绍了Android的 - 在搜索查看建议prevent文本截断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是出现在默认的ListView 下方的建议,我的搜索查看包含被截断的文本。我想显示文本的全部内容(在多行如果需要的话)。

我想出了解决这个,但是,有没有例子两种可能的方式在网络上被发现的,我希望有人在这里也许可以帮助...

办法#1 / Q1:我怎么能直接访问和修改保存TextViews的外观 SUGGEST_COLUMN_TEXT_1 SUGGEST_COLUMN_TEXT_1 文本?

办法#2 / Q2:(?更多)另外,搜索查看有一个 setSuggestionsAdapter(CursorAdapter的适配器)方法,看起来可能是合适的做法比1。虽然我已经读了CursorAdapters和已经有一个在我的应用程序实现的,我不知道我会怎样配置一个用于搜索查看(特别是在进入光标而言),所以任何人都可以帮我出一些一般性的指导意见或者一个骷髅的例子?

下面是从我SearchViewFragment类的现有code:

 公共类SearchViewFragment扩展片段{

公共SearchViewFragment(){
}

@覆盖
公共无效的onCreate(包savedInstanceState){

    super.onCreate(savedInstanceState);
}

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
    捆绑savedInstanceState){

    //充气的布局该片段
    查看fragmentView = inflater.inflate(R.layout.fragment_search_view,集装箱,假);

    //使用搜索管理器找到与此相关的活动的SearchableInfo
    SearchManager searchManager =(SearchManager)getActivity()getSystemService(Context.SEARCH_SERVICE)。
    SearchableInfo searchableInfo = searchManager.getSearchableInfo(getActivity()getComponentName());

    //绑定活动的SearchableInfo到搜索查看
    搜索查看搜索查看=(搜索查看)fragmentView.findViewById(R.id.searchView);
    sea​​rchView.setSearchableInfo(searchableInfo);
    sea​​rchView.setIconifiedByDefault(假);
    sea​​rchView.setSubmitButtonEnabled(真正的);
    //searchView.setQueryRefinementEnabled(true);

    返回fragmentView;
}
 

}

更新:解决

由于接受的答案,我创造了这个code这是pretty的干净,做的工作做好......

 公共类SearchViewFragment扩展片段{

    私有静态最后弦乐LOG_TAG = SearchViewFragment.class.getSimpleName();

    公共SearchViewFragment(){
    }

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        setRetainInstance(真正的); // TODO  - 不工作 - 记住搜索字词
        super.onCreate(savedInstanceState);
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){

        //充气的布局该片段
        查看fragmentView = inflater.inflate(R.layout.fragment_search_view,集装箱,假);

        //使用搜索管理器找到与此相关的活动的SearchableInfo
        SearchManager searchManager =(SearchManager)getActivity()getSystemService(Context.SEARCH_SERVICE)。
        SearchableInfo searchableInfo = searchManager.getSearchableInfo(getActivity()getComponentName());

        //绑定活动的SearchableInfo到搜索查看
        最后搜索查看搜索查看=(搜索查看)fragmentView.findViewById(R.id.searchView);
        sea​​rchView.setSearchableInfo(searchableInfo);
        sea​​rchView.setIconifiedByDefault(假);
        sea​​rchView.setSubmitButtonEnabled(真正的);
        //searchView.setQueryRefinementEnabled(true);

        sea​​rchView.setOnQueryTextListener(新SearchView.OnQueryTextListener(){
            @覆盖
            公共布尔onQueryTextSubmit(String s)将{
               //做任何你想在这里的文本提交搜索查看
                Log.d(LOG_TAGonQueryTextSubmit(+ S +));

                返回true;
            }
            @覆盖
            公共布尔onQueryTextChange(字符串textChange){

                Log.d(LOG_TAGonQueryTextChange(+ textChange +));

                ContentResolver的CR = getActivity()getContentResolver()。

                开放的我们的uri = DbContentProvider.CONTENT_URI_REAL_PRODUCTS;
                的String []投影= DbContentProvider.getProjectionIn(DbContentProvider.REAL_PRODUCTS_SUGGEST);
                串选择= DbContentProvider.getSelection(假);
                的String [] selectionArgs两个= {Utilities.formatQueryString(textChange)};
                字符串排序顺序= NULL;
                光标光标= cr.query(URI,投影,选择,selectionArgs两个,排序顺序);
                Log.d(LOG_TAG,设置setSuggestionsAdapter光标:+光标);
                sea​​rchView.setSuggestionsAdapter(新SearchSuggestionsAdapter(getActivity(),光标));

                返回true;

            }
        });

        返回fragmentView;
    }

    私有静态类SearchSuggestionsAdapter扩展SimpleCursorAdapter {

        私有静态最后的String [] mVisible = {SearchManager.SUGGEST_COLUMN_TEXT_1,SearchManager.SUGGEST_COLUMN_TEXT_2};
        私有静态最终诠释[] mViewIds = {R.id.product_name,R.id.product_shelf};

        公共SearchSuggestionsAdapter(上下文的背景下,光标光标){

            超级(上下文,R.layout.search_view_suggestions,光标,mVisible,mViewIds,0);

        }
        / *
        @覆盖
        公共无效bindView(查看视图,上下文的背景下,光标光标){

            Log.d(LOG_TAGbindView(+视图+,+上下文+,+光标+));
            super.bindView(查看,背景,光标);

        }

        @覆盖
        公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){

            Log.d(LOG_TAGNewView的(+上下文+,+光标+,+父+));
            返回super.newView(背景下,光标,父母);

        }
        * /

    }

}
 

和这里是我的search_view_suggestions.xml ...

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin>

    <的TextView
        机器人:ID =@ + ID / PRODUCT_NAME
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=产品名称占位符
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:ID =@ + ID / product_shelf
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=产品的货架占位符/>

< / LinearLayout中>
 

...结果是没有文字截断。 : - )

解决方案

好像你想为你的搜索结果来看一个自定义布局。我会尽量勾勒下面的一些明确步骤:

为了使搜索视图的工作,我们需要有一个 searchable.xml RES / XML文件夹内容提供商在任何时候。

searchable.xml例如:

 <可搜索的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:标签=@字符串/ APP_NAME
    机器人:searchSuggestAuthority =com.example.searchProvider>< /搜索>
 

标签 searchSuggestAuthority 是必需的。该 searchSuggestAuthority 应指向内容提供者的位置

意向滤波器的元数据添加到清单中的活性。例如:

 <元数据的android:NAME =android.app.searchable安卓资源=@ XML /搜索/>
 

在活动/片段,让你的搜索查看对象,并通过它的自定义适配器用于显示自定义布局。

<$p$p><$c$c>searchView.setSearchableInfo(manager.getSearchableInfo(getActivity().getComponentName()));         最后SearchView.OnQueryTextListener queryTextListener =新SearchView.OnQueryTextListener(){             @覆盖             公共布尔onQueryTextSubmit(String s)将{                //做任何你想在这里的文本提交搜索查看                 返回true;             }  @覆盖             公共布尔onQueryTextChange(字符串textChange){                 sea​​rchView.setSuggestionsAdapter(新ExampleAdapter(上下文,yourData));             }         };

确认自定义适配器扩展CursorAdapter的。

 公共类ExampleAdapter扩展的CursorAdapter {

    私人光标指针;

    私人TextView的文字;

    公共ExampleAdapter(上下文的背景下,光标光标){

        超(背景下,光标);

        this.cursor =光标;

    }

    @覆盖
    公共无效bindView(查看视图,上下文的背景下,光标光标){

        text.setText((cursor.getString(cursor.getColumnIndex(YOUR_STRING_KEY))));

    }

    @覆盖
    公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){

        LayoutInflater充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        查看查看= inflater.inflate(R.layout.item,父母,假);

        文=(TextView的)view.findViewById(R.id.text);

        返回查看;

    }

}
 

在上面的code R.layout.item 指的是您的自定义 XML 文件,你可以使用在搜索查看结果膨胀。

请确保您虽然使用内容提供商。该搜索查看不无一部作品。不要紧,即使你只是缓存在临时的基础数据。

希望这有助于!

The suggestions that appear in the default ListView beneath my SearchView contain text that is truncated. I would like the text to be displayed in its entirety (on multiple lines if necessary).

I have come up with two possible ways to solve this but, with no examples to be found on the net, I was hoping someone on here may be able to help...

Approach #1 / Q1: How can I directly access and modify the appearance of the TextViews that hold the SUGGEST_COLUMN_TEXT_1 and SUGGEST_COLUMN_TEXT_1 text?

Approach #2 / Q2: Alternatively, SearchView has a setSuggestionsAdapter(CursorAdapter adapter) method which looks like it may be (more?) suitable than approach #1. Although I have read up on CursorAdapters and have one already implemented in my app, I'm not sure how I would configure one for the SearchView (esp. in terms of accessing the cursor), so could anyone help me out with some general guidance or a skeleton example?

Here is the existing code from my SearchViewFragment class:

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;
}

}

Update: SOLVED!

Thanks to the accepted answer, I have created this code which is pretty clean and does the job well...

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);

        }
        */

    }

}

And here is my 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>

...the result is no text truncation. :-)

解决方案

It seems like you want a custom layout for your search view results. I will try to outline some clear steps below:

To make the search view to work we need to have a searchable.xml in the res/xml folder and a content provider at all times.

searchable.xml example:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:searchSuggestAuthority="com.example.searchProvider" ></searchable>

The label and searchSuggestAuthority are required. The searchSuggestAuthority should point to the location of the content provider

Add the intent-filter meta-data to the activity in the manifest. Example:

  <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>

In the activity/fragment, get your searchView object and pass it the custom adapter for displaying the custom layout.

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));

            }
        };

Make sure the custom adapter extends the 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;

    }

}

In the above code R.layout.item refers to your custom xml file that you can use to inflate in the searchview results.

Make sure you are using a content provider though. The searchView doesn't work without one. It doesn't matter even if you are only caching the data on a temporary basis.

Hope this helps!

这篇关于Android的 - 在搜索查看建议prevent文本截断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆