Bonjour, je travail sur un projet scolaire d'une application Android qui permet de visualiser une ListView de tout mes logements. J'ai une méthode InsertLogement() qui ajoute quelques logements et cela fonctionne, en revanche lorsque j'essais d'ajouter des logements depuis un "formulaire" (Edit Text) et y insérer les valeurs en utilisant la même fonction l'application plante. Je me retrouve bloquer avec ce problème et tout ce que j'essais ne fonctionne pas.
Avez-vous des idées vers lesquels je pourrais m'orienter ?
Merci d'avance !
à savoir que ma listview et mon formulaire sont placer dans une TabHost. Tab1 = listview et Tab2 = formulaire
LogementDbAdapter.java
MainActivity.java
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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import android.widget.EditText; public class LogementDbAdapter { public static final String KEY_ROWID = "_id"; public static final String KEY_RUE = "rue"; public static final String KEY_CP = "cp"; public static final String KEY_VILLE = "ville"; public static final String KEY_ETAGE = "etage"; public static final String KEY_SURFACE = "surface"; public static final String KEY_PRIXM = "prixm"; public static final String KEY_DESC = "desc"; private static final String TAG = "LogementsDbAdapter"; private DatabaseHelper mDbHelper; private SQLiteDatabase mDb; private static final String DATABASE_NAME = "E4"; private static final String SQLITE_TABLE = "Logement"; private static final int DATABASE_VERSION = 1; private final Context mCtx; private static final String DATABASE_CREATE = "CREATE TABLE if not exists " + SQLITE_TABLE + " (" + KEY_ROWID + " integer PRIMARY KEY autoincrement," + KEY_RUE + "," + KEY_CP + "," + KEY_VILLE + "," + KEY_ETAGE + "," + KEY_SURFACE + "," + KEY_PRIXM + "," + KEY_DESC + ");"; private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { Log.w(TAG, DATABASE_CREATE); db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE); onCreate(db); } } public LogementDbAdapter(Context ctx) { this.mCtx = ctx; } public LogementDbAdapter open() throws SQLException { mDbHelper = new DatabaseHelper(mCtx); mDb = mDbHelper.getWritableDatabase(); return this; } public void close() { if (mDbHelper != null) { mDbHelper.close(); } } public long createLogement(String rue, Integer cp, String ville, Integer etage, Integer surface, Integer prixm, String desc) { ContentValues initialValues = new ContentValues(); initialValues.put(KEY_RUE, rue); initialValues.put(KEY_CP, cp); initialValues.put(KEY_VILLE, ville); initialValues.put(KEY_ETAGE, etage); initialValues.put(KEY_SURFACE, surface); initialValues.put(KEY_PRIXM, prixm); initialValues.put(KEY_DESC, desc); return mDb.insert(SQLITE_TABLE, null, initialValues); } public boolean deleteAllLogement() { int doneDelete = 0; doneDelete = mDb.delete(SQLITE_TABLE, null , null); Log.w(TAG, Integer.toString(doneDelete)); return doneDelete > 0; } public Cursor fetchAllLogement() { Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID, KEY_RUE, KEY_CP, KEY_VILLE, KEY_ETAGE,KEY_SURFACE, KEY_PRIXM, KEY_DESC}, null, null, null, null, null); if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; } public void insertSomeLogement() { createLogement("RUE DES LICES",72100,"LE MANS",2,25,500,"Très lumineux"); createLogement("RUE PRINCIPAL",49100,"ANGERS",0, 50,600,"Pratique"); createLogement("CHAMPS ELYSEE",75000,"PARIS",5, 125,1000,"Très grand"); } }
Activity_main.xml
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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 import android.app.Activity; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.FilterQueryProvider; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TabHost; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends Activity { private LogementDbAdapter dbHelper; private SimpleCursorAdapter dataAdapter; EditText etrue,etcp,etville,etetage,etsurface,etprixm,etdesc; String srue,sville,sdesc; Integer icp,ietage,isurface,iprixm; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TabHost host = (TabHost)findViewById(R.id.TabHost); host.setup(); //Tab 1 TabHost.TabSpec spec = host.newTabSpec("Tab One"); spec.setContent(R.id.tab1); spec.setIndicator("Tab One"); host.addTab(spec); //Tab 2 spec = host.newTabSpec("Tab Two"); spec.setContent(R.id.tab2); spec.setIndicator("Tab Two"); host.addTab(spec); //Tab 3 spec = host.newTabSpec("Tab Three"); spec.setContent(R.id.tab3); spec.setIndicator("Tab Three"); host.addTab(spec); dbHelper = new LogementDbAdapter(this); dbHelper.open(); //Clean all data dbHelper.deleteAllLogement(); //Add some data dbHelper.insertSomeLogement(); //Generate ListView from SQLite Database displayListView(); } private void displayListView() { Cursor cursor = dbHelper.fetchAllLogement(); // The desired columns to be bound String[] columns = new String[] { LogementDbAdapter.KEY_RUE, LogementDbAdapter.KEY_CP, LogementDbAdapter.KEY_VILLE, LogementDbAdapter.KEY_ETAGE, LogementDbAdapter.KEY_SURFACE, LogementDbAdapter.KEY_PRIXM, LogementDbAdapter.KEY_DESC }; // the XML defined views which the data will be bound to int[] to = new int[] { R.id.rue, R.id.cp, R.id.ville, R.id.etage, R.id.surface, R.id.prixm, R.id.desc, }; // create the adapter using the cursor pointing to the desired data //as well as the layout information dataAdapter = new SimpleCursorAdapter( this, R.layout.logement_info, cursor, columns, to, 0); ListView listView = (ListView) findViewById(R.id.listView1); // Assign adapter to ListView listView.setAdapter(dataAdapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> listView, View view, int position, long id) { // Get the cursor, positioned to the corresponding row in the result set Cursor cursor = (Cursor) listView.getItemAtPosition(position); // Get the state's capital from this row in the database. String LogementCode = cursor.getString(cursor.getColumnIndexOrThrow("rue")); Toast.makeText(getApplicationContext(), LogementCode, Toast.LENGTH_SHORT).show(); } }); } public void insertion(){ EditText etrue = (EditText)findViewById(R.id.editText2); String srue = etrue.getText().toString(); EditText etcp = (EditText)findViewById(R.id.editText3); Integer icp = Integer.parseInt(etcp.getText().toString()); EditText etville = (EditText)findViewById(R.id.editText4); String sville = etville.getText().toString(); EditText etetage = (EditText)findViewById(R.id.editText5); Integer ietage = Integer.parseInt(etetage.getText().toString()); EditText etsurface = (EditText)findViewById(R.id.editText6); Integer isurface = Integer.parseInt(etsurface.getText().toString()); EditText etprixm = (EditText)findViewById(R.id.editText7); Integer iprixm = Integer.parseInt(etprixm.getText().toString()); EditText etdesc = (EditText)findViewById(R.id.editText8); String sdesc = etdesc.getText().toString(); dbHelper.createLogement(srue,icp,sville,ietage,isurface,iprixm,sdesc); } }
Logement_info.xml
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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:weightSum="1"> <TabHost android:id="@+id/TabHost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.07"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.07" /> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="RUE" android:ems="10" > <requestFocus /> </EditText> <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:hint="CP" android:ems="10" > </EditText> <EditText android:id="@+id/editText4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:ems="10" android:hint="VILLE"> </EditText> <EditText android:id="@+id/editText5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:hint="ETAGE" android:ems="10" > </EditText> <EditText android:id="@+id/editText6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:hint="SURFACE" android:ems="10" > </EditText> <EditText android:id="@+id/editText7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:hint="PRIX M" android:ems="10" > </EditText> <EditText android:id="@+id/editText8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_below="@+id/editText1" android:hint="DESCRIPTION" android:ems="10" > </EditText> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_alignRight="@+id/editText2" android:layout_below="@+id/editText2" android:onClick="insertion" android:text="Enregistrer" /> </LinearLayout> <LinearLayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>
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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="6dip" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="RUE :" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/rue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView1" android:layout_alignBottom="@+id/textView1" android:layout_toRightOf="@+id/textView1" android:text="TextView" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:text="CP :" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/cp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView2" android:layout_alignBottom="@+id/textView2" android:layout_toRightOf="@+id/textView2" android:text="TextView" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView2" android:layout_below="@+id/textView2" android:text="VILLE :" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/ville" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView3" android:layout_alignBottom="@+id/textView3" android:layout_toRightOf="@+id/textView3" android:text="TextView" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView3" android:layout_below="@+id/textView3" android:text="ETAGE : " android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/etage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView4" android:layout_alignBottom="@+id/textView4" android:layout_toRightOf="@+id/textView4" android:text="TextView" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView4" android:layout_below="@+id/textView4" android:text="SURFACE : " android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/surface" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView5" android:layout_alignBottom="@+id/textView5" android:layout_toRightOf="@+id/textView5" android:text="TextView" /> <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView5" android:layout_below="@+id/textView5" android:text="PRIX M :" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/prixm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView6" android:layout_alignBottom="@+id/textView6" android:layout_toRightOf="@+id/textView6" android:text="TextView" /> <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView6" android:layout_below="@+id/textView6" android:text="DESC : " android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView7" android:layout_alignBottom="@+id/textView7" android:layout_toRightOf="@+id/textView7" android:text="TextView" /> </RelativeLayout>
Partager