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!