android - cannot update listview on changing an EditText value

前端 未结 1 923
伪装坚强ぢ
伪装坚强ぢ 2020-12-22 07:47

I have an activity that retrieves data from a database that contains information (name, capital, population, flag image) for many countries. The main layout has an EditText

相关标签:
1条回答
  • 2020-12-22 08:09

    just open teh same page from your current page for the below code

    enter cod  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capital);
    
        private ArrayList<Country> items;
        private ArrayList<Country> items_all;
    
        private ArrayList<Country>  items_new;
    
        private EfficientAdapter adapter;
        private ListView listView;
    
    
        EditText searchInput=(EditText)findViewById(R.id.search_input_capital);
    
        items = new ArrayList<Country>();
        items_new = new ArrayList<Country>();
        listView = (ListView) findViewById(R.id.listView);
        listView.setDividerHeight(10);
    
        adapter = new EfficientAdapter(CapitalActivity.this);
        listView.setAdapter(adapter);
    
       setListItems("");
    
      searchInput.addTextChangedListener(new TextWatcher() {
    
               @Override
               public void afterTextChanged(Editable s) {
    
               }
    
               @Override    
               public void beforeTextChanged(CharSequence s, int start,
                 int count, int after) {
               }
    
               @Override    
               public void onTextChanged(CharSequence s, int start,
                 int before, int count) {
    
    
                   String searchCapital = new String(s.toString()).trim();
    
                 items_new.clear();
    
               for(Country myCountry  : items_all) {
    
               if( myCountry.getCapital() != null &&  
                     myCountry.getCapital().trim().toLowerCase().contains(searchCapital)) {
    
               items_new.add(myCountry);
                 }
                }
    
    
    
             items.clear();
    
            items = (ArrayList<Country>) items_new.clone();
    
            if(searchCapital.trim().isEmpty()){
    
            items = items_all;
            }
    
            adapter.notifyDataSetChanged();
    
    
               }
              });
    

    private void setListItems(String searchKey) {

    items_all= DatabaseAccessor.getCountryList(CapitalActivity.this, type, typeValue, searchKey);
    items=items_all;
    
    adapter.notifyDataSetChanged();
    

    }e here

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