Searching in Listview with searched textcolor highlighted in Listview Android

前端 未结 4 1476
终归单人心
终归单人心 2021-01-17 04:23

I Have a listview with arrayadapter .. i need to implement this in my music application ... help me out

4条回答
  •  佛祖请我去吃肉
    2021-01-17 04:55

    first add two function getData and set Data Search to baseadapter

       public class bsAdapter extends BaseAdapter {
        Activity cntx;
    
        public bsAdapter(Activity context) {
            // TODO Auto-generated constructor stub
            this.cntx = context;
    
        }
    
        public int getCount() {
            // TODO Auto-generated method stub
            return AllSongs.size();
        }
    
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return AllSongs.get(position);
        }
    
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return AllSongs.size();
        }
        public void setDataSearch(String data) {
                this.search = data;
        } 
    
        public String getData() {
            return this.search;
        }
    

    first declare searchData in Search_class

    public String searchData;
    private bsAdapter mAdapter;
    

    and declare

    mAdapter = new bsAdapter(this);
    

    then you add condition in text watcher

      public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                textlength = et.getText().length();
                AllSongs.clear();
                for (int i = 0; i < AllSongsArray.length; i++) {
                    if (textlength <= AllSongsArray[i].length()) {
                        if (AllSongsArray[i].toLowerCase().contains(
                                et.getText().toString().toLowerCase().trim())) {
                            AllSongs.add(AllSongsArray[i]);
                        }
                    }
                  if (textlength == 0) {
    
                         mAdapter.setDataSearch(null);
    
                    } else {
    
                        searchData = s.toString().toLowerCase();
                        mAdapter.setDataSearch(SearchData);
                    }
    
             }
                AppendList(AllSongs);
            }
    

    and then put this function in baseadapter

    public static CharSequence highlight(String search, String originalText) {
    
           String normalizedText = Normalizer.normalize(originalText, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "").toLowerCase();
    
           int start = normalizedText.indexOf(search);
    
           Spannable highlighted = new SpannableString(originalText);
            if (start < 0) {
                // not found, nothing to to
                return originalText;
            } else {
    
                while (start >= 0) {
    
                    int spanStart   = Math.min(start, originalText.length());
                    int spanEnd     = Math.min(start + search.length(), originalText.length());
    
                    highlighted.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    highlighted.setSpan(new ForegroundColorSpan(cntx.getResources().getColor(R.color.text_color_white)), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    start = normalizedText.indexOf(search, spanEnd);
                }
    
                return highlighted;
            }
        }
    

    and put this

    public View getView(final int position, View convertView,
                ViewGroup parent) {
            View row = null;
    
            LayoutInflater inflater = cntx.getLayoutInflater();
            row = inflater.inflate(R.layout.listview_item, null);
    
            TextView tv = (TextView) row.findViewById(R.id.tv_artist_name);
    
            if(search != null){
                    tv.setText(highlight(search,AllSongs.get(position));
                }
                else if (search == null){
                    tv.setText(AllSongs.get(position));
    
                }
    
    
            return row;
    

提交回复
热议问题