If you want to filter listview item with name then follow below steps
write this code in oncreate method of your main activity
edtsearch.addTextChangedListener(searchTextWatcher);
(here edtsearch is your edittext,searchTextWatcher is method define outside oncreate method)
create method outside oncreate method of your main activity
private TextWatcher searchTextWatcher = new TextWatcher() {
@SuppressWarnings("static-access")
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(phonebookadp != null){
phonebookadp.getFilter().filter(s.toString());
}
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
public void afterTextChanged(Editable s) {}
};
here phonebookadp is your adapter.
here declaired adapter in your main activity
public class Phonebook_Adapter extends BaseAdapter implements Filterable{
private LayoutInflater l_Inflater;
public Phonebook_Adapter(Context context) {
l_Inflater = LayoutInflater.from(context);
// this is new array list if we click on backpress so fill listview back with this arraylist
// fill this array list where mylstcont list filling
mycheckcontlst = new ArrayList<String>();
}
public int getCount() {
// TODO Auto-generated method stub
// this is your list view items
return mylstcont.size();
}
public Object getItem(int position) {
return mylstcont.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Do yout stuff
return convertView;
}
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
return new Filter(){
// ContactSetGet is your gettersetter class
@Override
protected FilterResults performFiltering(CharSequence prefix) {
// TODO Auto-generated method stub
FilterResults results = new FilterResults();
List<ContactSetGet> i = new ArrayList<ContactSetGet>();
if (prefix!= null && prefix.toString().length() > 0) {
for (int index = 0; index < mylstcont.size(); index++) {
ContactSetGet si = mylstcont.get(index);
Log.i("----------------si.getFirstName()---------","."+si.getName());
Log.i("----------------prefix---------","."+prefix.toString());
//String number
if(si.getName().startsWith(prefix.toString())){
i.add(si);
}
}
results.values = i;
results.count = i.size();
}
else{
synchronized (mylstcont){
results.values = mycontnamesetget;
results.count = mycontnamesetget.size();
}
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// TODO Auto-generated method stub
mylstcont = (ArrayList<ContactSetGet>) results.values;
Phonebook_Adapter.this.notifyDataSetChanged();
}
};
}
}
write this code in oncreate method of your main activity
edtsearch.addTextChangedListener(searchTextWatcher);
(here edtsearch is your edittext,searchTextWatcher is method define outside oncreate method)
create method outside oncreate method of your main activity
private TextWatcher searchTextWatcher = new TextWatcher() {
@SuppressWarnings("static-access")
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(phonebookadp != null){
phonebookadp.getFilter().filter(s.toString());
}
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
public void afterTextChanged(Editable s) {}
};
here phonebookadp is your adapter.
here declaired adapter in your main activity
public class Phonebook_Adapter extends BaseAdapter implements Filterable{
private LayoutInflater l_Inflater;
public Phonebook_Adapter(Context context) {
l_Inflater = LayoutInflater.from(context);
// this is new array list if we click on backpress so fill listview back with this arraylist
// fill this array list where mylstcont list filling
mycheckcontlst = new ArrayList<String>();
}
public int getCount() {
// TODO Auto-generated method stub
// this is your list view items
return mylstcont.size();
}
public Object getItem(int position) {
return mylstcont.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Do yout stuff
return convertView;
}
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
return new Filter(){
// ContactSetGet is your gettersetter class
@Override
protected FilterResults performFiltering(CharSequence prefix) {
// TODO Auto-generated method stub
FilterResults results = new FilterResults();
List<ContactSetGet> i = new ArrayList<ContactSetGet>();
if (prefix!= null && prefix.toString().length() > 0) {
for (int index = 0; index < mylstcont.size(); index++) {
ContactSetGet si = mylstcont.get(index);
Log.i("----------------si.getFirstName()---------","."+si.getName());
Log.i("----------------prefix---------","."+prefix.toString());
//String number
if(si.getName().startsWith(prefix.toString())){
i.add(si);
}
}
results.values = i;
results.count = i.size();
}
else{
synchronized (mylstcont){
results.values = mycontnamesetget;
results.count = mycontnamesetget.size();
}
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// TODO Auto-generated method stub
mylstcont = (ArrayList<ContactSetGet>) results.values;
Phonebook_Adapter.this.notifyDataSetChanged();
}
};
}
}
No comments:
Post a Comment
Comments