0

In my app I have a ListView that manages the input of the user. The user can input text or image. I have two listeners that interact with the ListView:

LongClickListener

listView.setAdapter(adapter);

//LongClickListener for delete row in listview and row in a mysqlite
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
        ImageView image_to_zoom = (ImageView) view.findViewById(R.id.compiti_imageView);

        if (image_to_zoom.getDrawable() != null) {
            //Image IN
            //Path dell'immagine da cancellare eliminando inizio file:
            String path = image_to_zoom.getTag().toString().substring(5);

            File file = new File(path);
            boolean deleted = file.delete();

            if(deleted) {
                db.deleteSelectedEvento_image(activity_name, path);
                Toast.makeText(getApplicationContext(), "Foto eliminata!", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(), "Errore eliminazione foto", Toast.LENGTH_LONG).show();
            }
        } else {

            //Imane none, cancello il record con il testo
            //Reperisco il testo toccato e lo elimino
            TextView compito = (TextView)view.findViewById(R.id.compiti_text);
            String text_compito = compito.getText().toString();

            db.deleteSelectedEvento_text(activity_name, text_compito);
            Toast.makeText(getApplicationContext(), "Testo eliminato!" , Toast.LENGTH_LONG).show();
        }

        //Lista con il nuovo elemento
        List<Compiti> newcompito = db.CompitiPerData(activity_name);

        if(newcompito != null) {
            //Aggiorno la Listview dell'activity con il nuovo inserimento
            compiti.clear();
            compiti.addAll(newcompito);
            adapter.notifyDataSetChanged();
        }

        return true;
    }
});

That handling the cancellation of items in the ListView. And a normal ClickListener

// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        ImageView image_to_zoom = (ImageView) view.findViewById(R.id.compiti_imageView);

        if (image_to_zoom.getDrawable() != null) {

            //Image IN
            String path = image_to_zoom.getTag().toString();
            openImageReader(path);

        } else {
            //Imane none
            Toast.makeText(getApplicationContext(), "Nessuna foto da visualizzare!" , Toast.LENGTH_LONG).show();

        }
    }
});

That if in the row there is an image loaded with Picasso in the ImageView the app open the gallery viewer. The problem appears when I delete the image with a longpress. The file in memory is deleted, in the record of SQLite too, but the thumbnail of the image still remains in the ListView thanks to cache that store the image. I need something like

Picasso.with(getApplicationContext()).invalidate(image_path);

But my Picasso version is 2.5.2 and the invalidate method doesn't exist.

Why do I have the thumbnail in the listview if I deleted the image file?

This is the adapter that loads the image in the ImageView

@Override
public View getView(int position, View convertView, ViewGroup parent){
    LayoutInflater inflater = (LayoutInflater) getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    convertView = inflater.inflate(R.layout.evento_list_view_line, null);

    //TODO gestire le immagini delle foto
    ImageView img = (ImageView)convertView.findViewById(R.id.compiti_imageView);
    TextView compito = (TextView)convertView.findViewById(R.id.compiti_text);

    Compiti m = getItem(position);

    if(m.getTestoCompito() != null){

        compito.setText(m.getTestoCompito());
        //Invisibile e non presente nel layout
        img.setVisibility(View.GONE);

    } else if (m.getPathFoto() != null){
        //Carico la foto nell'imageView
        Picasso.with(getContext())
                .load(m.getPathFoto())
                .resize(200, 200)
                .centerCrop()
                .into(img);
        //Setto un tag all'imageView per ritrovarla nella funzione di zoom
        img.setTag(m.getPathFoto());
    }
    return convertView;
}

I apologize for some Italian comments in the code!

Dario
  • 732
  • 7
  • 30

1 Answers1

0

I think Picasso is referring to the cached image which it used previously, before deleting.

Try clearing picasso cache for the particular image.

Recent Picasso Version does not support invalidate() method.

Picasso.with(context).invalidate(imagePath);

HourGlass
  • 1,805
  • 1
  • 15
  • 29
  • I get error to this line. `Cannot resolve method getContext()`, the same for `context` invalid symbol, some ideas? Thanks – Dario Dec 15 '15 at 17:04
  • if you are using this code inside adapter use the context that you passed from activity to adapter. if inside activity use. Picasso.with(getApplicationContext (0r) filename.this) – HourGlass Dec 15 '15 at 17:06
  • If i use `Picasso.with(getApplicationContext()).invalidate(path);` Cannot resolve method invalidate. I use it inside the `OnLongClickListener` in the activity. What is filename.this? – Dario Dec 15 '15 at 17:23
  • the .java file name in which OnLongClickListener is present. ex: MainActivity.this. – HourGlass Dec 15 '15 at 17:25
  • Nothing...Cannot resolve method invalidate. I try `image_to_zoom.invalidate()` but nothing the image still remains. And image_to_zoom is the ImageView with the image taken by id – Dario Dec 15 '15 at 17:31
  • Read here [link](http://stackoverflow.com/questions/22243417/android-picasso-clear-cache) the first answere second comment...in the 2.5.2v not work the method invalidate() – Dario Dec 15 '15 at 17:33
  • Picasso.with(ctx).load(new File("/path/to/image")).skipMemoryCache().into(imageView). okay try this load image skipping cache. as you are choosing from gallery. – HourGlass Dec 15 '15 at 17:41
  • With the .skipMemoryCache() the app don't load the image in the listview, but if i delete it he don't remains. So, now the problem is, why i don't see the image now? Because it's not cached? Why? – Dario Dec 15 '15 at 17:51
  • I need some method for delete cache. It's better with the cache because every time that user input something the image disappear and reload with new text/image. With cache nope. But with cache i can't delete it.... – Dario Dec 15 '15 at 17:56
  • Nothing i get errors and it doesn't work and the post is from 2013 when the invalidate doesn't exist. I'm trying to find some better. In time i'm trying to set the imageview.setVisibility(View.GONE); but it's doesn't work too – Dario Dec 15 '15 at 19:23
  • I added the `setIndicatorsEnabled(true)` and before deleting che color is YELLOW and in the documentation there isn't the color yellow, after the delete command it become GREEN, so it's a memory file! [link](http://square.github.io/picasso/) – Dario Dec 15 '15 at 20:26