searchview结果列表视图,但不突出显示结果

时间:2017-07-01 18:48:39

标签: android listview search highlight spannable

我创建了一个搜索视图,当我输入一个单词并按下键盘上的ENTER时,结果会在3或4秒内显示在列表视图中。所以我想插入一个progres微调器,直到填充结果。此外,我想尝试在列表视图中的结果中突出显示搜索词。我使用" SpannableString highlightKeyword"但无法突出显示结果搜索词,我已经尝试了很多方法,其次是几个网站,但没有任何事情发生。我无法弄清楚错误在哪里。这是我的代码:     mainactivity.java:

 #menu{
    }
    #ulmain{
    }
    #limain{
        list-style:none;
    }
    #ulsub {
        visibility:hidden;
    }
    #lisub{
        list-style:none;
        display:inline-block;
    }
    #ulmain:hover #ulsub{
        visibility:visible;
    }
    #ulsubsub {
        visibility:hidden;
    }
    #lisub:hover #ulsubsub{
        visibility:visible;
    }
    #lisubsub{
        list-style:none;
        display:inline-block;
    }
    #menu a{
        background-color:pink;
        text-decoration:none;
    }
    #ulmain, #ulsub{
        display:block;
    }

===== ListviewAdapter.java:

  public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {

// Declare Variables
ListView list;
ListViewAdapter adapter;
SearchView editsearch;
String[] animalNameList;
ArrayList<AnimalNames> arraylist = new ArrayList<AnimalNames>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Resources res = getResources();

    String[] animalNameList = res.getStringArray(R.array.animalNameList);



    // Locate the ListView in listview_main.xml
    list = (ListView) findViewById(R.id.listview);

    for (int i = 0; i < animalNameList.length; i++) {
        AnimalNames animalNames = new AnimalNames(animalNameList[i]);
        // Binds all strings into an array
        arraylist.add(animalNames);
    }

    // Pass results to ListViewAdapter Class
    adapter = new ListViewAdapter(this, arraylist);

    // Binds the Adapter to the ListView
    list.setAdapter(adapter);

    // Locate the EditText in listview_main.xml
    editsearch = (SearchView) findViewById(R.id.search);
    editsearch.setOnQueryTextListener(this);

}

@Override
    public boolean onQueryTextSubmit(String newText) {
    String text = newText;
    adapter.filter(text);

    return false;
}

@Override
public boolean onQueryTextChange(String newText) {

    return false;
}

先谢谢。

1 个答案:

答案 0 :(得分:0)

Alhamdulillah,最后我在google上找到了答案。在getview()方法中的自定义适配器中高亮显示。如下:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    TextView text;

    if (convertView == null) {
        view = mInflater.inflate(R.layout.list_item, null);
    } else {
        view = convertView;
    }

    try {
        if (mFieldId == 0) {
            //  If no custom field is assigned, assume the whole resource is a TextView
            text = (TextView) view;
        } else {
            //  Otherwise, find the TextView field within the layout
            text = (TextView) view.findViewById(mFieldId);
        }
    } catch (ClassCastException e) {
        Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
        throw new IllegalStateException(
                "ArrayAdapter requires the resource ID to be a TextView", e);
    }

    // HIGHLIGHT...



    String fullText = getItem(position);
    if (mSearchText != null && !mSearchText.isEmpty()) {
        int startPos = fullText.toLowerCase(Locale.US).indexOf(mSearchText.toLowerCase(Locale.US));
        int endPos = startPos + mSearchText.length();

        if (startPos != -1) {
            Spannable spannable = new SpannableString(fullText);
            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, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            text.setText(spannable);

        } else {
            text.setText(fullText);
             }
    } else {
        text.setText(fullText);


    }


    return view;
}