Clics longs sur une ListView affichant des enregistrements SQLite pour en supprimer
Bonjour à tous,
Je me permets de créer ce topic suite à celui-ci: http://www.developpez.net/forums/d12...ableau-string/
J'ai réussi à afficher dans une ListView le contenu d'une base de données SQLite, qui contient dans une table Reservation les attributs ID (long), Reference et LastName (String), et Reservation étant un objet Java créé par moi-même, ayant ces 3 attributs.
Je voudrais à présent que lorsque je fasse un clique long sur un élément de cette ListView, il m'affiche une boite de dialogue pour me demander confirmation de la suppression de l'enregistrement cliqué, puis le supprime de la ListView et de la base SQLite.
Voici mon code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
/* Display the bdd */
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this.getBaseContext(), R.layout.flight_book_list, c, from, to);
ReservationList.setAdapter(mAdapter);
//Gérer le clic long sur un élément de la listView
ReservationList.setOnItemLongClickListener (new OnItemLongClickListener()
{
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
return onLongListItemClick(view, position, id);
}
protected boolean onLongListItemClick(View v, final int pos, long id)
{
final String str=ReservationList.getItemAtPosition(pos).toString();
Log.i("ListView", "onLongListItemClick stirng=" + str);
AlertDialog.Builder builder = new AlertDialog.Builder(getBaseContext());
builder.setMessage("Delete this entry from Flight Book?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
}
}); |
Quand je clique longuement sur un enregistrement de la ListView, l'application (comme à sa grande habitude de ne pas se casser la tête) plante.
Voici le ChatDeJournal correspondant:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
11-18 19:55:03.996: E/AndroidRuntime(30759): FATAL EXCEPTION: main
11-18 19:55:03.996: E/AndroidRuntime(30759): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
11-18 19:55:03.996: E/AndroidRuntime(30759): at android.view.ViewRootImpl.setView(ViewRootImpl.java:589)
11-18 19:55:03.996: E/AndroidRuntime(30759): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
11-18 19:55:03.996: E/AndroidRuntime(30759): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
11-18 19:55:03.996: E/AndroidRuntime(30759): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
11-18 19:55:03.996: E/AndroidRuntime(30759): at android.app.Dialog.show(Dialog.java:277)
11-18 19:55:03.996: E/AndroidRuntime(30759): at com.example.mon.app.FlightBook$1.onLongListItemClick(FlightBook.java:68)
11-18 19:55:03.996: E/AndroidRuntime(30759): at com.example.mon.app.FlightBook$1.onItemLongClick(FlightBook.java:47)
11-18 19:55:03.996: E/AndroidRuntime(30759): at android.widget.AbsListView.performLongPress(AbsListView.java:2927)
11-18 19:55:03.996: E/AndroidRuntime(30759): at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2877)
11-18 19:55:03.996: E/AndroidRuntime(30759): at android.os.Handler.handleCallback(Handler.java:615)
11-18 19:55:03.996: E/AndroidRuntime(30759): at android.os.Handler.dispatchMessage(Handler.java:92)
11-18 19:55:03.996: E/AndroidRuntime(30759): at android.os.Looper.loop(Looper.java:137)
11-18 19:55:03.996: E/AndroidRuntime(30759): at android.app.ActivityThread.main(ActivityThread.java:4978)
11-18 19:55:03.996: E/AndroidRuntime(30759): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 19:55:03.996: E/AndroidRuntime(30759): at java.lang.reflect.Method.invoke(Method.java:511)
11-18 19:55:03.996: E/AndroidRuntime(30759): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
11-18 19:55:03.996: E/AndroidRuntime(30759): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
11-18 19:55:03.996: E/AndroidRuntime(30759): at dalvik.system.NativeStart.main(Native Method)
11-18 19:55:05.431: I/Process(30759): Sending signal. PID: 30759 SIG: 9 |
D'après ce que j'ai vu sur le net, ce message (en rouge) veut dire que j'ai mal choisi mon Contexte. Or, si je mets this, il me met une erreur de compilation sous Eclipse. Je ne peux donc pas faire autrement...
Pouvez-vous m'expliquer pourquoi ça ne marche pas? J'ai du mal à comprendre.
Merci d'avance.