I'm trying to sort my RecyclerView
with a Spinner
. Every item in the List contains 1 ImageView
and 2 TextView
components. Would be a pleasure if somebody could implement the Spinner
to my code, that I can sort those items.
I tried to implement the spinner twice, but needed to rebuild to recycler view without the spinner, because I failed. Don't know how to set up recycler view and spinner together, even with tutorials... I am stuck.
My Item.xml Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:background="@color/Green">
<ImageView
android:id="@+id/SupItemIV1"
android:layout_width="150dp"
android:layout_height="75dp"
android:padding="2dp"
android:layout_margin="4dp"/>
<TextView
android:id="@+id/SupItemTV1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="160dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="4dp"
android:text="CREATINE"
android:textColor="@color/Black"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/SupItemTV2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="160dp"
android:layout_marginTop="30dp"
android:text="amino acid"
android:textColor="@color/GreyDark"
android:textSize="15sp"
android:textStyle="bold"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
My MainActivity (Here Supplements.java class):
package com.example.etigym;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class Supplements extends AppCompatActivity{
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_supplements);
ArrayList<SupplementsItem> supplementsList = new ArrayList<>();
supplementsList.add(new SupplementsItem(R.drawable.creatine, "CREATINE", "amino acid"));
supplementsList.add(new SupplementsItem(R.drawable.glutamine, "GLUTAMINE", "amino acid"));
supplementsList.add(new SupplementsItem(R.drawable.leucine, "LEUCINE", "amino acid"));
supplementsList.add(new SupplementsItem(R.drawable.valine, "VALINE", "amino acid"));
supplementsList.add(new SupplementsItem(R.drawable.isoleucine, "ISOLEUCINE", "amino acid"));
supplementsList.add(new SupplementsItem(R.drawable.alanine, "ALANINE", "amino acid"));
supplementsList.add(new SupplementsItem(R.drawable.arginine, "ARGININE", "amino acid"));
mRecyclerView = findViewById(R.id.Supplements_RV);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new SupplementsAdapter(supplementsList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
ViewCompat.setNestedScrollingEnabled(mRecyclerView, false);
}
}
My Supplements Layout from the Supplements.java class:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Supplements">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/Supplements_RV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:background="@color/GreyDark"
android:scrollbars="vertical"/>
</RelativeLayout>
My Adapter:
public class SupplementsAdapter extends RecyclerView.Adapter<SupplementsAdapter.SupplementsViewHolder> {
private ArrayList<SupplementsItem> mSupplementsList;
public static class SupplementsViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public SupplementsViewHolder(@NonNull View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.SupItemIV1);
mTextView1 = itemView.findViewById(R.id.SupItemTV1);
mTextView2 = itemView.findViewById(R.id.SupItemTV2);
}
}
public SupplementsAdapter(ArrayList<SupplementsItem> supplementsList){
mSupplementsList = supplementsList;
}
@NonNull
@Override
public SupplementsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.supplements_item, parent, false);
SupplementsViewHolder svh = new SupplementsViewHolder(v);
return svh;
}
@Override
public void onBindViewHolder(@NonNull SupplementsViewHolder holder, int position) {
SupplementsItem currentSupplementsItem = mSupplementsList.get(position);
holder.mImageView.setImageResource(currentSupplementsItem.getImageResource());
holder.mTextView1.setText(currentSupplementsItem.getText1());
holder.mTextView2.setText(currentSupplementsItem.getText2());
}
@Override
public int getItemCount() {
return mSupplementsList.size();
}
}
SupplementsItem.java:
package com.example.etigym;
public class SupplementsItem {
private int mImageResource;
private String mText1;
private String mText2;
public SupplementsItem(int imageResource, String text1, String text2){
mImageResource = imageResource;
mText1 = text1;
mText2 = text2;
}
public int getImageResource(){
return mImageResource;
}
public String getText1(){
return mText1;
}
public String getText2(){
return mText2;
}
}
Afterwards we should have a List which we can sort by categories.
First you have to know how to sort the items on the list according to your needs, to do that you have to create a class that implements Comparator
usually you want to do this class(es) within your Model Class SupplementsItem
, this is how it could look your SupplementsItem
, note that I added an extra property expiryDate
, the idea is that you understand how to use the comparator to sort your list.
public class SupplementsItem {
private int itemImage;
private String item;
private String category;
private long expiryDate;
public SupplementsItem(int itemImage, String item, String category, long expiryDate) {
this.itemImage = itemImage;
this.item = item;
this.category = category;
this.expiryDate = expiryDate;
}
public int getItemImage() {
return itemImage;
}
public void setItemImage(int itemImage) {
this.itemImage = itemImage;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public long getExpiryDate() {
return expiryDate;
}
public void setExpiryDate(long expiryDate) {
this.expiryDate = expiryDate;
}
// This will sort your list ascending by category
public static class CategoryComparatorUp implements Comparator<SupplementsItem> {
@Override
public int compare(SupplementsItem o1, SupplementsItem o2) {
return o1.getCategory().compareToIgnoreCase(o2.getCategory());
}
}
// This will sort your list descending by category
public static class CategoryComparatorDown implements Comparator<SupplementsItem> {
@Override
public int compare(SupplementsItem o1, SupplementsItem o2) {
return o2.getCategory().compareToIgnoreCase(o1.getCategory());
}
}
// This will sort your list ascending by expiration date
public static class ExpiryComparatorUp implements Comparator<SupplementsItem> {
@Override
public int compare(SupplementsItem o1, SupplementsItem o2) {
return Long.compare(o1.getExpiryDate(), o2.getExpiryDate());
}
}
// This will sort your list descending by expiration date
public static class ExpiryComparatorDown implements Comparator<SupplementsItem> {
@Override
public int compare(SupplementsItem o1, SupplementsItem o2) {
return Long.compare(o2.getExpiryDate(), o1.getExpiryDate());
}
}
}
I have placed 4 comparators but you really need only one by category, or make a more complex comparator.
For example if you want to sort by Expiration date descending you want to use ExpiryComparatorUp
and then Collections.reverse()
, like this you can use only one comparator by property in your model.
To use it after you populate your list, you call:
Collections.sort(supplementsList, new SupplementsItem.CategoryComparatorDown());
followed by:
mAdapter.notifyDataSetChanged();
or you can create a method for each type of sorting you want to do, i.e.:
private void sortByCategoryDown() {
Collections.sort(supplementsList, new SupplementsItem.CategoryComparatorDown());
mAdapter.notifyDataSetChanged();
}
and then in your onItemSelected of your spinner just call sortByCategoryDown()
From here you only need to work in your spinner logic. Let me know of your doubts in the comments.
I modified your class with the idea of giving you an idea on how to do the Comparator
thingy, but you don't need to make it like that.