我想实现一个搜索栏,该搜索栏将接受一个字符串,然后它将过滤填充的ListView或在服务器上搜索该字符串



当前我的Android应用程序中,正在使用搜索栏,该搜索栏接受字符串并使用TextWatcher及其东西过滤填充列表。但是现在,如果我从列表中获得了空白,那么我需要在服务器端搜索相同的字符串。后端火基。该怎么做?让我知道您是否需要任何其他细节。

 searchview.setOnQueryTextListener(new 
    android.widget.SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            Log.d("new text 2",query);
            adapter.getFilter().filter(query);
            return false;
        }
        @Override
        public boolean onQueryTextChange(String newText) {
            Log.d("new text 1",newText);
            if (TextUtils.isEmpty(newText)) {
                listView.clearTextFilter();
            }
            adapter.getFilter().filter(newText);
            listView.setFilterText(newText);
            return false;
        }
    });

    @NonNull
       public Filter getFilter() {
          if (customFilter == null) {
              customFilter = new CustomFilter();
          }
          return customFilter;
      }

    //the class that creates the filter
    private class CustomFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            Log.d("inside perform filter",constraint.toString());
            FilterResults results = new FilterResults();
            // We implement here the filter logic
            if (constraint == null || constraint.length() == 0) {
                // No filter implemented we return all the list
                results.values = conn_List;
                results.count = conn_List.size();
            } else {
                // We perform filtering operation
                List<Connection_Class> newList = new ArrayList<>();
                for (Connection_Class p : conn_List) {
                    if (p.getName().toUpperCase().startsWith(constraint.toString().toUpperCase()))
                        newList.add(p);
                }
                results.values = newList;
                results.count = newList.size();
            }
            return results;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            Log.d("inside publishing",constraint.toString()+" "+ results.count);
            // Now we have to inform the adapter about the new list filtered
            if (results.count == 0) {
                notifyDataSetChanged();
                //notifyDataSetInvalidated();
                //Context context = getActivity();
                //CharSequence text = "Please Search Something Else...!";
                //int duration = Toast.LENGTH_SHORT;
                //Toast toast = Toast.makeText(context, text, duration);
            } else {
                connection_Array = (ArrayList<Connection_Class>) 
      results.values;
                Log.d("inside results",constraint.toString()+" "+ 
            connection_Array.size());
                  notifyDataSetInvalidated();
 }
        }

    }

问题已编辑。现在看看我所做的。该代码过滤填充结果中的字符串。现在,如果字符串不在列表中,则应开始在SEVE上搜索它。像普通的搜索。感谢您的帮助。非常感激。

如注释中所述,我尝试在我获得过滤结果列表尺寸0时触发查询以搜索服务器上的结果0。>

相关内容

最新更新