I have a listView who's populated by data inserted in my sqlite Database.
I created a contextMenu so I can be able to delete the Items from my ListView.
When I click on Delete, I want to delete the Item from my database and from my listView. But i'm not able and I don't know why. But I want to delete by getting the string of the Item I, because it does not work with a primary key.
Here's my code :
DatabaseHelper
public void deleteCours(String cours)
{
Open();
db.execSQL("DELETE from "+TABLE_COURS+" WHERE "+COLONNE_COURS+"='"+cours+"'");
db.close();
}
My ListView and the contextmenu
//ce Listener permet de détecter si on clique sur un élément de la liste
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//récupération de l'ID
long itemid= lv.getItemIdAtPosition(i);
//conversion en int
int id= (int)itemid;
//conversion en string du cours que je selectionne
String a =lv.getItemAtPosition(i).toString();
dbhelper.deleteCours(a);
//Intent pour naviguer de pag
/*Intent b = new Intent(AffichageCours.this,AffichageNotes.class);
//création d0un Bundle pour transférer des données
Bundle args = new Bundle();
args.putString("nom_cours",a);
args.putInt("id",id);
//insertion du Bundle dans l'Intent
b.putExtras(args);
//démarrage de l'intent
startActivity(b);*/
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu,menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId())
{
case R.id.supprimer:
// I WANT TO DELETE HERE !
return true;
default:
return super.onContextItemSelected(item);
}
}
}
So, I need to get the string of the item so I can use it in my method. Ii want as well refresh my listview Automatically.
Thank's in advance ! Tell me if you need more code !