Aller au contenu

listeview + base de donnée Sqlite


nathalie85

Recommended Posts

Posté(e)

Bonjour à tous,

voila je débute dans la programmation android, et j'ai un problème que je n'arrive pas à résoudre.

j'aimerai afficher une listeview à partir de ma base de donnée, mais ça ne marche pas.

j'ai une classe DBAdapter dans laquelle je crée ma table et j'utilise un Cursor pour récupérer les éléments de la table.

et dans ma classe main je fais ça :

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        bd_transfert = new DBAdapter(this);
        bd_transfert.open();
        DataBind();

    }

public void DataBind(){
Cursor c = bd_transfert.recupererLalisteDesPersonne();
    	startManagingCursor( c );
    	SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
    	R.layout.listitem,c,new String[]{"nom","adresse","code_postal", "ville"},
    	new int[]{R.id.txtNom,R.id.txtAdresse,R.id.txtCodePostal,R.id.txtVille});

Dans le logcat j'ai des erreurs par rapport à la fonction onCreate(), par rapport à DataBind() et aussi par rapport au SimpleCursorAdapter.

voila les erreurs dans le logcat

01-07 10:54:07.230: DEBUG/AndroidRuntime(9975): Shutting down VM
01-07 10:54:07.230: WARN/dalvikvm(9975): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975): FATAL EXCEPTION: main
01-07 10:54:07.348: ERROR/AndroidRuntime(9975): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.projet.transfert/com.projet.transfert.Main}: java.lang.IllegalArgumentException: column '_id' does not exist
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at android.os.Looper.loop(Looper.java:123)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at java.lang.reflect.Method.invokeNative(Native Method)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at java.lang.reflect.Method.invoke(Method.java:521)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at dalvik.system.NativeStart.main(Native Method)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at android.widget.CursorAdapter.init(CursorAdapter.java:111)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at android.widget.CursorAdapter.<init>(CursorAdapter.java:90)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:47)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:84)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at com.projet.transfert.Main.DataBind(Main.java:52)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at com.projet.transfert.Main.onCreate(Main.java:43)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-07 10:54:07.348: ERROR/AndroidRuntime(9975):     ... 11 more

Je vous remercie d'avance.

Posté(e)

Mauvaise section.

Je déplace.

Ps : pour aider à t'aider :

  • Utilise la balise "code" pour que ca soit mieux mis en forme
  • Donne nos toute la trace du Logcat

Posté(e)

Comme l'indique le logcat, il te manque une colonne _id (qui peut être un entier autoincrémentable) dans ta table. Cette colonne est nécessaire pour que le CursorAdapter fonctionne

cf doc

Posté(e)

merci chpil pour ta réponse.

voila le code de la création de ma table il y a bien un id:

public void onCreate(SQLiteDatabase bd_transfert) {

		bd_transfert.execSQL("create table personne ("
				+ "id_personne integer primary key autoincrement, "
				+ "nom varchar(30) not null, "
				+ "adresse varchar (50), "
				+ "code_postal varchar (30), "
				+ "ville varchar (30), "
				+ "telephonevarchar (30), "
				+ "mail varchar(50), "
				+ "fax varchar(30)"
				+");");

	}

Posté(e)

CursorAdapter pose la contrainte que la colonne identifiant doit être nommée '_id'. La tienne est nommée 'id_personne', cela ne peut marcher.

Posté(e)

d'accord donc je la nomme juste _id, est ce que je dois alors changer le Cursor qui est actuellement:

public Cursor recupererLalisteDesPersonne(){
	return bd_transfert.query("personne", new String[]{
			"nom",
			"adresse",
			"code_postal",
			"ville"}, null, null, null, null, null);
}

et l'écrire :

public Cursor recupererLalisteDesPersonne(){
	return bd_transfert.query("personne", new String[]{
			"_id",
			"nom",
			"adresse",
			"code_postal",
			"ville"}, null, null, null, null, null);
}
}

Posté(e)

Oui, il faut que _id soit dans la réponse du cursor pour que le CursorAdapter puisse le trouver et l'utiliser

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×
×
  • Créer...