Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello i have made a dynamic listview..Now i have added filter in listview.m using Adapter in it.

Now i have problem:
My listview is getting filtered perfectly but while backpressing button to clear edittext listview is not getting updated..m updating my code.

here is my code:
From here m calling Getfilter method of my adater class:


Java
list = (ListView) findViewById(R.id.list_Upadates);
// Pass the results into ListViewAdapter.java
adapter = new UpdatesAdapterList(Cardiology_updates.this,
        FinalLocalDataList);
// Set the adapter to the ListView
list.setAdapter(adapter);
// adapter.notifyDataSetChanged();

et = (EditText) findViewById(R.id.search);

// Capture Text in EditText
et.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) {
        //String text = et.getText().toString().toLowerCase(Locale.getDefault());
        adapter.getFilter().filter(arg0);
    }
});

 }
 }



Here is my adapter class includes filter:

This is my filter:

Java
@Override
	public Filter getFilter() {

	    if(valueFilter==null) {

	        valueFilter=new ValueFilter();
	    }

	    return valueFilter;
	}
	
	private class ValueFilter extends Filter {
		//Filter filter = new Filter() {
			
	           
	           
	           ArrayList> filteredList;

	           

	           @Override
	           protected FilterResults performFiltering(CharSequence constraint) {
	               // Holds the results of a filtering operation in values
	        	   FilterResults results=new FilterResults();
	        	   
		           if(constraint!=null && constraint.length()>0){
		        	   
	               filteredList= new ArrayList>();

	               //This is the search string user typed in EditText. 
	              // String searchString = constraint.toString().startsWith(prefix).toUpperCase(Locale.getDefault());

	               /*
	                * Code to update the filteredList based on Search String.
	                * 
	                * I'm just writing pseudo code for search, based on
	                * Cardiology_updates.UpdateTitle
	                */ 
	                int mCount=data.size();
	                
	                for (int i = 0; i < mCount; i++) {
	                    HashMap obj= data.get(i);
	                    String updateTitle=obj.get(Cardiology_updates.UpdateTitle);
	                    if(updateTitle.toUpperCase().startsWith(constraint.toString().toUpperCase())){
	                         filteredList.add(obj);
	                         
	                    } 
	                } 
	                results.count = filteredList.size();
                    results.values = filteredList;
		           }else{
	                    // set the Filtered result to return
	                    results.count = filteredList.size();
	                    results.values = filteredList;
	                }
	            
	            return results;
	            
	        }
	           @Override
	           protected void publishResults(CharSequence constraint,
	                FilterResults results) {
	               // Has the filtered values
	               data = (ArrayList>) results.values;  
	               notifyDataSetChanged();          
	           }
	     };
	     
}
Posted
Updated 11-Nov-14 20:07pm
v6
Comments
Dominic Burford 10-Nov-14 9:01am    
Can you be more specific. You have posted a whole load of code and expect us to make sense of it all. Can you refine your question so it's easier to understand what you are asking.
vn12 10-Nov-14 9:20am    
my listview is not getting updated when m clearing edittext...m updating new code with
Richard MacCutchan 10-Nov-14 10:01am    
Please format your code to make it readable, as I have done for you.
vn12 11-Nov-14 6:33am    
format means what should i do??
Richard MacCutchan 11-Nov-14 7:08am    
Use the buttons at the top of the editor window when you edit your question. If you look at it now (use the Improve question link) you will see the <pre> tags that were added by me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900