0

I have a recycleview showing a list of audio files fetched from my audios.json file hosted on my server. i have a model class with a getter method getLanguage() to see the audio language. I would like to show only audio files of users preference in recycle view. Say for example, if user wants only english and russian i would like to show only list of russian and english. How can we achieve this? Right now the entire list is displayed.

public class AudioAdapter extends  RecyclerView.Adapter<AudioAdapter.HomeDataHolder> {

int currentPlayingPosition = -1;

Context context;
ItemClickListener itemClickListener;

List<Output> wikiList;

public AudioAdapter(List<Output> wikiList, Context context) {
    this.wikiList = wikiList;
    this.context = context;
}



@NonNull
@Override
public HomeDataHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(context).inflate(R.layout.audio_row_layout,viewGroup,false);
    HomeDataHolder mh = new HomeDataHolder(view);
    return mh;
}

@Override
public void onBindViewHolder(@NonNull final HomeDataHolder homeDataHolder, int i) {
    String desc = wikiList.get(i).getLanguage() + " • " + wikiList.get(i).getType();
    homeDataHolder.tvTitle.setText(wikiList.get(i).getTitle());
    homeDataHolder.tvotherinfo.setText(desc);
    homeDataHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (itemClickListener != null)
                itemClickListener.onClick(view,homeDataHolder.getAdapterPosition());
        }
    });


    homeDataHolder.rippleLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (itemClickListener != null)
                itemClickListener.onClick(view,homeDataHolder.getAdapterPosition());
        }
    });



}

@Override
public int getItemCount() {
    return wikiList.size();
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    return position;
}

public void setClickListener(ItemClickListener itemClickListener) {    //  Method for setting clicklistner interface
    this.itemClickListener = itemClickListener;

}

public class HomeDataHolder extends RecyclerView.ViewHolder {

    TextView tvTitle,tvotherinfo;
    MaterialRippleLayout rippleLayout;

    public HomeDataHolder(View v) {
        super(v);
        this.tvTitle = v.findViewById(R.id.title);
        this.tvotherinfo = v.findViewById(R.id.audioDesc);
        this.rippleLayout = v.findViewById(R.id.ripple);
    }

  }

}
Manohar
  • 22,116
  • 9
  • 108
  • 144

2 Answers2

1

The general idea for this should be:

  1. you have one list with all items
  2. you have filter rules selected by the user
  3. You filter items from number 1, to see which ones match the constraints and store this in another list.

Then the recycler view only shows the items of the list from number 3.

This means that recycler view's getItemCount would return the size of the filtered list, not the whole list.

Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
  • List response Application.getInstance().setSingletonResponse(response); audioAdapter = new AudioAdapter(response, getActivity()); recyclerViewAudios.setAdapter(audioAdapter); audioAdapter.setClickListener(this); – Clutchplasdfsdfte Apr 29 '20 at 17:31
  • 1
    How can i filter this list according to users preferred language ? – Clutchplasdfsdfte Apr 29 '20 at 17:31
0

Instead of passing the wikiList as it is, filter it then send it:

Lets say that you filled up the wikiList, before passing it to the adapter, filter it like this:

In the activity that you initialize the adapter in:

public class YourActivity extends ............{


........
........

//your filled list
private List<Output> wikiList;

//filtered list
private List<Output> filteredList= new ArrayList<Output>();

//filters
private List<String> filters = new ArrayList<String>();


//lets say the user chooses the languages "english" and "russian" after a button click or anything (you can add as many as you want)

filters.add("english");
filters.add("russian");


//now filter the original list

for(int i = 0 ; i<wikiList.size() ; i++){

Output item = wikiList.get(i);

if(filters.contains(item.getLanguage())){

filteredList.add(item);

}


}

//now create your adapter and pass the filteredList instead of the wikiList


AudioAdapter adapter = new AudioAdapter(filteredList , this);


//set the adapter to your recyclerview........
......
.....
......


}

I use above "english" and "russian" for language. I don't know how they are set in your response, maybe you use "en" for "english" so be careful.

Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22
  • This works perfect. My list would grow over item to thousands. Does user have to wait till all the 1000's of objects load ? – Clutchplasdfsdfte Apr 30 '20 at 17:50
  • My solution is to filter the list before passing to adapter so if you already pass 1000 object filtering should reduce them. But I would recommend to do some pagination later in the adapter, but for the filtering it should be fine. – Hasan Bou Taam Apr 30 '20 at 17:52
  • Each language has around 1000 audio files. How can i load it like - every 20 files - then load again like facebook does. My backend is a json file with all the audio file url and each has an id number starting from 1 to 1000. So i would like to load id 1 to 20 first and then on scroll rest of the files. Is this possible with much editing ? – Clutchplasdfsdfte Apr 30 '20 at 19:36