vendredi 24 juin 2016

How to filter a list inside the RecyclerView adapter based on the object field

So I have a list of tracks inside a RecyclerView. Everything is ok regarding displaying the tracks in the RecyclerView. However I am struggling filtering the RecyclerView based on the user preferences that he has chosen. Every Track has an attribute either short, long, or medium. I want to give the user the ability to choose from the preferences if he wants to display all periods or a specific one. My problem now is filtering the result.

public class TrackAdapter extends RecyclerView.Adapter<TrackAdapter.ViewHolder>  {

private LayoutInflater layoutInflater;
List<Track> tracks;
private Context context;
private String length;



private ClickListener clickListener;
public TrackAdapter(Context context, List<Track> tracks){
    this.context=context;
    this.tracks=tracks;
    layoutInflater=layoutInflater.from(context); 
 public  void setlength (String level) {
   this.length = length ;

}}

and the preferences in the xml:

<ListPreference android:title="@string/level"
    android:defaultValue="@string/length_default"
    android:entries="@array/length_enteries"
    android:entryValues="@array/length_entery_values"
    android:key="@string/length_key"
    />

in the Mainactivity: I get the value of the chosen length filter as following:

public void setlevel(){
SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
level=sharedPreferences.getString(getString(R.string.length_key),getString(R.string.length_default));
trackAdapter.setLevel(level);}

Aucun commentaire:

Enregistrer un commentaire