-2

I have been looking into sorting array list of caller names alphabetically. Not sure how to apply collections or anything else, which will be ideal? Any ideas on how to go about it? I have seen a couple of examples online but nothing seemed to help.

Not sure how it applies with respect to my code. My code is as follows:

public class GroupCallAdapter extends RecyclerView.Adapter<GroupCallAdapter.CallerViewHolder> {

private CopyOnWriteArrayList<Caller> callers;

public GroupCallAdapter(@NonNull final CopyOnWriteArrayList<Caller> callers) {
    this.callers = callers;

    for (Caller caller : this.callers) {

    }
}

@Override
public CallerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_caller, parent, false);
    final CallerViewHolder viewHolder = new CallerViewHolder(layout);
    return viewHolder;
}

@Override
public void onBindViewHolder(CallerViewHolder holder, int position) {
    final Caller caller = callers.get(position);
    final Participant participant = caller.getParticipant();

    if (caller.getName() != null) {
        holder.callerName.setText(caller.getName());
    }

    if (caller.getImage() != null) {
        holder.callerImage.setImageBitmap(caller.getImage());
    } else {
        holder.callerImage.setImageResource(R.drawable.call_default_profile);
    }
    holder.callerImage.setVisibility(View.INVISIBLE);
    holder.callerImage.setVisibility(View.VISIBLE);

    final VideoView videoView;
    if (participant.isMe) {
        videoView = participant.videoStreamer.videoView;
    } else {
        videoView = participant.videoPlayer.videoView;
    }
    if (holder.callerVideo.getChildCount() != 0) {
        holder.callerVideo.removeAllViews();
    }
    if (videoView.getParent() != null) {
        ViewGroup parent = (ViewGroup) videoView.getParent();
        parent.removeView(videoView);
    }
    holder.callerVideo.addView(videoView, CallerViewHolder.videoLayoutParams);

    if (caller.isVideoPaused()) {
        holder.callerVideo.setVisibility(View.INVISIBLE);
        holder.callerImage.setVisibility(View.VISIBLE);
    } else {
        holder.callerVideo.setVisibility(View.VISIBLE);
        holder.callerImage.setVisibility(View.INVISIBLE);
    }
}

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

public static final class CallerViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.callerVideo)
    public ViewGroup callerVideo;
    @BindView(R.id.callerImage)
    public ImageView callerImage;
    @BindView(R.id.callerName)
    public TextView callerName;

    protected static FrameLayout.LayoutParams videoLayoutParams = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
    );

    public CallerViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Use `Collections.sort()` somewhere upstream, wherever you are creating this `CopyOnWriteArrayList`. Or, have this `RecyclerView.Adapter` work off of a sorted copy of the list that it creates (again, using `Collections.sort()`). – CommonsWare Dec 09 '17 at 22:13

3 Answers3

1

Just have caller implement Comparable and then call Collections.sort(callers)

Like this:

public class Caller implements Comparable<Caller> {

    String name;

    public int compareTo(Caller other) {
        return name.compareTo(other.name);
    }

}

And then:

Collections.sort(callers);
MidasLefko
  • 4,499
  • 1
  • 31
  • 44
0

You can use this code to sort a list alphabetically:

Collections.sort(list);
Abir Hossain
  • 111
  • 7
0

I found an answer from here.

Collections.sort(list, new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        return s1.compareToIgnoreCase(s2);
    }
});
Pang
  • 9,564
  • 146
  • 81
  • 122
Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105