Search functionality Within List View in Android

前端 未结 2 737
闹比i
闹比i 2021-01-28 03:39

I\'m pretty new to android. I\'m trying to develop a search function on a list of items by using arrayadapter as shown below. but search is not working correctly. plz plz help

相关标签:
2条回答
  • 2021-01-28 04:25

    Try to do like this. hope it will help you...

    editsearch.addTextChangedListener(new TextWatcher() {
    
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub
            editsearch.requestFocus();
            final ArrayList<Item> itemArray = new ArrayList<Item>();
            int textlength = 0;
            textlength = editsearch.getText().length();
            itemArray.clear();
            for (int i = 0; i < item.size(); i++)
            {
                String itemTitle = item.get(i).name();
                String compareItemTitle = "";
                if (textlength <= item.length())
                {
                   compareItemTitle = (String) itemTitle.toLowerCase().subSequence(0, textlength);
                   if(compareItemTitle.contentEquals(editsearch.getText().toString().toLowerCase()))
                   {
                        itemArray.add(item.get(i));
                   }
                }
            }
            adapter = new MyAdapter(AddToOutlet.this, R.layout.addtooutlet_list_item, itemArray);
            lv.setAdapter(adapter);
    }
    
        }
    });
    
    0 讨论(0)
  • 2021-01-28 04:28

    Try this way, this is exactly what you looking for

       @Override
        public boolean onCreateOptionsMenu(Menu menu) {
    
            // adds item to action bar
            getMenuInflater().inflate(R.menu.search_main, menu);
    
            // Get Search item from action bar and Get Search service
            MenuItem searchItem = menu.findItem(R.id.action_search);
            SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
            if (searchItem != null) {
                searchView = (SearchView) searchItem.getActionView();
            }
            if (searchView != null) {
                searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
                searchView.setIconified(false);
            }
    
            return true;
        }
    

    http://androidcss.com/android/android-search-view-php-mysql/

    http://androidcss.com/android/fetch-json-data-android/

    0 讨论(0)
提交回复
热议问题