I have my RecyclerView that gets the data from my MySQL database through json. The fact is that when I run the app and it gets the remote data, I always put the element with id 1 at the beginning of the list and the others behind ranked in ascending order.
I need the opposite, that is, that the elements with the highest id appear first and the elements with the id less than the end.
Thanks
That is, in the first image appear the elements added before, and in the second the last items added, I want it to be the other way around, that the last items added appear at the beginning and the items added before the end.
That is, how to sort by date
Meta class definition:
public class Meta {
private static final String TAG = Meta.class.getSimpleName();
/*
Atributos
*/
private String idMeta;
private String titulo;
private String descripcion;
private String prioridad;
private String fechaLim;
private String categoria;
public Meta(String idMeta,
String titulo,
String descripcion,
String fechaLim,
String categoria,
String prioridad) {
this.idMeta = idMeta;
this.titulo = titulo;
this.descripcion = descripcion;
this.prioridad = prioridad;
this.fechaLim = fechaLim;
this.categoria = categoria;
}
public String getIdMeta() {
return idMeta;
}
public String getTitulo() {
return titulo;
}
public String getDescripcion() {
return descripcion;
}
public String getPrioridad() {
return prioridad;
}
public String getFechaLim() {
return fechaLim;
}
public String getCategoria() {
return categoria;
}
/**
* Compara los atributos de dos metas
*
* @param meta Meta externa
* @return true si son iguales, false si hay cambios
*/
public boolean compararCon(Meta meta) {
return this.titulo.compareTo(meta.titulo) == 0 &&
this.descripcion.compareTo(meta.descripcion) == 0 &&
this.fechaLim.compareTo(meta.fechaLim) == 0 &&
this.categoria.compareTo(meta.categoria) == 0 &&
this.prioridad.compareTo(meta.prioridad) == 0;
}
}
MetaAdapter:
/**
* Adaptador del recycler view
*/
public class MetaAdapter extends RecyclerView.Adapter<MetaAdapter.MetaViewHolder>
implements ItemClickListener {
/**
* Lista de objetos {@link Meta} que representan la fuente de datos
* de inflado
*/
private List<Meta> items;
/*
Contexto donde actua el recycler view
*/
private Context context;
public MetaAdapter(List<Meta> items, Context context) {
this.context = context;
this.items = items;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public MetaViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_list, viewGroup, false);
return new MetaViewHolder(v, this);
}
@Override
public void onBindViewHolder(MetaViewHolder viewHolder, int i) {
viewHolder.titulo.setText(items.get(i).getTitulo());
viewHolder.prioridad.setText(items.get(i).getPrioridad());
viewHolder.fechaLim.setText(items.get(i).getFechaLim());
viewHolder.categoria.setText(items.get(i).getCategoria());
}
/**
* Sobrescritura del método de la interfaz {@link ItemClickListener}
*
* @param view item actual
* @param position posición del item actual
*/
@Override
public void onItemClick(View view, int position) {
DetailActivity.launch(
(Activity) context, items.get(position).getIdMeta());
}
public static class MetaViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
// Campos respectivos de un item
public TextView titulo;
public TextView prioridad;
public TextView fechaLim;
public TextView categoria;
public ItemClickListener listener;
public MetaViewHolder(View v, ItemClickListener listener) {
super(v);
titulo = (TextView) v.findViewById(R.id.titulo);
prioridad = (TextView) v.findViewById(R.id.prioridad);
fechaLim = (TextView) v.findViewById(R.id.fecha);
categoria = (TextView) v.findViewById(R.id.categoria);
this.listener = listener;
v.setOnClickListener(this);
}
@Override
public void onClick(View v) {
listener.onItemClick(v, getAdapterPosition());
}
}
}
interface ItemClickListener {
void onItemClick(View view, int position);
}
Where the RecyclerView list is loaded:
public class MainFragment extends Fragment {
...
/*
Adaptador del recycler view
*/
private MetaAdapter adapter;
/*
Instancia global del recycler view
*/
private RecyclerView lista;
...
/**
* Interpreta los resultados de la respuesta y así
* realizar las operaciones correspondientes
*
* @param response Objeto Json con la respuesta
*/
private void procesarRespuesta(JSONObject response) {
try {
// Obtener atributo "estado"
String estado = response.getString("estado");
switch (estado) {
case "1": // EXITO
// Obtener array "metas" Json
JSONArray mensaje = response.getJSONArray("metas");
// Parsear con Gson
Meta[] metas = gson.fromJson(mensaje.toString(), Meta[].class);
// Inicializar adaptador
adapter = new MetaAdapter(Arrays.asList(metas), getActivity());
// Setear adaptador a la lista
lista.setAdapter(adapter);
break;
case "2": // FALLIDO
String mensaje2 = response.getString("mensaje");
Toast.makeText(
getActivity(),
mensaje2,
Toast.LENGTH_LONG).show();
break;
}
} catch (JSONException e) {
Log.d(TAG, e.getMessage());
}
}
}