gpt4 book ai didi

android - 在 arrayAdapter ListView 中突出显示搜索到的文本

转载 作者:行者123 更新时间:2023-11-29 00:52:24 26 4
gpt4 key购买 nike

是否有任何突出显示我正在使用的搜索文本 arrayAdapter ListView

这是我的代码

  private TextWatcher filterTextWatcher = new TextWatcher() {

public void afterTextChanged(Editable s) {
}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}

public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s);

}
};

protected void onPostExecute(final List<Site> sites) {
progressBar.hide();
adapter = new ArrayAdapter(context, R.layout.alert_dialog_single_choice_list_item,sites);
list.setAdapter(adapter);
list.setItemChecked(selectedSite, true);
list.setSelection(selectedSite);
filterText.setVisibility(View.VISIBLE);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()

}

这是onViewCreated方法

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
filterText = (EditText) view.findViewById(R.id.et_search_state);
filterText.addTextChangedListener(filterTextWatcher);
list = (ListView) view.findViewById(R.id.lv_state);
progressBar = view.findViewById(R.id.progressBar);
final ImageButton btnClear = view.findViewById(R.id.btn_clear);
btnClear.setOnClickListener(this);
new BindDataAsync().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
filterText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() > 0) {
btnClear.setVisibility(View.VISIBLE);
} else {
btnClear.setVisibility(View.INVISIBLE);
}
}

@Override
public void afterTextChanged(Editable editable) {

}
});

}

我有支票this但这里他们使用的是自定义 adapter;

最佳答案

尝试以下:

  1. 添加全局字段,String mSearchText = "";

  2. 在调用过滤器 (adapter.getFilter().filter(s);) 之前,添加 mSearchText = s.toString();

  3. 改变

adapter = new ArrayAdapter(context, R.layout.alert_dialog_single_choice_list_item,sites);

    adapter = new ArrayAdapter(context, R.layout.alert_dialog_single_choice_list_item,sites){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text;
text = (TextView)super.getView(position, convertView, parent);
String fullText = getItem(position);
// highlight search text
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 text;
}
};

希望对您有所帮助!

关于android - 在 arrayAdapter ListView 中突出显示搜索到的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58077740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com