0

I've a RecyclerView populated by an ArrayList of custom objects. I've implemented Comparable interface to sort items in array list based on index. Sorting gives me the desired results but when i populate RecyclerView, my items appear in the order they were in before sorting.

I'm calling Collection.sort(arrayList) in the constructor of Adaper like this:

public MyRecyclerViewListAdapter(List<Something> somethings, OnStartDragListener mDragStartListener) {
    this.mDragStartListener = mDragStartListener;
    this.somethings = somethings;
    Collections.sort(this.somethings);
}

Even I sorted the list before sending it in as an argument to the constructor:

    Collections.sort(this.somethings);
    myRecyclerViewListAdapter = new myRecyclerViewListAdapter(somethings, this);

My Adapter code:

public class DefaultCurrenciesAdapter extends RecyclerView.Adapter<DefaultCurrenciesAdapter.MyViewHolder> implements ItemTouchHelperAdapter{

private List<Currency> currencies;
DataManager dataManager = new DataManager();
private OnStartDragListener mDragStartListener;

public DefaultCurrenciesAdapter(List<Currency> currencies, OnStartDragListener mDragStartListener) {
    this.mDragStartListener = mDragStartListener;
    this.currencies = currencies;
    Collections.sort(this.currencies);
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    View currencyView = inflater.inflate(R.layout.currency_list,parent,false);
    MyViewHolder viewHolder = new MyViewHolder(currencyView);

    return viewHolder;
}

//populate data
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Currency currency = currencies.get(position);
    TextView tvCurrencyValue = holder.tvCurrencyValue;

    TextView tvCurrencyName=holder.tvCurrencyName;
    tvCurrencyName.setText(currencies.get(position).getCode() + " - " + currencies.get(position).getName());
    TextView tvUnitValue = holder.tvUnitValue;
    tvUnitValue.setText("1 " + currencies.get(position).getCode()+ " = " +Double.toString(round(currencies.get(position).getRate()/ Currency.getBaseCurrencyRate(),2)) +" "+ Currency.getBaseCurrencyCode());
    holder.ivFlag.setImageResource(App.getmContext().getResources().getIdentifier("drawable/"+currencies.get(position).getCountryCode().toLowerCase(),null,App.getmContext().getPackageName()));
}

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

@Override
public boolean onItemMove(int fromPosition, int toPosition) {
    Collections.swap(currencies, fromPosition, toPosition);
    int tempSort = currencies.get(fromPosition).getSortOrder();
    dataManager.setSortOrder(currencies.get(fromPosition).getCode(), currencies.get(toPosition).getSortOrder());
    dataManager.setSortOrder(currencies.get(toPosition).getCode(), tempSort);
    notifyItemMoved(fromPosition, toPosition);
    return true;
}

@Override
public void onItemDismiss(int position) {
    dataManager.removeDefaultCurrency(currencies.get(position).getCode());
    currencies.remove(position);
    notifyItemRemoved(position);
}


public static class MyViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder,View.OnClickListener{

    public TextView tvCurrencyValue;
    public TextView tvCurrencyDecimals;
    public TextView tvCurrencyName;
    public TextView tvUnitValue;
    public ImageView ivFlag;

    public MyViewHolder(View itemView) {
        super(itemView);

        tvCurrencyValue = (TextView) itemView.findViewById(R.id.tvCurrencyValue);
        tvCurrencyDecimals = (TextView) itemView.findViewById(R.id.tvCurrencyValueDecimals);
        tvCurrencyName = (TextView)itemView.findViewById(R.id.tvCurrencyExp);
        tvUnitValue = (TextView)itemView.findViewById(R.id.tvUnitValue);
        ivFlag = (ImageView)itemView.findViewById(R.id.ivFlag);

        Typeface fontMontserratSemiBold = Typeface.createFromAsset(App.getmContext().getAssets(), "fonts/Montserrat-SemiBold.otf");
        Typeface fontMontserratRegular = Typeface.createFromAsset(App.getmContext().getAssets(), "fonts/Montserrat-Regular.otf");

        tvCurrencyValue.setTypeface(fontMontserratSemiBold);
        tvCurrencyDecimals.setTypeface(fontMontserratSemiBold);
        tvCurrencyName.setTypeface(fontMontserratRegular);
        tvUnitValue.setTypeface(fontMontserratRegular);
    }

    @Override
    public void onItemSelected() {
    }

    @Override
    public void onItemClear() {
        itemView.setBackgroundColor(0);
    }

    @Override
    public void onClick(View view) {

    }
}

} `

Arsal
  • 565
  • 2
  • 8
  • 17
  • Show your Adapter code please. – earthw0rmjim Sep 11 '16 at 04:33
  • you mean the whole of adapter class? thats a lot of code – Arsal Sep 11 '16 at 04:35
  • 1
    Well, if you're absolutely sure the `sort()` works properly and you're passing the properly sorted `List` to the `Adapter`, the problem must be there. – earthw0rmjim Sep 11 '16 at 04:37
  • Have you heard about the `SortedList`? It's a way to sort items in an Adapter based on a `Comparator`. It should be exactly what you are looking for and more! The `SortedList` has many other useful features. There is an example project I maintain which shows how to use the `SortedList`, you can find it here: https://wrdlbrnft.github.io/Searchable-RecyclerView-Demo/ The related Stack Overflow answer can be found here: http://stackoverflow.com/a/30429439/2310866 – Xaver Kapeller Sep 11 '16 at 04:37
  • How do you populate the views in the RecycleView? I suggest creating a simplified project to test this with. – Code-Apprentice Sep 11 '16 at 04:42
  • @user13 I've added my adapter code. P.S. I'm absolutely sure my list is correctly sorted. – Arsal Sep 11 '16 at 04:52
  • i believe the problem is with onBindViewHolder() – Arsal Sep 11 '16 at 04:57
  • i solved the problem by moving Collections.sort(myArray) inside onBindViewHolder() @user13 any thoughts on this? – Arsal Sep 11 '16 at 05:03
  • i cannot understand why in need to sort inside onBindViewHolder() even to I already sorted it in constructor and even before passing it as an argument to constructor – Arsal Sep 11 '16 at 05:08
  • 1
    as you solved your own issue put it as an answer to help others in the future. – Tasos Sep 11 '16 at 05:11
  • unfortunately that wasn't a good solution. My `RecyclerView` started to show duplicate entries. so this question is still open – Arsal Sep 11 '16 at 19:44

0 Answers0