我必须实现多个搜索查询,如何实现它,我知道用搜索视图过滤文本但不知道如何用多个字符串进行搜索的基本思路,我附上快照显示它会让你感觉更好。
TruckInfo列是:
现在在快照第二,我必须过滤此数据,我已在容量列中搜索 - > 10吨至45吨,状态为 - >免费,免费的位置是 - >简历,从7wed2016到2016 Fri 2016然后我点击搜索按钮。
预期输出不应显示任何结果,因为:
我的尝试:
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();
}
答案 0 :(得分:0)
您还需要明确地将每个过滤器与每个对象的属性进行比较,以查看它是否匹配。
只是流量的想法:
使用该过滤器数组显示搜索结果。
///////// 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
希望这有帮助!