Android Searchview OnQueryTextListener,数据库无法正常工作

时间:2014-09-17 14:42:04

标签: android search android-fragments searchview

我试图在我的片段类中实现Searchview,我按照THIS教程,但它没有搜索该项目,我在这里遗漏了一些东西,但我可以&#39找到它是什么,这是我到目前为止所尝试的:

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;
    }

1 个答案:

答案 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;
                }