Bonjour,

Je cherche tout simplement à améliorer mon code, étant donné que j'ai l'impression d'avoir fait beaucoup de bricolages. Le but de cette class est de crée un AlertDialog dynamique. Ce qui est, à mes yeux, pas chose très facile.

J'ai une liste de contacts, avec nom, prenom, telephone....quand je click sur un contact j'ai un premier AlertDialog qui m'affiche 3 options : "Afficher le contact", "Modifier", "Supprimer", quand je clique sur "Afficher le contact", j'ai un second AlertDialog qui s'affiche, mais uniquement avec les informations présentent dans la base de données. Par exemple, si j'ai le numero de portable mais pas l'adresse email, j'ai un menu qui ne contient que 1 item. Selon l'item, je vais pouvoir lancer un "intent" pour soit téléphoner, soit envoyer un mail.

J'ai réussi mais j'aimerai pouvoir améliorer le code :

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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
 
import...
 
public class AdresseActivity extends Activity {
 
	private AdresseAdapter adapter;
	private MonMariageDAO dao;
 
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.adresse_layout);
 
 
        dao = MonMariageDAO.getInstance(this);
 
        ListView listView = (ListView) findViewById(R.id.liste_adresse);
		adapter = new AdresseAdapter(this, R.layout.adresse_listrow, dao.selectCursorAdresse());
		listView.setAdapter(adapter);
 
		listView.setOnItemClickListener(onListClick);
    }
 
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(0, 1, 0, "Ajouter un contact");
    	return super.onCreateOptionsMenu(menu);
    }
 
	 @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	    	switch(item.getItemId()){
	    	case 1:
	    		Intent intent = new Intent(AdresseActivity.this,
			    AdresseAjoutActivity.class);
	    		startActivity(intent);
	    		break;
	    	}
	    	return super.onOptionsItemSelected(item);
	    }
 
	 public void choixList(int num, Cursor cursor, CharSequence[] items2){
		 String Newligne=System.getProperty("line.separator");
		 if(items2[num].equals("Appeler : "+cursor.getString(4))){
			 Intent intent = new Intent(Intent.ACTION_CALL, 
			 Uri.parse("tel:"+cursor.getString(4)));
			 startActivity(intent);
 		 }
 		 else{
 			if(items2[num].equals("Message : "+cursor.getString(4))){
 				Intent intent = new Intent(Intent.ACTION_VIEW);
 				intent.putExtra("address", cursor.getString(4));
 				intent.setType("vnd.android-dir/mms-sms"); 
 				startActivity(intent);
 			}
 			else{
 				if(items2[num].equals("Appeler : "+cursor.getString(5))){
 					 Intent intent = new Intent(Intent.ACTION_CALL, 
 					 Uri.parse("tel:"+cursor.getString(5)));
 					 startActivity(intent);
 		 		 }
 				else{
 					if(items2[num].equals("Message : "+cursor.getString(5))){
 		 				Intent intent = new Intent(Intent.ACTION_VIEW);
 		 				intent.putExtra("address", cursor.getString(5));
 		 				intent.setType("vnd.android-dir/mms-sms"); 
 		 				startActivity(intent);
 		 			}
 		 			else{
 		 				if(items2[num].equals("Email : "+cursor.getString(6))){
 		 					Intent intent = new Intent(Intent.ACTION_SEND);
 		 					intent.setType("message/rfc822");
 		 					intent.putExtra(Intent.EXTRA_EMAIL, new String[]{cursor.getString(6)});
 		 					startActivity(Intent.createChooser(intent, "Titre:"));
 		 		 		 }
 		 				else{
 		 					if(items2[num].equals("Adresse : "+cursor.getString(7)+Newligne+cursor.getString(8)+" "+cursor.getString(9))){
 
 	 		 		 		 }
 	 		 				else{
 	 		 					if(items2[num].equals("SiteWeb : "+cursor.getString(10))){
 	 		 						Intent intent = new Intent(Intent.ACTION_VIEW);  
 	 		 					    intent.setData(Uri.parse("http://"+cursor.getString(10)));  
 	 		 					    startActivity(intent);
 	 	 		 				}
 	 		 				}
 		 				}
 		 			}
 				}
 			}
 		 }
	 }
 
	 OnItemClickListener onListClick = new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> listView, View rowView,
					int position, long id) {
 
				final Cursor cursor = adapter.getCursor();
				cursor.moveToPosition(position);
 
				int nbMenuAffichage = 0;
				ArrayList<String> infoMenuProv = new ArrayList<String>();
				String Newligne=System.getProperty("line.separator"); 
 
				if(cursor.getString(4).trim().length()>0){
					nbMenuAffichage++;
					infoMenuProv.add("Appeler : "+cursor.getString(4));
				}
 
				if(cursor.getString(4).trim().length()>0){
					nbMenuAffichage++;
					infoMenuProv.add("Message : "+cursor.getString(4));
				}
 
				if(cursor.getString(5).trim().length()>0){
					nbMenuAffichage++;
					infoMenuProv.add("Appeler : "+cursor.getString(5));
				}
 
				if(cursor.getString(5).trim().length()>0){
					nbMenuAffichage++;
					infoMenuProv.add("Message : "+cursor.getString(5));
				}
 
				if(cursor.getString(6).trim().length()>0){
					nbMenuAffichage++;
					infoMenuProv.add("Email : "+cursor.getString(6));
				}
 
				if(cursor.getString(7).trim().length()>0){
					nbMenuAffichage++;
					infoMenuProv.add("Adresse : "+cursor.getString(7)+Newligne+cursor.getString(8)+" "+cursor.getString(9));
				}
 
				if(cursor.getString(10).trim().length()>0){
					nbMenuAffichage++;
					infoMenuProv.add("SiteWeb : "+cursor.getString(10));
				}
 
				final CharSequence[] items = {"Afficher Le contact", "Modifier", "Supprimer"};
				final CharSequence[] items2 = new CharSequence[nbMenuAffichage];
 
				for(int i=0; i<infoMenuProv.size(); i++){
					items2[i] = infoMenuProv.get(i);
				}
 
				AlertDialog.Builder builder = new AlertDialog.Builder(AdresseActivity.this);
				builder.setTitle("Faite votre choix :");
 
				builder.setItems(items, new DialogInterface.OnClickListener() {
				    public void onClick(DialogInterface dialog, int item) {
				    	switch(item){
				    	case 0:
				    		AlertDialog.Builder builder = new AlertDialog.Builder(AdresseActivity.this);
							builder.setTitle(cursor.getString(1)+" "+cursor.getString(2));
 
							builder.setItems(items2, new DialogInterface.OnClickListener() {
							    public void onClick(DialogInterface dialog, int item) {
							    	switch(item){
							    	case 0:
							    		choixList(0, cursor, items2);
							    		break;
							    	case 1:
							    		choixList(1, cursor, items2);
							    		break;
							    	case 2:
							    		choixList(2, cursor, items2);
							    		break;
							    	case 3:
							    		choixList(3, cursor, items2);
										break;
							    	case 4:
							    		choixList(4, cursor, items2);
										break;
							    	case 5:
							    		choixList(5, cursor, items2);
										break;
							    	case 6:
							    		choixList(6, cursor, items2);
										break;
							    	case 7:
							    		choixList(7, cursor, items2);
										break;
							    	case 8:
							    		choixList(8, cursor, items2);
										break;
							    	}
							    }
							});
							AlertDialog alert = builder.create();				
							alert.show();
				    		break;
				    	case 1:
				    		int id2 = cursor.getInt(0);
				    		Intent intent2 = new Intent(AdresseActivity.this,
				    		AdresseModifActivity.class);
				    		intent2.putExtra("id_adresse", id2);
				    	    startActivity(intent2);
				    		break;
				    	case 2:
				    		dao.deleteAdresse(cursor);
				    		adapter.changeCursor(dao.selectCursorAdresse());
							adapter.notifyDataSetChanged();
							Toast.makeText(AdresseActivity.this, "Le contact à était supprimé !", 1000).show();
				    	}
				    }
				});
				AlertDialog alert = builder.create();				
				alert.show();
			}
		};
}
Merci à vous.
Cordialement