Last active
December 1, 2016 09:57
-
-
Save IevgenOsetrov/ba0ad57a45c88b9ead8576cf4daa05b6 to your computer and use it in GitHub Desktop.
Add search for listview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
searchEditText.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
filmsAdapter.getFilter().filter(s); | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.Filter; | |
import android.widget.TextView; | |
import com.example.lenovo.webfliks.R; | |
import com.example.lenovo.webfliks.parser.FilmsItem; | |
import java.util.ArrayList; | |
public class FilmsAdapter extends ArrayAdapter<FilmsItem> { | |
private Context context; | |
private ArrayList<FilmsItem> filmsItems; | |
public FilmsAdapter(Context context, int resource, ArrayList<FilmsItem> objects) { | |
super(context, resource, objects); | |
this.context = context; | |
filmsItems = new ArrayList<>(objects.size()); | |
filmsItems.addAll(objects); | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
FilmsItem filmsItem = getItem(position); | |
if (convertView == null) { | |
convertView = LayoutInflater.from(context).inflate(R.layout.video_item_layout, parent, false); | |
} | |
TextView filmNameTextView = (TextView) convertView.findViewById(R.id.film_name); | |
filmNameTextView.setText(filmsItem.getFilmName()); | |
return convertView; | |
} | |
@Override | |
public Filter getFilter() { | |
return nameFilter; | |
} | |
Filter nameFilter = new Filter() { | |
@Override | |
public CharSequence convertResultToString(Object resultValue) { | |
return ((FilmsItem) resultValue).getFilmName(); | |
} | |
@Override | |
protected FilterResults performFiltering(CharSequence constraint) { | |
FilterResults results = new FilterResults(); | |
if (constraint != null) { | |
ArrayList<FilmsItem> suggestions = new ArrayList<>(); | |
for (FilmsItem filmsItem : filmsItems) { | |
if (filmsItem.getFilmName().toLowerCase().contains(constraint.toString().toLowerCase())) { | |
suggestions.add(filmsItem); | |
} | |
} | |
results.values = suggestions; | |
results.count = suggestions.size(); | |
} | |
return results; | |
} | |
@Override | |
protected void publishResults(CharSequence constraint, FilterResults results) { | |
clear(); | |
if (results != null && results.count > 0) { | |
addAll((ArrayList<FilmsItem>) results.values); | |
} else { | |
addAll(filmsItems); | |
} | |
notifyDataSetChanged(); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment