首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在listview中搜索结果,但不突出显示结果

在listview中搜索结果,但不突出显示结果
EN

Stack Overflow用户
提问于 2017-07-02 02:48:39
回答 1查看 272关注 0票数 1

我已经创建了一个搜索视图,当我键入一个单词并从键盘上按回车键时,结果会在3到4秒内显示在列表视图中。所以我想在填充结果之前插入一个进度微调器。我还想在列表视图的结果中突出显示搜索词。我使用了"SpannableString highlightKeyword“,但无法突出显示结果搜索词,我尝试了许多方法,后面跟着几个网站,但什么都没有发生。我不知道错误出在哪里。下面是我的代码: mainactivity.java:

代码语言:javascript
运行
复制
  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;
}

===== ListviewAdapter.java:

代码语言:javascript
运行
复制
  public class ListViewAdapter extends BaseAdapter {

// Declare Variables

Context mContext;
LayoutInflater inflater;
private List<AnimalNames> animalNamesList = null;
private ArrayList<AnimalNames> arraylist;

public ListViewAdapter(Context context, List<AnimalNames> animalNamesList) {
    mContext = context;
    this.animalNamesList = animalNamesList;
    inflater = LayoutInflater.from(mContext);
    this.arraylist = new ArrayList<AnimalNames>();
    this.arraylist.addAll(animalNamesList);
}

public class ViewHolder {
    TextView name;
}

@Override
public int getCount() {
    return animalNamesList.size();
}

@Override
public AnimalNames getItem(int position) {
    return animalNamesList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.listview_item, null);
        // Locate the TextViews in listview_item.xml
        holder.name = (TextView) view.findViewById(R.id.name);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    // Set the results into TextViews
    holder.name.setText(animalNamesList.get(position).getAnimalName());


    return view;
}


// Filter Class
public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    animalNamesList.clear();
    if (charText.length() == 0) {
        animalNamesList.addAll(arraylist);
    } else {
        for (AnimalNames wp : arraylist) {
            if (wp.getAnimalName().toLowerCase(Locale.getDefault()).contains(charText)) {
                animalNamesList.add(wp);
            }
        }
    }
    notifyDataSetChanged();
}


////Here My problem starts : 

public static SpannableString highlightKeyword(CharSequence text, Pattern p, int fgcolor, int bgcolor) {
    SpannableString ss = new SpannableString(text);

    ColorStateList blueColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.BLUE});

    int start = 0;
    int end;
    Matcher m = p.matcher(text);
    while (m.find(start)) {
        start = m.start();
        end = m.end();

        BackgroundColorSpan bgspan = new BackgroundColorSpan(bgcolor);
        ss.setSpan(bgspan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ForegroundColorSpan fgspan = new ForegroundColorSpan(fgcolor);
        ss.setSpan(fgspan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        start = end;
    }

    return ss;
}


////But even did not the result word highlighted. 

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-07 21:22:22

安拉姆杜利拉,我终于在谷歌上找到了答案。getview()方法中自定义适配器中的高亮度。如下所示:

代码语言:javascript
运行
复制
@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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44864302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档