I'm making an app that shows the days of the week coming from FireBase, the problem is that I have them messed up and I want them to be sorted.
I have searched in many places and put order with the sort method of the adapter:
Sort listview with array adapter
The problem is that I can not use this method because I use a FirebaseRecyclerAdapter which derives from an Adapter, which does not have the sort method.
At the end I was able to sort the data of the adapter but when I show it only the last element is shown and the previous one is not.
I put the code:
The adapter:
public class MyFireAdapterDiasFiestaRecyclerView extends FirebaseRecyclerAdapter<DiaFiestaMeta, MyFireAdapterDiasFiestaRecyclerView.MyFireViewHolder>
implements View.OnClickListener {
private View.OnClickListener listener;
private List<DiaFiestaMetaFecha> dias;
private static List<TextView> views ;
private DiaFiestaMetaFecha fechaDia;
public MyFireAdapterDiasFiestaRecyclerView(Class<DiaFiestaMeta> modelClass, int modelLayout, Class<MyFireViewHolder> viewHolderClass, DatabaseReference ref) {
super(modelClass, modelLayout, viewHolderClass, ref);
dias = new ArrayList<>();
views = new ArrayList<>();
}
@Override
public MyFireViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fila_diasfiesta_layout, viewGroup, false);
itemView.setOnClickListener(this);
MyFireViewHolder holder = new MyFireViewHolder(itemView);
return holder;
}
public void setOnClickListener(View.OnClickListener listener) {
this.listener = listener;
}
@Override
public void onClick(View view) {
if (listener != null)
listener.onClick(view);
}
@Override
protected void populateViewHolder(MyFireViewHolder viewHolder, DiaFiestaMeta modelo, int position) {
fechaDia = new DiaFiestaMetaFecha(modelo.getUidDiaFiesta(), modelo.getTituloDiaFiesta());
dias.add(fechaDia);
if (this.getItemCount() == position + 1) {
viewHolder.bindDatos(dias);
}
}
public static class MyFireViewHolder extends RecyclerView.ViewHolder {
private TextView textoTv;
private int num;
public MyFireViewHolder(View itemView) {
super(itemView);
textoTv = (TextView) itemView.findViewById(R.id.tvTituloDiaFiesta);
views.add(textoTv);
}
public void bindDatos(String titulo) {
textoTv.setText(titulo);
}
public void bindDatos(List<DiaFiestaMetaFecha> dias) {
burbuja(dias);
for (int i = 0; i < dias.size(); i++) {
views.get(i).setText(dias.get(i).getTituloDiaFiesta());
}
}
}
}
And the fragment where I use it:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
uidsFiestas = getUidsFiestas();
//Por defecto mostramos la ultima fecha
uidFiestaSeleccionada = getUltimaFiesta();
adaptadorFiestas = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, uidsFiestas);
//Ponemos el uidFiestaSeleccionada el primero para que nos lo muestre
adaptadorFiestas.remove(uidFiestaSeleccionada);
adaptadorFiestas.insert(uidFiestaSeleccionada,0);
mDataBaseDiasFiestaSelRef = mDataBaseFiestasRef.child(uidFiestaSeleccionada).child("diasFiestas");
adaptadorDiasFiestas = new MyFireAdapterDiasFiestaRecyclerView(DiaFiestaMeta.class, R.layout.fila_diasfiesta_layout, MyFireAdapterDiasFiestaRecyclerView.MyFireViewHolder.class, mDataBaseDiasFiestaSelRef);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fiestas_layout, container, false);
ButterKnife.bind(this, view);
spFiestas.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView textoTv = (TextView) view;
uidFiestaSeleccionada = textoTv.getText().toString();
//fiestaSeleccionada = crearFiesta(uidFiestaSeleccionada);
mDataBaseDiasFiestaSelRef = mDataBaseFiestasRef.child(uidFiestaSeleccionada).child("diasFiestas");
adaptadorDiasFiestas = new MyFireAdapterDiasFiestaRecyclerView(DiaFiestaMeta.class, R.layout.fila_diasfiesta_layout, MyFireAdapterDiasFiestaRecyclerView.MyFireViewHolder.class, mDataBaseDiasFiestaSelRef);
adaptadorViejoDiasFiestas = (MyFireAdapterDiasFiestaRecyclerView) recView.getAdapter();
recView.setAdapter(adaptadorDiasFiestas);
if (adaptadorViejoDiasFiestas!=null)
adaptadorViejoDiasFiestas.cleanup();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spFiestas.setAdapter(adaptadorFiestas);
setHasOptionsMenu(true);
recView.setHasFixedSize(true);
manager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
recView.setLayoutManager(manager);
recView.setAdapter(adaptadorDiasFiestas);
return view;
}
Thanks!