我在这里遇到一个奇怪的情况,当我搜索一个项目时,它会返回我正在搜索的内容,但它正在做其他事情,它会在列表视图中添加2个空行如果我搜索例如:" fire"它会在我的listview末尾添加8个空行,为什么会这样?
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class FragmentClientes extends Fragment implements OnQueryTextListener{
private boolean searchCheck;
private List<ClienteModel> clientes = new ArrayList<ClienteModel>();
private ListView lv;
private View rootView;
ProgressBar progressBar;
private LinearLayout footerLinearLayout;
public FragmentActivity activity;
private SearchView searchView;
private String currentQuery = null;
private ClientViewAdapter ad;
private ClientSearchViewAdapter ads;
@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;
}
public 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, 1);
return false;
}
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, 1);
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(1);
System.out.println("PAGE " + page);
}
});
new LoadMoreClientTask(progressBar, this, ad, getActivity()).execute(1);
lv.setAdapter(ads);
System.out.println("Pesquisa: " + clientes);
}catch (Exception e){
e.printStackTrace();
}
}
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;
}
@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() {
try {
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);
lv = (ListView) rootView.findViewById(R.id.listaClientes);
clientes = new ArrayList<ClienteModel>();
ad = new ClientViewAdapter(getActivity(), this, clientes);
lv.setVerticalFadingEdgeEnabled(true);
lv.setVerticalScrollBarEnabled(true);
lv.setAdapter(ad);
} catch (Exception e) {
e.printStackTrace();
}
}
LoadMoreTask:
public class LoadMoreClientTask extends AsyncTask<Integer, Void, Boolean> {
private SearchView.OnQueryTextListener activity;
private ClientViewAdapter adapter;
private List<ClienteModel> cliente = new ArrayList<ClienteModel>();
private ProgressBar progressBar;
private Context context;
public LoadMoreClientTask(ProgressBar progressBar, SearchView.OnQueryTextListener activity, ClientViewAdapter adapter, Context context){
this.progressBar = progressBar;
this.activity = activity;
this.adapter = adapter;
this.context = context;
}
@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Boolean doInBackground(Integer... parameters) {
int npagina = parameters[0];
cliente= new ArrayList<ClienteModel>();
try {
Repositorio mRepositorio = new Repositorio(context);
List listaDeClientes = mRepositorio.getClientes("", 5, npagina);
cliente = listaDeClientes;
System.out.println("pagina " + npagina);
}catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
if(result){
adapter.setData(cliente);
}
progressBar.setVisibility(View.INVISIBLE);
}
}
getClientes:
public List<ClienteModel> getClientes( String consulta, Integer limit, Integer pagina) throws SQLException {
int offset = pagina * limit - limit;
System.out.println("OFFSET: "+ offset);
List<ClienteModel> listaDeRegistros = new ArrayList<ClienteModel>();
System.out.println("Consulta: "+ consulta);
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;
}
答案 0 :(得分:1)
这样做是因为你不断为查询更改添加footerView。
lv.addFooterView(footerLinearLayout);
您应该添加footerView一次并在Activity中保留对它的引用,然后根据需要更新视图。