包含多个字词的Android搜索

时间:2016-12-07 06:06:53

标签: android searchview

我必须实现多个搜索查询,如何实现它,我知道用搜索视图过滤文本但不知道如何用多个字符串进行搜索的基本思路,我附上快照显示它会让你感觉更好。

我正在从网络服务器加载数据。Click Here to see

TruckInfo列是:

  • 卡车名称
  • 总容量(是卡车的容量)
  • 状态忙/闲(当前所有卡车都是免费的卡车状态)
  • 自由位置(卡车即将免费)。

Here is search implementation i have to do

现在在快照第二,我必须过滤此数据,我已在容量列中搜索 - > 10吨至45吨,状态为 - >免费,免费的位置是 - >简历,从7wed2016到2016 Fri 2016然后我点击搜索按钮。

预期输出不应显示任何结果,因为:

  1. 10吨到45吨,有2辆卡车..这是对的。作为来自服务器的数据
  2. 卡车的状态是免费的,这是对的
  3. 免费位置是我搜索过的简历,但是你可以看到两辆卡车都有免费的位置mumbais,chandigarh,所以这个条件是假的所以整体搜索应该没有数据
  4. 我的尝试:

    filter_search.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    Log.e("filter_search","filter_search");
                    Localytics.tagEvent("Trucks Search Screen");
                    if(frmcap.getText().toString().length()==0){
                        frmcap_val=0;
                        cap_bool=1;
                    }
                    else
                    {
                        frmcap_val=Integer.parseInt(frmcap.getText().toString());
                        cap_bool=0;
                    }
                    if(tocap.getText().toString().length()==0) {
                        tocap_val=0;
                        cap_bool = 1;
                    }
                    else {
                        tocap_val=Integer.parseInt(tocap.getText().toString());
                        cap_bool=0;
                    }
                    if(from_loc_text.getText().length()==0)
                        loc_bool=1;
                    else
                        loc_bool=0;
    
                    if(frm_date_value.length()==0 || to_date_value.length()==0)
                        date_bool=1;
                    else
                        date_bool=0;
    
                    if(free.isChecked())
                        status_value="true";
                    if(busy.isChecked())
                        status_value="false";
                    if(any.isChecked())
                        status_value="any";
                    if(!any.isChecked()&&!free.isChecked()&&!busy.isChecked()){
                        Log.e("@@inside","inside");
                        status_value="-default value";
                    }
    
    
                    if(!any.isChecked()&&!free.isChecked()&&!busy.isChecked())
                        status_bool=1;
    
                    if(search.getText().length()==0)
                        loc_search_info=1;
                    else
                        loc_search_info=0;
    
                    try {
                        if(status_value!=null && status_value.length()>0 && !status_value.equalsIgnoreCase("any")){
                            truckadapter.advanceSearchFilter(status_value,"checkBox");
                            list_mytrucks.setVisibility(View.VISIBLE);
    
                        }else if(status_value!=null && status_value.equalsIgnoreCase("any")){
                            list_mytrucks.setVisibility(View.VISIBLE);
                        }else if(frmcap.getText().toString()!=null && frmcap.getText().toString().length()>0){
                            truckadapter.advanceSearchFilter(frmcap_val+"","capacity");
                            list_mytrucks.setVisibility(View.VISIBLE);
                        }else if(tocap.getText().toString()!=null && tocap.getText().toString().length()>0){
                            truckadapter.advanceSearchFilter(tocap_val+"","capacity");
                            list_mytrucks.setVisibility(View.VISIBLE);
                        }else if(current_location.getText().length()>=0){
                            truckadapter.advanceSearchFilter(current_location.getText().toString(),"freeLocation");
                            list_mytrucks.setVisibility(View.VISIBLE);
                        }else if(fromdt.getText().length()>=0){
                            truckadapter.advanceSearchFilter(current_location.getText().toString(),"dateStamp");
                            list_mytrucks.setVisibility(View.VISIBLE);
                        }else if(todt.getText().length()>=0){
                            truckadapter.advanceSearchFilter(current_location.getText().toString(),"dateStamp");
                            list_mytrucks.setVisibility(View.VISIBLE);
                        }
                        view_advance_search.setVisibility(View.GONE);
    
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
    
    
                }
            });
    

    这是getViewMethod中的searchFiletring:

    public void advanceSearchFilter(String constraint,String tag) throws JSONException {
                items.clear();
                if(constraint.isEmpty()){
                    items.addAll(advanceSearchArrayList);
                } else{
                    constraint = constraint.toUpperCase();
                    for(int i=0;i<advanceSearchArrayList.size();i++){
                        JSONObject jsonData = new JSONObject(String.valueOf(advanceSearchArrayList.get(i).getVehicle()));
                        if(tag.equalsIgnoreCase("capacity")){
                            int truckCapacity=Integer.parseInt(constraint);
                            if(Integer.parseInt(jsonData.getJSONObject("type").getString("capacity").toUpperCase())>=truckCapacity){
                                items.add(advanceSearchArrayList.get(i));
                            }
                        }
                        if(tag.equalsIgnoreCase("checkBox")){
                            if(advanceSearchArrayList.get(i).getFree().toUpperCase().contains(constraint)){
    
                                items.add(advanceSearchArrayList.get(i));
                            }
                        }
                        if(tag.equalsIgnoreCase("freeLocation")){
                            if(advanceSearchArrayList.get(i).getFree().toUpperCase().equalsIgnoreCase("true")){
                                Log.e("getFree_loc",""+advanceSearchArrayList.get(i).getFree_loc());
                                if(advanceSearchArrayList.get(i).getFree_loc().toUpperCase().contains(constraint)){
                                    items.add(advanceSearchArrayList.get(i));
                                }
    
                            }
    
                        }
                       if(tag.equalsIgnoreCase("dateStamp")){
                           SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
                           Date date = null;
                           try {
                               date = formatter.parse(constraint);
                           } catch (ParseException e) {
                               e.printStackTrace();
                           }
                           try {
                               if(formatter.parse(advanceSearchArrayList.get(i).getFree().toUpperCase()).before(date) ){
                                   items.add(advanceSearchArrayList.get(i));
                               }
                           } catch (ParseException e) {
                               e.printStackTrace();
                           }
    
                       }
    
    
                    }
                }
                notifyDataSetChanged();
            }
    

