I have an edittext and a listview in my application my listview show contact list. I want listview filter with edittext. I searched a lot on google and found some examles but none worked for me here's my code
my custom adapter:
public class CustomAdapter extends BaseAdapter
{
Fragment frgmnt;
ArrayList<HashMap<String, ArrayList>> contactarr;
LayoutInflater inflater;
public CustomAdapter(Fragment1 frgmnt, ArrayList<HashMap<String, ArrayList>> contactarr)
{
//
originalData=contactarr;
filteredData=contactarr;
this.frgmnt=frgmnt;
this.contactarr=contactarr;
inflater= (LayoutInflater)getActivity().getSystemServic(Context.LAYOUT_INFLATER_SERVICE);
this.contactarrsrch=new ArrayList<HashMap<String, ArrayList>>();
this.contactarrsrch.addAll(contactarr);
}
@Override
public int getCount()
{
return filteredData.size();
}
@Override
public Object getItem(int position)
{
return filteredData.get(position);
//return null;
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent)
{
if(view==null)
{
view=inflater.inflate(R.layout.list_data,null);
}
LinearLayout ll_row =(LinearLayout) view.findViewById(R.id.ll_row);
TextView txtContact = (TextView) view.findViewById(R.id.contact_name);
HashMap<String, ArrayList> map=contactarr.get(position);
ArrayList name =map.get("Name_");
txtContact.setText(name.get(0).toString());
return view;
}
}
this is in onCrate():
InputSearch.addTextChangedListener(new TextWatcher()
{
final ArrayList<HashMap<String,ArrayList>> tempArrayList =new ArrayList<HashMap<String,ArrayList>>(contactarr1);
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
String text = InputSearch.getText().toString().toLowerCase(Locale.getDefault());
//adapter.getFilter(s.toString());
int textlength = s.length();
for (HashMap<String, ArrayList> wp : tempArrayList)
{
if(textlength<=wp.get("Name_").toString().length())
{
if (( wp.get("Name_").toString()).toLowerCase().contains(s.toString().toLowerCase()))
{
tempArrayList.add(wp);
}
}
}
adapter=new CustomAdapter(Fragment1.this, tempArrayList);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{}
@Override
public void afterTextChanged(Editable s)
{}
});
i dont know why filter is not working when i typed somthing in edit text