Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在ListView中突出显示搜索结果_Android_Search_Highlight - Fatal编程技术网

Android 如何在ListView中突出显示搜索结果

Android 如何在ListView中突出显示搜索结果,android,search,highlight,Android,Search,Highlight,我不明白如何突出显示搜索结果 我试图借助SpannableString之类的工具来实现它,但我不知道如何在我的案例中实现它。第mTextView.setText(spannable) public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.search); //列表视图数据 最终字符串[]产品=新字符串[]{ getResources(

我不明白如何突出显示搜索结果

我试图借助
SpannableString
之类的工具来实现它,但我不知道如何在我的案例中实现它。第
mTextView.setText(spannable)

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
//列表视图数据
最终字符串[]产品=新字符串[]{
getResources().getString(R.string.t1_2),
getResources().getString(R.string.t1_3),
getResources().getString(R.string.t1_4),
};
lv=(ListView)findViewById(R.id.list\u视图);
inputSearch=(EditText)findViewById(R.id.inputSearch);
mTextView=(TextView)findViewById(R.id.product_name);
//向listview添加项目
适配器=新阵列适配器(此,R.layout.list\u项,R.id.product\u名称,产品);
低压设置适配器(适配器);
lv.setVisibility(视图已消失);
//一旦用户在EditText中输入新数据,我们需要从中获取文本
//并将其传递给阵列适配器筛选器。
inputSearch.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列cs、int arg1、int arg2、int arg3){
//当用户更改文本时
Search.this.adapter.getFilter().filter(cs);
textlength=inputSearch.getText().length();
//适配器。清除();
如果(textlength!=0){
lv.setVisibility(视图可见);
对于(int i=0;iif(textlength)mTextView与您的listview有什么关系?使用新的spanEnable设置文本三次有什么意义?具体是哪一个写入错误?请重新阅读您的链接,并意识到,如上所述,您应该在getView()中以另一种样式设置文本。您还应该意识到,您希望突出显示搜索结果,但不应首先将其过滤掉。@greenapps mTextView这是我的ListView布局文件只有TextView id=product\u name…我需要什么?创建新适配器并在getView()中写入span?
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    // Listview Data
    final String[] products = new String[] {
        getResources().getString(R.string.t1_2),
        getResources().getString(R.string.t1_3),
        getResources().getString(R.string.t1_4),
    };

    lv = (ListView) findViewById(R.id.list_view);
    inputSearch = (EditText) findViewById(R.id.inputSearch);
    mTextView = (TextView) findViewById(R.id.product_name);

    // Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.list_item,R.id.product_name, products);
    lv.setAdapter(adapter);
    lv.setVisibility(View.GONE);

    // Once user enters a new data in EditText we need to get the text from
    // it and passing it to array adapter filter.
    inputSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
    // When user changed the Text

        Search.this.adapter.getFilter().filter(cs);

        textlength = inputSearch.getText().length();
        // adapter.clear();
        if (textlength != 0) {
            lv.setVisibility(View.VISIBLE);
            for (int i = 0; i < products.length; i++) {
                if (textlength <= products[i].length()) {
                    Spannable spannable = new SpannableString(inputSearch.getText().toString());
                    ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE });
                    TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);
                    spannable.setSpan(highlightSpan, 0,5 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    mTextView.setText(spannable);
                }
            }
        }
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub

    }
});
}
}