- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试在我的 Fragment 类中实现 Searchview,我遵循了 THIS教程,但它不是在搜索该项目,我在这里遗漏了一些东西,但我找不到它是什么,这是我到目前为止尝试过的:
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 ));
progressBar = new ProgressBar(getActivity(), null, android.R.attr.progressBarStyle);
LinearLayout.LayoutParams progressBarParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
progressBar.setLayoutParams(progressBarParams);
progressBar.setPadding(6, 6, 6, 6);
footerLinearLayout = new LinearLayout(getActivity());
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
footerLinearLayout.setGravity(Gravity.CENTER);
footerLinearLayout.setLayoutParams(layoutParams);
footerLinearLayout.addView(progressBar);
try {
lv = (ListView) rootView.findViewById(R.id.listaClientes);
clientes = new ArrayList<ClienteModel>();
ad = new ClientViewAdapter(getActivity(), this, clientes);
lv.setVerticalFadingEdgeEnabled(true);
lv.setVerticalScrollBarEnabled(true);
lv.setOnScrollListener(new EndlessScrollListener(){
@Override
public void onLoadMore(int page, int totalItemsCount) {
new LoadMoreClientTask(progressBar,FragmentClientes.this,ad,getActivity()).execute(page);
}
});
lv.addFooterView(footerLinearLayout);
lv.setAdapter(ad);
new LoadMoreClientTask(progressBar,this,ad,getActivity()).execute(1);
} catch (Exception e) {
e.printStackTrace();
}
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;
}
最佳答案
已解决:
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;
}
关于数据库无法正常工作的 Android Searchview OnQueryTextListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25893652/
我正在尝试使用 SearchView 搜索项目,但我不知道为什么我找不到可能的方法,你能帮助我吗? FragGridCampos.java public class FragGridCampos ex
我正在尝试在我的 Fragment 类中实现 Searchview,我遵循了 THIS教程,但它不是在搜索该项目,我在这里遗漏了一些东西,但我找不到它是什么,这是我到目前为止尝试过的: public
我正在使用第三方库创建一个可搜索的微调器。我在我的应用程序中添加了库类(SearchableListDialog、SearchableSpinner)。一切正常,但我仍然面临一个问题,例如,在搜索 V
Searchview 的结果正常,但是当我单击 Recycleview 时,它给出了错误的位置。例如:当我搜索 Padang 时,它显示“Padang”的结果,但是当我单击 Recyclerview
我想在android Scala eclipse插件中实现android的搜索方法: 我有这个方法: val queryListener = new OnQueryTextListener() { o
我使用的是 Android 4.1.2。我在 ActionBar 上有一个 SearchView 小部件。来自 android 开发者网站的关于 SearchView.OnQueryTextListe
我有一个自定义的 AsyncTaskLoader,它根据输入到 ActionBar 搜索中的一些查询文本执行搜索: class SearchLoader extends AsyncTaskLoader
我已经在我的应用程序中实现了 Searchview,但遇到了一个奇怪的问题。基本上,用户能够输入要搜索的字符串并正确提交。但是,我的应用程序将根据搜索查询过滤 ListView,然后启动 Intent
我的 Activity 中有一个搜索 View 。当我尝试附加 OnQueryTextListener 时,它给了我一个 NullPointerException。其Java代码如下 @Ove
我提出的问题是关于事件 onQueryTextListener,我声明它之前在 java 中使用它,但是当我提交文本或更改时它不显示日志正文。 这是我的 fragment 的代码: Fragment_
我正在尝试将 SearchView Support v4 版本与操作栏 sherlock 一起使用。 所以我在操作栏中有我的搜索按钮 -> 当我触摸它时,键盘和搜索栏也会出现。 我的问题是我需要使用
在我的应用程序中使用 SearchView。 无论如何我可以延迟对 onQueryTextChange() 方法的调用。例如,当用户键入一系列字符时,他必须等待此方法被调用。 此等待应该不取决于键入的
SearchView.OnQueryTextListener 方法 onQueryTextChange 在我调用 invalidateOptionsMenu() 后调用 onPrepareOption
我正在 CustomAdapter 上执行 SearchView,它将在其中搜索门名称。在我尝试运行该应用程序后,它突然崩溃并给我如上所述的错误。我不知道我什至做错了哪一部分导致我每次尝试运行我的应用
我是一名优秀的程序员,十分优秀!