Ishan
Ishan

Reputation: 303

List view not refreshed after filtering and array adapter in Android - Please help

I created one list view, and arrayadapter now when i filter the arrayadpter its filtering the data perfect. But the listview is not refreshed :( it shows the previous data only.

My coding is as follows:

  1. TextView used inside ListView: A Simple text view for displaying each row of list view

  2. ListView : here there is a textview for search bar and a list view where I am using above textview.

  3. onCreate method of Activity Here seachCountry is the textbox in above xml to search (filter) the countries.

    countryList = (ListView) findViewById(R.id.countryList);
    searchCountry = (EditText) findViewById(R.id.search);
    createProfile = (Button) findViewById(R.id.createProfile);      
    countries = getResources().getStringArray(R.array.countries_array);     
    //countryArray = new ArrayAdapter<String>(this,R.layout.country_list_row , countries);
    countryArray = new CountryArrayAdapter<String>(this,R.layout.country_list_row , countries);
    countryList.setAdapter(countryArray);       
    countryList.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
            countryFrom = ((TextView) view).getText().toString();               
        }
    });
    
    searchCountry.addTextChangedListener(new TextWatcher() {            
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {               
        }           
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {                
        }
    
        @Override
        public void afterTextChanged(Editable s) {
            SelectCountryActivity.this.countryArray.getFilter().filter(s);
            countryArray.notifyDataSetChanged();
        }
    });
    
    createProfile.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            Intent i = new Intent(SelectCountryActivity.this,CreateProfileActivity.class);
            i.putExtra("countryFrom", countryFrom);
            startActivity(i);
            finish();
        }
    });
    
  4. ArrayList Adapter

    public class CountryArrayAdapter extends ArrayAdapter{ public String[] allItems; public String[] subItems; private CountryFilter filter;

    public CountryArrayAdapter(Context context, int textViewResourceId, String[] countryList) {
        super(context, textViewResourceId, countryList);
        // TODO Auto-generated constructor stub
        this.subItems = countryList;
        this.allItems = this.subItems;
    }
    
    @Override
    public Filter getFilter() {
        if (filter == null){
          filter  = new CountryFilter();
        }
        return filter;
      }
    
     @Override
     public int getCount() {
         return subItems.length;
     }
    
     @Override
     public String getItem(int position) {
         return subItems[position];
     }
    
     private class CountryFilter extends Filter{
    
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
             FilterResults results = new FilterResults();
             ArrayList<String> i = new ArrayList<String>();
             String[] contents;
    
              if (constraint!= null && constraint.toString().length() > 0) {
    
                  for (int index = 0; index < allItems.length; index++) {
                      String si = allItems[index];
                      if(si.startsWith(constraint.toString())){
                        i.add(si);  
                      }
                  }
                  contents = new String[i.size()];
                  i.toArray(contents);
                  results.values = contents;
                  results.count = contents.length;                   
              }
              else{
                  synchronized (allItems){
                      results.values = allItems;
                      results.count = allItems.length;
                  }
              }
              return results;
        }
    
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            // TODO Auto-generated method stub
            subItems =  (String[])results.values;
            notifyDataSetChanged();
        }
    }
    

I come to know from some links that inflater will solve my problem but not able to figure it out that how to use inflater.

Please help me to solve the problem of refreshing the view part.

Thanks a lot in advance.

with best regards, Ishan

Upvotes: 1

Views: 2596

Answers (1)

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28523

set adapter object in the list explicitly after filter is done.

Upvotes: 2

Related Questions