Greetings this is my first post and the problem I have is that I am making an application in which I store images in webservice
and as a response I get a array
with the names of the images that were stored, this process and update the field Sync
in BD
to indicate that this image was already sent, all this function well, the problem is when I want to update my recyclerview
to show a Check
indicating that the image was already sent this one does not do it.
By debugging I realized that the problem is that the data is updated in the BD
but when the cursor is filled it continues to have the same data and not the new ones even though I close the cursor, the BD
and the connection.
To see the changes I have to close the activity where the images are being displayed and open it again.
I will be doing something wrong with your help please.
This is the function with which you sent the images and updated the field Sync
in BD
private void crearObjetoJson() {
try {
jsonArray = new JSONArray();
for (int i = 0; i < listSeleccion.size(); i++) {
objetoFotos = new JSONObject();
objetoFotos.put("NOM_FOTOGRAFIA", listSeleccion.get(i).getNombreFoto());
Bitmap bitmap = BitmapFactory.decodeFile(listSeleccion.get(i).getUbicacion());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
byte[] array = stream.toByteArray();
String foto_codificada = Base64.encodeToString(array,Base64.DEFAULT);
objetoFotos.put("IMAGEN",foto_codificada);
jsonArray.put(objetoFotos);
}
//String array = jsonArray.toString();
//System.out.println("jsonString: " + array);
}catch (JSONException e){
e.printStackTrace();
}
JsonArrayRequest jsonRequest = new JsonArrayRequest(Request.Method.POST, URL_ENVIO_FOTOS, jsonArray,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
Log.i("JsonArray",response.toString());
DBHelper conn = new DBHelper(Fotografias_Capturadas.this,DBHelper.DB_NAME,null,DBHelper.DB_VERSION);
SQLiteDatabase db = conn.getWritableDatabase();
db.beginTransaction();
for(int i=0;i<response.length();i++){
JSONObject jresponse = response.getJSONObject(i);
String nom_Fotografia = jresponse.getString("NOM_FOTOGRAFIA");
Log.i("NomFoto",nom_Fotografia);
String Query = "UPDATE FOTOGRAFIAS SET SYNCRONIZACION='1' WHERE NOM_FOTOGRAFIA ='" + nom_Fotografia + "'";
db.execSQL(Query);
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
conn.close();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Fotografias_Capturadas.this, error.getMessage(),Toast.LENGTH_SHORT).show();
}
});
VolleySingleton.getInstanciaVolley(Fotografias_Capturadas.this).addToRequestQueue(jsonRequest);
}
This is the class with which I upload my images and show them in recycler
.
private void cargar_imagenes() {
DBHelper conn = new DBHelper(this,DBHelper.DB_NAME,null,DBHelper.DB_VERSION);
SQLiteDatabase db = conn.getReadableDatabase();
Cursor cursor =null;
cursor = db.rawQuery("SELECT CODIGO as _id, CODIGO_DEFECTO, CODIGO_CIRCUITO, NOM_FOTOGRAFIA, SYNCRONIZACION FROM FOTOGRAFIAS",null);
DatosFotografias datos;
listFotografias = new ArrayList<>();
while (cursor.moveToNext()){
datos = new DatosFotografias();
datos.setCodigo(cursor.getString(0));
datos.setCodigoDefecto(cursor.getString(1));
datos.setCodigoSegmento(cursor.getString(2));
datos.setNombreFoto(cursor.getString(3));
datos.setUbicacion(RutaFotos + "/" + cursor.getString(3) + ".JPG");
datos.setSync(cursor.getString(4));
listFotografias.add(datos);
}
cursor.close();
db.close();
conn.close();
adapter = new AdapterFotografias(this,listFotografias,this);
recyclerFotos.setAdapter(adapter);
recyclerFotos.setItemAnimator(new DefaultItemAnimator());
}
Adapter
public class AdapterFotografias extends RecyclerView.Adapter<AdapterFotografias.ViewHolder> {
Context context;
ArrayList<DatosFotografias> listFotos;
Fotografias_Capturadas activity;
FrameLayout frameLayout;
View view;
int posicion;
public AdapterFotografias(Context context, ArrayList<DatosFotografias> listFotografias, Fotografias_Capturadas activity) {
this.context = context;
this.listFotos = listFotografias;
this.activity =activity;
}
@Override
public AdapterFotografias.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fotografias_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(AdapterFotografias.ViewHolder holder, int position) {
posicion=holder.getAdapterPosition();
if (!activity.is_in_actionMode){
holder.checkBoxFotos.setVisibility(View.INVISIBLE);
}else {
holder.checkBoxFotos.setVisibility(View.VISIBLE);
}
Uri uri = Uri.fromFile(new File(listFotos.get(position).getUbicacion()));
Glide.with(context)
.load(uri)
.centerCrop()
.placeholder(R.drawable.camara)
.crossFade()
.into(holder.imageFoto);
holder.imageFoto.setScaleType(ImageView.ScaleType.FIT_CENTER);
if (listFotos.get(position).getSync().equals("0")){
holder.imageCheck.setImageResource(R.drawable.wait);
}else{
holder.imageCheck.setImageResource(R.drawable.check);
}
}
@Override
public int getItemCount() {
return (listFotos == null) ?0:listFotos.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView imageFoto, imageCheck;
CheckBox checkBoxFotos;
public ViewHolder(View itemView) {
super(itemView);
imageFoto = itemView.findViewById(R.id.ImagenFotoTotal);
imageCheck = itemView.findViewById(R.id.ImagenCheckTotal);
frameLayout = itemView.findViewById(R.id.frameLayout);
checkBoxFotos = itemView.findViewById(R.id.checkboxFotos);
imageFoto.setOnLongClickListener(activity);
imageFoto.setOnClickListener(activity);
checkBoxFotos.setOnClickListener(this);
}
@Override
public void onClick(View view) {
activity.prepararSeleccion(view,getAdapterPosition());
activity.mostrarImagenSeleccionada(getAdapterPosition());
}
}
public void removeAdapter(ArrayList<DatosFotografias> list){
for (DatosFotografias fotografias : list){
listFotos.remove(fotografias);
}
notifyDataSetChanged();
}
}