Java SearchView过滤器不使用';我们不能产生预期的结果
当我在搜索框中输入文本时,在搜索框中解决问题后,过滤器仍然不起作用 这是我的代码:Java SearchView过滤器不使用';我们不能产生预期的结果,java,android,searchview,Java,Android,Searchview,当我在搜索框中输入文本时,在搜索框中解决问题后,过滤器仍然不起作用 这是我的代码: 包intikom.streammobile.Customer; 导入android.app.ProgressDialog; 导入android.app.SearchManager; 导入android.content.Context; 导入android.content.Intent; 导入android.content.SharedReferences; 导入android.os.Bundle; 导入andr
包intikom.streammobile.Customer;
导入android.app.ProgressDialog;
导入android.app.SearchManager;
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.SearchView;
导入android.text.TextUtils;
导入android.view.KeyEvent;
导入android.view.Menu;
导入android.view.MenuInflater;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ListView;
导入android.widget.Toast;
导入org.json.simple.JSONArray;
导入org.json.simple.JSONObject;
导入org.json.simple.parser.JSONParser;
导入org.json.simple.parser.ParseException;
导入java.util.ArrayList;
导入java.util.List;
进口intikom.streammobile.Index;
进口intikom.streammobile.R;
导入intikom.streammobile.Utility.bigprocess;
导入intikom.streammobile.Utility.RESTClient;
导入intikom.streammobile.Workflow.MyRequest.ListRequestItem;
导入intikom.streammobile.Workflow.WorkflowActivity;
公共类ListCustomer扩展AppCompatActivity实现SearchView.OnQueryTextListener{
私有静态最终字符串ARG\u SECTION\u NUMBER=“SECTION\u NUMBER”;
私有静态字符串服务;
私有静态字符串行页面;
私有静态ListView lv;
私有静态SharedReferences优先;
私人名单;
私人名单2;
私有ListCustomerItem项目;
私有静态字符串customerID;
公共列表客户(){
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u list\u customer);
URLService=getString(R.string.URLService);
最终ListView lv=(ListView)findViewById(R.id.lvCustomerList);
lv.setTextFilterEnabled(真);
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
item=新的ListCustomerItem();
item=mItems.get(位置);
SharedPreferences pref=getApplicationContext().getSharedPreferences(“MyPref”,MODE_PRIVATE);
SharedReferences.Editor=pref.edit();
editor.putString(“CustomerID”,item.CustID);
editor.putString(“CustomerName”,item.CustName);
commit();
Intent i=新的Intent(getApplicationContext(),WorkflowActivity.class);
i、 putExtra(“客户名称”,item.CustName);
setResult(1000,i);
完成();
}
});
getCustomer();
getLocal();
if(mItems==null)
{
if(mItems2==null){
Toast.makeText(此“Internet连接似乎处于脱机状态”,
Toast.LENGTH_LONG).show();
}
其他的
{
lv.setAdapter(新的ListCustomerAdapter(this,mItems2));
}
}
其他的
{
lv.setAdapter(新的ListCustomerAdapter(this,mItems));
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.searchbox,菜单);
搜索管理器搜索管理器=
(SearchManager)getSystemService(Context.SEARCH\u服务);
搜索视图搜索视图=
(SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
getSearchableInfo(getComponentName());
searchView.setSubmitButtonEnabled(true);
setOnQueryTextListener(这个);
返回true;
}
@凌驾
公共布尔值onQueryTextSubmit(字符串查询){
返回false;
}
@凌驾
公共布尔onQueryTextChange(字符串newText){
if(TextUtils.isEmpty(newText))
{
lv.clearTextFilter();
}
其他的
{
lv.setFilterText(newText.toString());
}
返回true;
}
}
我认为您的代码:
//在代码中的一些地方(如onCreate
)定义适配器如下:
ListCustomerAdapter adapter = new ListCustomerAdapter(this, mItems2);
...
然后将onQueryTextChange
方法更改为如下内容:
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText))
{
adapter.getFilter().filter("");
lv.clearTextFilter();
}
else
{
lv.setFilterText(newText);
adapter.getFilter().filter(newText);
}
return true;
}
您可能还想查看。
我希望这能有所帮助。我想问题出在您的适配器上。