I am having a list view and an edit text. When i write on Edit text i want to display my list view Starting with those alphabets. I am adding a custom adapter to My list view.
here is my Edit text addTextChangedListener
getFriendsName.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
friends_available.setVisibility(View.VISIBLE);
adapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
and here is my List view over which i am setting a custom adapter
adapter = new GroupAvailableFriendsToAddAdapter((BaseActivity) getActivity(),
connectionsList);
friends_available.setAdapter(adapter);
here is my Adapter class
public class GroupAvailableFriendsToAddAdapter extends ArrayAdapter<ItemsAllPost> {
private BaseActivity _context;
private ArrayList<ItemsAllPost> data;
private LayoutInflater inflater = null;
private ItemsAllPost postData;
private String result;
@SuppressWarnings("unused")
private int count;
private TextView textView;
private ImageLoader imageLoader;
public GroupAvailableFriendsToAddAdapter(BaseActivity activity,
ArrayList<ItemsAllPost> connectionsList) {
super(activity, R.layout.group_avialable_friends_list_adapter, connectionsList);
_context = activity;
data = connectionsList;
count=connectionsList.size();
inflater = (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ItemsAllPost items = data.get(position);
if (convertView == null) {
view = inflater.inflate(R.layout.group_avialable_friends_list_adapter,
null);
}
TextView FriendsName = (TextView) view
.findViewById(R.id.group_available_friendNameTV);
CircularImageView image_profile = (CircularImageView) view
.findViewById(R.id.group_available_friendImgCI);
FriendsName.setText(items.user_first_name);
imageLoader.DisplayImage(items.profile_image,
image_profile);
return view;
}
}
problem is initially list view is showing the List but when i enter a text it simply hides even the entered text matches the List view Items and when i erase the text from Edit text list view shows back.
what i am doing wrong?
any help would be greatly appreciated.
Ali's method would require you to set the adapter each time with a new search text. Rather you could just implement a Filterable class and notify the adapter regarding a change in data after every search. Change you adapter to the below. It should work fine!
public class GroupAvailableFriendsToAddAdapter extends ArrayAdapter<ItemsAllPost> implements Filterable{
//create a new temporary list
private ArrayList<ItemsAllPost> data, tempListData;
public GroupAvailableFriendsToAddAdapter(BaseActivity activity, ArrayList<ItemsAllPost> connectionsList) {
super(activity, R.layout.group_avialable_friends_list_adapter, connectionsList);
data = connectionsList;
**tempListData** = connectionsList; //make this list point to your original list
}
@Override
public int getCount() {
return **tempListData** .size(); // make the count point to this tempList as the list size would vary with every search text
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemsAllPost items = **tempListData** .get(position); // reference data using this tempList
FriendsName.setText(items.user_first_name);
return view;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
final List<ItemsAllPost> tempFliteredDataList = new ArrayList<ItemsAllPost();
// We implement here the filter logic
if (constraint == null || constraint.toString().trim().length() == 0) {
// No filter implemented we return all the list
results.values = data;
} else {
// We perform filtering operation
String constrainString = constraint.toString().toLowerCase();
for (ItemsAllPost post: data) {
if (post.user_first_name.toLowerCase().contains(constrainString)) {
tempFliteredDataList.add(post);
}
}
results.values = tempFliteredDataList ;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.values!=null){
**tempListData** = (ArrayList<ItemsAllPost>) results.values; // returns the filtered list based on the search
notifyDataSetChanged();
}
}
};
return filter;
}
}