我正在尝试使用SearchView在我的Android应用中实现搜索功能。
问题是,一旦我在键盘上输入搜索文本,就没有任何反应。永远不会调用可搜索的意图。我必须遗漏导致搜索不能触发的内容。
public class FragmentClientes extends Fragment implements OnQueryTextListener, android.widget.SearchView.OnCloseListener {
private boolean searchCheck;
List<ClienteModel> clientes;
private ListView lv;
private View rootView;
private ProgressBar progressBar;
private LinearLayout footerLinearLayout;
public FragmentActivity activity;
private SearchView searchView;
private String currentQuery = null;
private ClientViewAdapter ad;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
rootView = inflater.inflate(R.layout._fragment_clientes, container, false);
rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT ));
return rootView;
}
private void gerarToast(CharSequence message) {
int duration = Toast.LENGTH_LONG;
Toast toast = Toast
.makeText(getActivity(), message, duration);
toast.show();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(Menus.PROCURAR));
if (searchView != null) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
public boolean onQueryTextSubmit(String submit) {
showResults(submit);
return false;
}
public boolean onQueryTextChange(String change) {
//TODO Auto-generated method stub
showResults(change);
return false;
}
});
}
searchView.setQueryHint(this.getString(R.string.search));
((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text))
.setHintTextColor(getResources().getColor(R.color.white));
menu.findItem(Menus.PROCURAR).setVisible(true);
searchCheck = true;
}
public boolean onClose() {
showResults("");
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case Menus.PROCURAR:
searchCheck = true;
break;
}
return true;
}
private void showResults(String query) {
try {
lv = (ListView) rootView.findViewById(R.id.listaClientes);
Repositorio mRepositorio = new Repositorio(getActivity());
mRepositorio.getClientes(1,5,query);
lv.setVerticalFadingEdgeEnabled(true);
lv.setVerticalScrollBarEnabled(true);
lv.addFooterView(footerLinearLayout);
lv.setOnScrollListener(new EndlessScrollListener(){
@Override
public void onLoadMore(int page, int totalItemsCount) {
new LoadMoreClientTask(progressBar,FragmentClientes.this,ad,getActivity()).execute(page);
}
});
lv.setAdapter(ad);
new LoadMoreClientTask(progressBar,this,ad,getActivity()).execute(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
getClientes函数:
public List<ClienteModel> getClientes(Integer pagina, Integer limit, String consulta) throws SQLException {
Integer offset = pagina * limit - limit;
List<ClienteModel> listaDeRegistros = new ArrayList<ClienteModel>();
if(consulta.isEmpty()) {
query = "SELECT * FROM " + tabelaCLIENTES + " WHERE credencial_id = " + mSessao.getString("id_credencial") + " LIMIT " + offset + ", " + limit;
}else {
query = "SELECT * FROM " + tabelaCLIENTES + " WHERE (credencial_id = " + mSessao.getString("id_credencial") + ") and (nome LIKE '%"+consulta+"%') LIMIT " + offset + ", " + limit;
}
System.out.println(query);
try {
Cursor mCursor = bd.rawQuery(query, null);
if (mCursor.getCount() > 0) {
if (mCursor.moveToFirst()) {
do {
ClienteModel mClienteModel = new ClienteModel();
mClienteModel.setClientes_id(mCursor.getInt(mCursor.getColumnIndex(ClienteModel.Coluna.CLIENTES_ID)));
mClienteModel.setId_rm(mCursor.getInt(mCursor.getColumnIndex(ClienteModel.Coluna.ID_RM)));
mClienteModel.setCredencial_id(mCursor.getInt(mCursor.getColumnIndex(ClienteModel.Coluna.CREDENCIAL_ID)));
mClienteModel.setNome(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna.NOME)));
mClienteModel.setTipo(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna.TIPO)));
mClienteModel.setInformacao_adicional(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna.INFORMACAO_ADICIONAL)));
mClienteModel.set_criado(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna._CRIADO)));
mClienteModel.set_modificado(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna._MODIFICADO)));
mClienteModel.set_status(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna._STATUS)));
listaDeRegistros.add(mClienteModel);
} while (mCursor.moveToNext());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return listaDeRegistros;
}
LoadMoreTask:
protected Boolean doInBackground(Integer... parameters) {
int npagina = parameters[0];
cliente= new ArrayList<ClienteModel>();
try {
Repositorio mRepositorio = new Repositorio(context);
List listaDeClientes = mRepositorio.getClientes(npagina,5,"");
cliente = listaDeClientes;
System.out.println("pagina " + npagina);
}catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
答案 0 :(得分:0)
解决:
public boolean onQueryTextChange(String change) {
//TODO Auto-generated method stub
if (searchCheck) {
showResults();
try {
clientes = new ArrayList<ClienteModel>();
Repositorio mRepositorio = new Repositorio(getActivity());
List Clientes = mRepositorio.getClientes(change, 15, 0);
clientes = Clientes;
ads = new ClientSearchViewAdapter(getActivity(), this, clientes);
lv.addFooterView(footerLinearLayout);
lv.setOnScrollListener(new EndlessScrollListener() {
@Override
public void onLoadMore(int page, int totalItemsCount) {
new LoadMoreClientTask(progressBar, FragmentClientes.this, ad, getActivity()).execute(page);
System.out.println("PAGE " + page);
}
});
lv.setAdapter(ads);
new LoadMoreClientTask(progressBar, this, ad, getActivity()).execute(1);
System.out.println("Pesquisa: " + clientes);
}catch (Exception e){
e.printStackTrace();
}
}
return false;
}