gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:14:42 26 4
gpt4 key购买 nike

目前,在我的 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();
}

}


}

问题已编辑。现在看看我做了什么。此代码过滤填充结果中的字符串。现在,如果该字符串不在列表中,它应该开始在服务器上搜索它。就像普通的搜索一样。谢谢你们的帮助。非常感激。

最佳答案

如评论中所述,当我得到过滤结果列表大小为 0 时,我尝试在服务器上触发搜索结果的查询。然后根据该结果更新适配器。

关于java - 我想实现一个搜索栏,它将接受一个字符串,然后它将过滤填充的 ListView 或在服务器上搜索该字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47472643/

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