Bonjour a tous , je sollicite votre aide ; je dois enregistrer dans la base des données sqlite depuis une liste déroulante récupérer dans une autre table; dans la table ou je souhaite enregistrer j'ai 3 colonnes ; numS, numP, EtatCP ; voici la la creation de la tale :
2. la liste récupérer ;affichée par un spinner (marche) ;
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 private static final String PLOT_CREATE = "CREATE TABLE " + TABLE_PLOT + " (" + COLUMN_PLOT_NUMS + " VARCHAR ( 20 ) , " + COLUMN_PLOT_NUMP + " INTEGER ," + COLUMN_PLOT_ETATCP + " VARCHAR ( 1 ) , " + "primary key (NUMS, NUMP)," + "FOREIGN KEY(NUMS) REFERENCES STATION(NUMS) " + " ); ";
3.Methode pour enregister :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 final Spinner spinnerStations = (Spinner) findViewById(R.id.spinnerForStations); ArrayAdapter<String> spinnerStationsAdapter = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item); for(int i = 0; i< listeStations.size(); i++){ spinnerStationsAdapter.add(listeStations.get(i).getNomS()); } spinnerStations.setAdapter(spinnerStationsAdapter);
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 public boolean addPlot(int laStation, String etatCP) { // Ajout d'un enregistrement dans la table ContentValues values = new ContentValues(); values.put(DatabaseOpenHelper.COLUMN_PLOT_NUMS, laStation); values.put(DatabaseOpenHelper.COLUMN_PLOT_ETATCP, etatCP); long result = db.insert(DatabaseOpenHelper.TABLE_PLOT, null, values); if (result == -1) { return false; } else return true; }
4.Lors de l'enregistrement j'appelle ma methode comme ceci :
le probleme vient de la
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 boolean isInserted = plotsAcces.addPlot(Integer.parseInt(spinnerStations.getSelectedItem().toString()), radioChoiceButton.getText().toString()); if (isInserted) Toast.makeText(AjoutPlotActivity.this, "AJOUTE", Toast.LENGTH_LONG).show(); else Toast.makeText(AjoutPlotActivity.this," NON AJOUTE", Toast.LENGTH_LONG).show(); }car je tente d'enregistrer un type string récupéré dans la liste deroulante (nom) alors que je dois enregistrer le numero (numS) correspondant au nom selectionné dans le champ NUMS de la table en haut.Integer.parseInt(spinnerStations.getSelectedItem().toString()
j'ai cette erreur lors de l'enregistrement :
comment faire svp ? Merci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 E/AndroidRuntime: FATAL EXCEPTION: main Process: ppe4.velib, PID: 11682 java.lang.NumberFormatException: Invalid int: "TestSave" at java.lang.Integer.invalidInt(Integer.java:138) at java.lang.Integer.parse(Integer.java:410) at java.lang.Integer.parseInt(Integer.java:367) at java.lang.Integer.parseInt(Integer.java:334) at ppe4.velib.Activities.AjoutPlotActivity$1.onClick(AjoutPlotActivity.java:84) at android.view.View.performClick(View.java:5721) at android.widget.TextView.performClick(TextView.java:10930) at android.view.View$PerformClick.run(View.java:22620) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7331) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Partager