I was working with an example of a RecyclerView, in that RecyclerView, I inserted an image of a web server by means of the URL of the image ... Well at the time of installing the application everything looks good the image I had in the web server, but when updating the image on my server, the image in my application is not updated. I have to delete the application and install it again to update the image. I would like the image to be updated at the moment when I update the image of my server.
Main Activity java code
package com.herprogramacion.alquileres;
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.herprogramacion.alquileres.provider.Contrato.Alquileres;
public class ActividadListaAlquileres extends AppCompatActivity implements AdaptadorAlquileres.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {
private RecyclerView listaUI;
private LinearLayoutManager linearLayoutManager;
private AdaptadorAlquileres adaptador;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actividad_lista_alquileres);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Filtro...", Snackbar.LENGTH_LONG)
.setAction("Acción", null).show();
}
});
// Preparar lista
listaUI = (RecyclerView) findViewById(R.id.lista);
listaUI.setHasFixedSize(true);
linearLayoutManager = new LinearLayoutManager(this);
listaUI.setLayoutManager(linearLayoutManager);
adaptador = new AdaptadorAlquileres(this, this);
listaUI.setAdapter(adaptador);
// Iniciar loader
getSupportLoaderManager().restartLoader(1, null, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_actividad_lista_alquileres, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(AdaptadorAlquileres.ViewHolder holder, String idAlquiler) {
Snackbar.make(findViewById(android.R.id.content), ":id = " + idAlquiler,
Snackbar.LENGTH_LONG).show();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(this, Alquileres.URI_CONTENIDO, null, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (adaptador != null) {
adaptador.swapCursor(data);
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
}
Java Activity Code Adapter
package com.herprogramacion.alquileres;
import android.content.Context;
import android.database.Cursor;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
public class AdaptadorAlquileres extends RecyclerView.Adapter<AdaptadorAlquileres.ViewHolder> {
private final Context contexto;
private Cursor items;
private OnItemClickListener escucha;
interface OnItemClickListener {
public void onClick(ViewHolder holder, String idAlquiler);
}
public class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
// Referencias UI
public TextView viewNombre;
public TextView viewUbicacion;
public TextView viewDescripcion;
public TextView viewPrecio;
public ImageView viewFoto;
public ViewHolder(View v) {
super(v);
viewNombre = (TextView) v.findViewById(R.id.nombre);
viewUbicacion = (TextView) v.findViewById(R.id.ubicacion);
viewDescripcion = (TextView) v.findViewById(R.id.descripcion);
viewPrecio = (TextView) v.findViewById(R.id.precio);
viewFoto = (ImageView) v.findViewById(R.id.foto);
v.setOnClickListener(this);
}
@Override
public void onClick(View view) {
escucha.onClick(this, obtenerIdAlquiler(getAdapterPosition()));
}
}
private String obtenerIdAlquiler(int posicion) {
if (items != null) {
if (items.moveToPosition(posicion)) {
return items.getString(ConsultaAlquileres.ID_ALQUILER);
}
}
return null;
}
public AdaptadorAlquileres(Context contexto, OnItemClickListener escucha) {
this.contexto = contexto;
this.escucha = escucha;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_lista_alquiler, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
items.moveToPosition(position);
String s;
// Asignación UI
s = items.getString(ConsultaAlquileres.NOMBRE);
holder.viewNombre.setText(s);
s = items.getString(ConsultaAlquileres.UBICACION);
holder.viewUbicacion.setText(s);
s = items.getString(ConsultaAlquileres.DESCRIPCION);
holder.viewDescripcion.setText(s);
s = items.getString(ConsultaAlquileres.PRECIO);
holder.viewPrecio.setText(String.format("%s", s));
s = items.getString(ConsultaAlquileres.URL);
Glide.with(contexto).load(s).centerCrop().into(holder.viewFoto);
}
@Override
public int getItemCount() {
if (items != null)
return items.getCount();
return 0;
}
public void swapCursor(Cursor nuevoCursor) {
if (nuevoCursor != null) {
items = nuevoCursor;
notifyDataSetChanged();
}
}
public Cursor getCursor() {
return items;
}
interface ConsultaAlquileres {
int ID_ALQUILER = 1;
int NOMBRE = 2;
int UBICACION = 3;
int DESCRIPCION = 4;
int PRECIO = 5;
int URL = 6;
}
}
SQL code in the sqlite database where I insert THE IMAGES
package com.herprogramacion.alquileres.provider;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import com.herprogramacion.alquileres.provider.Contrato.Alquileres;
public class BaseDatos extends SQLiteOpenHelper {
static final int VERSION = 1;
static final String NOMBRE_BD = "alquileres.db";
interface Tablas {
String APARTAMENTO = "alquiler";
}
public BaseDatos(Context context) {
super(context, NOMBRE_BD, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"CREATE TABLE " + Tablas.APARTAMENTO + "("
+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ Alquileres.ID_ALQUILER + " TEXT UNIQUE NOT NULL,"
+ Alquileres.NOMBRE + " TEXT NOT NULL,"
+ Alquileres.UBICACION + " TEXT NOT NULL,"
+ Alquileres.DESCRIPCION + " TEXT NOT NULL,"
+ Alquileres.PRECIO + " REAL NOT NULL,"
+ Alquileres.URL_IMAGEN + " TEXT NOT NULL)");
// Registro ejemplo #1
ContentValues valores = new ContentValues();
valores.put(Alquileres.ID_ALQUILER, Alquileres.generarIdAlquiler());
valores.put(Alquileres.NOMBRE, "Mis representantes");
valores.put(Alquileres.UBICACION, "México");
valores.put(Alquileres.DESCRIPCION, "Conoce a los posibles candidatos a la presidencia de México 2018");
valores.put(Alquileres.PRECIO, "¿Quiénes son?");
valores.put(Alquileres.URL_IMAGEN, "http://parkwebstudio.com/images/misrepresentantes.jpg");
db.insertOrThrow(Tablas.APARTAMENTO, null, valores);
// Registro ejemplo #2
valores.put(Alquileres.ID_ALQUILER, Alquileres.generarIdAlquiler());
valores.put(Alquileres.NOMBRE, "Elecciones");
valores.put(Alquileres.UBICACION, "México");
valores.put(Alquileres.DESCRIPCION, "Las elecciones federales de México de 2018, denominadas oficialmente por la autoridad electoral como el Proceso Electoral Federal 2017 — 2018...");
valores.put(Alquileres.PRECIO, "¡Enterate!");
valores.put(Alquileres.URL_IMAGEN, "http://parkwebstudio.com/images/elecciones.jpg");
db.insertOrThrow(Tablas.APARTAMENTO, null, valores);
// Registro ejemplo #3
valores.put(Alquileres.ID_ALQUILER, Alquileres.generarIdAlquiler());
valores.put(Alquileres.NOMBRE, "Reformas y leyes");
valores.put(Alquileres.UBICACION, "México");
valores.put(Alquileres.DESCRIPCION, "Enterate de las reformas federales vigentes asi como las proximas a tratar");
valores.put(Alquileres.PRECIO, "¿En que nos afectan?");
valores.put(Alquileres.URL_IMAGEN, "http://parkwebstudio.com/images/reformasyleyes.jpg");
db.insertOrThrow(Tablas.APARTAMENTO, null, valores);
// Registro ejemplo #4
valores.put(Alquileres.ID_ALQUILER, Alquileres.generarIdAlquiler());
valores.put(Alquileres.NOMBRE, "Noticias");
valores.put(Alquileres.UBICACION, "México");
valores.put(Alquileres.DESCRIPCION, "Noticias en tiempo real y anuncios clasificados de todo lo relacionado a las elecciones 2018");
valores.put(Alquileres.PRECIO, "Mantente enterado");
valores.put(Alquileres.URL_IMAGEN, "http://parkwebstudio.com/images/noticias.jpg");
db.insertOrThrow(Tablas.APARTAMENTO, null, valores);
// Registro ejemplo #5
valores.put(Alquileres.ID_ALQUILER, Alquileres.generarIdAlquiler());
valores.put(Alquileres.NOMBRE, "Movilízate");
valores.put(Alquileres.UBICACION, "México");
valores.put(Alquileres.DESCRIPCION, "Enterate que puedes hacer por tu pais, movilizate!!!");
valores.put(Alquileres.PRECIO, "¿Por qué?");
valores.put(Alquileres.URL_IMAGEN, "http://parkwebstudio.com/images/movilizate.jpg");
db.insertOrThrow(Tablas.APARTAMENTO, null, valores);
// Registro ejemplo #6
valores.put(Alquileres.ID_ALQUILER, Alquileres.generarIdAlquiler());
valores.put(Alquileres.NOMBRE, "¿Porqué participar?");
valores.put(Alquileres.UBICACION, "México");
valores.put(Alquileres.DESCRIPCION, "No te preguntes qué puede hacer tu país por ti, pregúntate que puedes hacer tú por tu país");
valores.put(Alquileres.PRECIO, "¡Animate!");
valores.put(Alquileres.URL_IMAGEN, "http://parkwebstudio.com/images/porqueparticipar.jpg");
db.insertOrThrow(Tablas.APARTAMENTO, null, valores);
// Registro ejemplo #7
valores.put(Alquileres.ID_ALQUILER, Alquileres.generarIdAlquiler());
valores.put(Alquileres.NOMBRE, "¿Quiénes somos?");
valores.put(Alquileres.UBICACION, "México");
valores.put(Alquileres.DESCRIPCION, "Somos una asociacion civil comprometida con nuestro pais, y el futuro que conlleva un nuevo presidente");
valores.put(Alquileres.PRECIO, "Conócenos");
valores.put(Alquileres.URL_IMAGEN, "http://parkwebstudio.com/images/quienessomos.jpg");
db.insertOrThrow(Tablas.APARTAMENTO, null, valores);
// Registro ejemplo #8
valores.put(Alquileres.ID_ALQUILER, Alquileres.generarIdAlquiler());
valores.put(Alquileres.NOMBRE, "Ubica tu casilla");
valores.put(Alquileres.UBICACION, "México");
valores.put(Alquileres.DESCRIPCION, "¿No sabes donde esta tu casilla? Enterate ahora!!!");
valores.put(Alquileres.PRECIO, "¡Qué no se te pase!");
valores.put(Alquileres.URL_IMAGEN, "http://parkwebstudio.com/images/ubicatucasilla.jpg");
db.insertOrThrow(Tablas.APARTAMENTO, null, valores);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
try {
db.execSQL("DROP TABLE IF EXISTS " + Tablas.APARTAMENTO);
} catch (SQLiteException e) {
// Manejo de excepciones
}
onCreate(db);
}
}
Java code of the contract with the structure of the database and form of the URIs
package com.herprogramacion.alquileres.provider;
import android.net.Uri;
import java.util.UUID;
public class Contrato {
interface ColumnasAlquiler {
String ID_ALQUILER = "idAlquiler"; // Pk
String NOMBRE = "nombre";
String UBICACION = "ubicacion";
String DESCRIPCION = "descripcion";
String PRECIO = "precio";
String URL_IMAGEN ="urlImagen";
}
// Autoridad del Content Provider
public final static String AUTORIDAD = "com.herprogramacion.alquileres";
// Uri base
public final static Uri URI_CONTENIDO_BASE = Uri.parse("content://" + AUTORIDAD);
/**
* Controlador de la tabla "alquiler"
*/
public static class Alquileres implements ColumnasAlquiler {
public static final Uri URI_CONTENIDO =
URI_CONTENIDO_BASE.buildUpon().appendPath(RECURSO_ALQUILERES).build();
public final static String MIME_RECURSO =
"vnd.android.cursor.item/vnd." + AUTORIDAD + "/" + RECURSO_ALQUILERES;
public final static String MIME_COLECCION =
"vnd.android.cursor.dir/vnd." + AUTORIDAD + "/" + RECURSO_ALQUILERES;
/**
* Construye una {@link Uri} para el {@link #ID_ALQUILER} solicitado.
*/
public static Uri construirUriAlquiler(String idApartamento) {
return URI_CONTENIDO.buildUpon().appendPath(idApartamento).build();
}
public static String generarIdAlquiler() {
return "A-" + UUID.randomUUID();
}
public static String obtenerIdAlquiler(Uri uri) {
return uri.getLastPathSegment();
}
}
// Recursos
public final static String RECURSO_ALQUILERES = "alquileres";
}
Java code of the provider encapsulator, encapsulates access to the database
package com.herprogramacion.alquileres.provider;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;
import com.herprogramacion.alquileres.provider.BaseDatos.Tablas;
import com.herprogramacion.alquileres.provider.Contrato.Alquileres;
public class ProviderApartamentos extends ContentProvider {
// Comparador de URIs
public static final UriMatcher uriMatcher;
// Casos
public static final int ALQUILERES = 100;
public static final int ALQUILERES_ID = 101;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(Contrato.AUTORIDAD, "alquileres", ALQUILERES);
uriMatcher.addURI(Contrato.AUTORIDAD, "alquileres/*", ALQUILERES_ID);
}
private BaseDatos bd;
private ContentResolver resolver;
@Override
public boolean onCreate() {
bd = new BaseDatos(getContext());
resolver = getContext().getContentResolver();
return true;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case ALQUILERES:
return Alquileres.MIME_COLECCION;
case ALQUILERES_ID:
return Alquileres.MIME_RECURSO;
default:
throw new IllegalArgumentException("Tipo desconocido: " + uri);
}
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// Obtener base de datos
SQLiteDatabase db = bd.getWritableDatabase();
// Comparar Uri
int match = uriMatcher.match(uri);
Cursor c;
switch (match) {
case ALQUILERES:
// Consultando todos los registros
c = db.query(Tablas.APARTAMENTO, projection,
selection, selectionArgs,
null, null, sortOrder);
c.setNotificationUri(resolver, Alquileres.URI_CONTENIDO);
break;
case ALQUILERES_ID:
// Consultando un solo registro basado en el Id del Uri
String idApartamento = Alquileres.obtenerIdAlquiler(uri);
c = db.query(Tablas.APARTAMENTO, projection,
Alquileres.ID_ALQUILER + "=" + "\'" + idApartamento + "\'"
+ (!TextUtils.isEmpty(selection) ?
" AND (" + selection + ')' : ""),
selectionArgs, null, null, sortOrder);
c.setNotificationUri(resolver, uri);
break;
default:
throw new IllegalArgumentException("URI no soportada: " + uri);
}
return c;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return 0;
}
}