1 个答案:

答案 0 :(得分:0)

您还需要明确地将每个过滤器与每个对象的属性进行比较,以查看它是否匹配。

只是流量的想法:

  1. 为过滤器输入创建一个类。当用户单击搜索按钮时,将所有过滤器值分配给该类。
  2. 在模型类中创建一个方法,如果任何过滤器值匹配,则返回true。这将过滤器类对象作为参数。
  3. 现在,当用户单击搜索按钮时,迭代数组中的每个对象。调用此方法。如果返回true,则在新的过滤器数组中添加此对象。
  4. 使用该过滤器数组显示搜索结果。

    示例代码

    ///////// Filter class
    class FilterTrucks {
    
    String location;
    int capacityStart;
    int capacityEnd;
    Arraylist<String> availability;
    
    }
    
    ////// You model class
    class Mytrucks {
    
    String name;
    String location;
    int capacity;
    String availability;
    
    boolean matchFilter(FilterTrucks filter) {
    
    boolean locationMatch = filterMatchesLocation(filter);
    if(locationMatch)
    return true;
    
    boolean matchCapacity = filterMatchesCapacity(filter);
    if(matchCapacity)
    return true;
    
    return false;
    
    }
    
    boolean filterMatchesLocation(FilterTrucks filter)  {
    return location.contains(filter.location);
    }
    
    boolean filterMatchesCapacity(FilterTrucks filter)  {
    return  (capacity >= filter.capacityStart ||  capacity <= filter.capacityEnd)
    }
    
    ////////// Your activity
    
    Arraylist<Mytrucks> filterArray = new Arraylist<>();
    
    filter_search.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
    //create filter object from user selected fields
    FilterTrucks filter = new FilterTrucks();
    filter.location = txtLocation.getText().toString(); //for example
    
    for(Mytrucks truck : arraylist) {
    if(truck.matchFilter(filter)) 
        filterArray.add(truck);
    }
        }
    });
    
    //use filter array to populate recycler view
    

    希望这有帮助!