如何在具有搜索功能的页面中加载listview

时间:2015-03-14 19:41:03

标签: android listview search

我试图实现一个第一次只显示10个项目的列表视图。当用户点击按钮时,显示更多"然后应该加载20个项目。我做了这个,但后来我只能搜索当前加载的列表。该列表有100个项目。因此,当我尝试搜索第一次运行时,我只能搜索这10个项目。

private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{

        @Override
        protected SimpleAdapter doInBackground(String... strJson) {

            connector=new OpenerpRpc(getBaseContext());
            connector.Config();
            current_page += 1;
            try{
                Object login = connector.Login();
                Object tests2 = connector.searchWorkers("res.partner",new Object[0]);  
                Object[] ret_workers=(Object[])tests2;
                Object[] params_var = {"name","parent_id","partner_longitude","partner_latitude","street","zip","city","country_id"};
                Object test =  connector.Read("res.partner",ret_workers,params_var);
                Object[] ret=(Object[])test;
                int length = current_page*10;
                listOfFieldValues = new ArrayList<HashMap<String, Object>>(length); //ret.length
                int totalCount = 0;
                for (Object objectFields : ret) {
                    listOfFieldValues.add((HashMap<String, Object>)objectFields);
                }
            }catch(Exception e){
                Log.d("Exception", e.toString());
            }
            //String[] from = { "name","street"};
            String[] from = { "name"};
            // Ids of views in listview_layout
            int[] to = { R.id.tv_address};
            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), listOfFieldValues, R.layout.lv_gps_layout, from, to);
            return adapter;
        }
        /** Invoked by the Android on "doInBackground" is executed */
        @Override
        protected void onPostExecute(final SimpleAdapter adapter) {
            // Setting adapter for the listview
            mListView.setAdapter(adapter);

            //for(int i=0;i<adapter.getCount();i++){
            for(int i=0;i<adapter.getCount();i++){
                HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
                name = (String) hm.get("name");
                HashMap<String, Object> hmDownload = new HashMap<String, Object>();
                hm.put("name",name);
                adapter.notifyDataSetChanged();

                mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                        Intent myIntent = new Intent(MainActivity.this, YardActivity.class);
                        myIntent.putExtra("key", position); //Optional parameters
                        MainActivity.this.startActivity(myIntent);
                    }
                });

            }

            inputSearch = (EditText) findViewById(R.id.inputSearch);
            inputSearch.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

                    adapter.getFilter().filter(cs);

                }
                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                              int arg3) {
                    // TODO Auto-generated method stub

                }
                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                }
            });

            progress.dismiss();
            Log.d("/****","Data from odoo is finished");
        }
    }

任何知道如何使用搜索功能实现此功能的好教程的人?

1 个答案:

答案 0 :(得分:0)

我认为,更好的搜索方式是使用php。

发送关键字(从应用程序到主机),然后将结果回复为json。