Bonjour,

J'ai un souci avec les menus contextuels (ContextMenu) qui m'empêmche
d'avancer sur mon projet.

J'ai configuré un contextmenu pour une activity faisant intervenir une vue
spécifique. Je veux implémenter le même contextmenu pour une autre activity
avec une autre vue.

En faisant un copier/coller, dans la nouvelle vue et en réadaptant le code
à cette nouvelle vue, le contextmenu ne s'affiche pas pour la seconde.

Est-ce normal ?

Voici mon code :
Pret.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
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
 
......
 
public class Pret extends Activity implements OnItemClickListener{
 
	private static final int NOTIFICATION_ID = 0;
	private SQLiteAdapter mySQLiteAdapter;
	SimpleCursorAdapter cursorAdapter;
	Cursor cursor;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listeprets);
		//registerForContextMenu(getListView());
 
		ListView ListePret = (ListView) findViewById(R.id.list);
		registerForContextMenu(ListePret);
		//ListePret.setBackgroundColor((position & 1) == 1 ? Color.WHITE : Color.LTGRAY);
		//Instanciation de la base MySQL
		mySQLiteAdapter = new SQLiteAdapter(this);
		mySQLiteAdapter.openToWrite();
 
		//Affichage des données en ligne
		cursor = mySQLiteAdapter.ListerPret();
		String total = mySQLiteAdapter.Somme(cursor);
		TextView Ttotal = (TextView)findViewById(R.id.textViewTotal);
		Ttotal.setText(total);
		    //updateList();
		String[] strtotal = new String[]{total};
		int[] txttot = new int[]{R.id.textViewTotal};
		String[] from = new String[]{SQLiteAdapter.KEY_IDPRET, SQLiteAdapter.EMPRUNTEUR, SQLiteAdapter.DATEPRET, SQLiteAdapter.SOMMEPRETEE, SQLiteAdapter.ETATPRET };
 
		if(SQLiteAdapter.ETATPRET == "Non soldé"){
			int[] to = new int[]{R.id.TextViewidPret, R.id.textViewNomEmprunteur, R.id.TextViewDatePret, R.id.TextViewSommePret, R.id.textViewEtatPret};
			cursorAdapter = new SimpleCursorAdapter(this, R.layout.rowpret, cursor, from, to);
		}
		else{
			int[] tos = new int[]{R.id.TextViewidEmprunts, R.id.textViewNomPreteurs, R.id.TextViewDateEmprunts, R.id.TextViewSommeEmprunts, R.id.textViewEtatEmprunts};
			cursorAdapter = new SimpleCursorAdapter(this, R.layout.rowpret_s, cursor, from, tos);
		}
		ListePret.setAdapter(cursorAdapter);
		ListePret.setClickable(true);
	}
 
	// Création du menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
 
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	    super.onCreateContextMenu(menu, v, menuInfo);
	    menu.setHeaderTitle(getString(R.string.LineAction));
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.contextmenu, menu);
	    SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.ArrayTypeNav, android.R.layout.simple_spinner_dropdown_item);
	    mOnNavigationListener = new OnNavigationListener() {
	    	  // Get the same strings provided for the drop-down's ArrayAdapter
	    	  String[] strings = getResources().getStringArray(R.array.action_list);
 
	    	  @Override
	    	  public boolean onNavigationItemSelected(int position, long itemId) {
	    	    // Create new fragment from our own Fragment class
	    	    ListContentFragment newFragment = new ListContentFragment();
	    	    FragmentTransaction ft = openFragmentTransaction();
	    	    // Replace whatever is in the fragment container with this fragment
	    	    //  and give the fragment a tag name equal to the string at the position selected
	    	    ft.replace(R.id.fragment_container, newFragment, strings[position]);
	    	    // Apply changes
	    	    ft.commit();
	    	    return true;
	    	  }
	    	};
	    /* ActionBar actionBar = getActionBar();
	    actionBar.setDisplayHomeAsUpEnabled(true);*/
	}
	// Actions à effectuer quand l'utilisateur clique sur une icone
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				Intent IntentStart = new Intent(getApplicationContext(), MainActivity.class);
			    startActivity(IntentStart);
			    //onBackPressed();
			    break;
		    case R.id.menu_new:
			    Toast.makeText(this, "Ajout", Toast.LENGTH_SHORT).show();
			    Intent IntentPret = new Intent(getApplicationContext(), AjouterPret.class);
			    startActivity(IntentPret);
			    break;
 
		    case R.id.menu_exit:
		    	Toast.makeText(this, "Deconnexion", Toast.LENGTH_SHORT).show();
		    	finish();
		    	break;
 
		    case R.id.menu_home:
		    	Intent Intenthome = new Intent(getApplicationContext(), MainActivity.class);
			    startActivity(Intenthome);
			    break;
 
		    default:
		    	break;
	    }
 
	    return true;
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
	    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	    long row_id = cursorAdapter.getItemId(info.position);
 
	    switch (item.getItemId()) {
		    case R.id.solder:    
		    	mySQLiteAdapter.openToWrite();
		        mySQLiteAdapter.solderPret(row_id);
		        updateList();
		        mySQLiteAdapter.close();
		        return true;
		    case R.id.notifier:		
		    	mySQLiteAdapter.openToWrite();
		    	String msg = mySQLiteAdapter.recupPretById(String.valueOf(row_id));
			    Notification("Rappel de dette",msg);
			    mySQLiteAdapter.close();
		        return true;
		    case R.id.supprimer:
		    	mySQLiteAdapter.openToWrite();
		    	mySQLiteAdapter.deleteByIdPret(row_id);
		    	updateList();
		    	mySQLiteAdapter.close();
		    default:
		        return super.onContextItemSelected(item);
	    }
	}
	private void updateList(){
		cursor.requery();
		String total = mySQLiteAdapter.Somme(cursor);
		TextView Ttotal = (TextView)findViewById(R.id.textViewTotal);
		Ttotal.setText(total);
	}
 
	private final void Notification(String titre, String message){ 
		//Récupération du notification Manager 
	    final NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
 
	    //Création de la notification avec spécification de l'icône de la notification et le texte qui apparait à la création de la notification 
	    final Notification notification = new Notification(R.drawable.ic_launcher, titre, System.currentTimeMillis()); 
 
	    //Définition de la redirection au moment du clic sur la notification. Dans notre cas la notification redirige vers notre application 
	    final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); 
 
	    //Récupération du titre et description de la notification 
	    //final String notificationTitle = getResources().getString(R.string.); 
	    //final String notificationDesc = getResources().getString(R.string.notification_desc);        
 
	    //Notification & Vibration 
	    notification.setLatestEventInfo(this, titre, message, pendingIntent); 
	    //notification.vibrate = new long[] {0,200,100,200,100,200}; 
 
	    notificationManager.notify(NOTIFICATION_ID, notification); 
	   }
 
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
 
	}
}
Emprunt.java (2ème vue/activity)
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
 
public class Emprunt extends Activity {
 
	private static final int NOTIFICATION_ID = 0;
	private SQLiteAdapter mySQLiteAdapter;
	SimpleCursorAdapter cursorAdapter;
	Cursor cursor;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listeprets);
		ListView ListeEmprunt = (ListView) findViewById(R.id.list);
 
		//Instanciation de la base MySQL
		mySQLiteAdapter = new SQLiteAdapter(this);
		mySQLiteAdapter.openToWrite();
 
		//Affichage des données en ligne
		cursor = mySQLiteAdapter.ListerEmprunt();
		String total = mySQLiteAdapter.Somme(cursor);
		TextView Ttotal = (TextView)findViewById(R.id.textViewTotal);
		Ttotal.setText(total);
		    //updateList();
		/*String[] strtotal = new String[]{total};
		int[] txttot = new int[]{R.id.TextViewtotalemprunt};*/
		    //updateList();
		String[] from = new String[]{SQLiteAdapter.KEY_IDEMPRUNT, SQLiteAdapter.PRETEUR, SQLiteAdapter.DATEEMPRUNT, SQLiteAdapter.SOMMEEMPRUNTEE, SQLiteAdapter.ETATEMPRUNT };
		int[] to = new int[]{R.id.TextViewidEmprunts, R.id.textViewNomPreteurs, R.id.TextViewDateEmprunts, R.id.TextViewSommeEmprunts, R.id.textViewEtatEmprunts};
 
		cursorAdapter = new SimpleCursorAdapter(this, R.layout.rowemprunt, cursor, from, to);
		ListeEmprunt.setAdapter(cursorAdapter);
		ListeEmprunt.setClickable(true);
		//mySQLiteAdapter.close();       
		}
 
	// Création du menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
 
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	    super.onCreateContextMenu(menu, v, menuInfo);
	    menu.setHeaderTitle(getString(R.string.LineAction));
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.contextmenu, menu);
	}
 
	// Actions à effectuer quand l'utilisateur clique sur une icone
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				Intent IntentStart = new Intent(getApplicationContext(), MainActivity.class);
			    startActivity(IntentStart);
			    //onBackPressed();
			    break;
		    case R.id.menu_new:
			    Toast.makeText(this, "Ajout", Toast.LENGTH_SHORT).show();
			    Intent IntentPret = new Intent(getApplicationContext(), AjouterEmprunt.class);
			    startActivity(IntentPret);
			    break;
 
		    case R.id.menu_exit:
		    	Toast.makeText(this, "Deconnexion", Toast.LENGTH_SHORT).show();
		    	finish();
		    	break;
 
		    case R.id.menu_home:
		    	Intent Intenthome = new Intent(getApplicationContext(), MainActivity.class);
			    startActivity(Intenthome);
			    break;
 
		    default:
		    	break;
	    }
 
	    return true;
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
	    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	    long row_id = cursorAdapter.getItemId(info.position);
 
	    switch (item.getItemId()) {
		    case R.id.solder:    
		    	mySQLiteAdapter.openToWrite();
		        mySQLiteAdapter.solderEmprunt(row_id);
		        updateList();
		        mySQLiteAdapter.close();
		        return true;
		    case R.id.notifier:		
		    	mySQLiteAdapter.openToWrite();
		    	String msg = mySQLiteAdapter.recupEmpruntById(String.valueOf(row_id));
			    Notification("Rappel de dette",msg);
			    mySQLiteAdapter.close();
		        return true;
		    case R.id.supprimer:
		    	mySQLiteAdapter.openToWrite();
		    	mySQLiteAdapter.deleteByIdEmprunt(row_id);
		    	updateList();
		    	mySQLiteAdapter.close();
		    default:
		        return super.onContextItemSelected(item);
	    }
	}
	private void updateList(){
		cursor.requery();
		String total = mySQLiteAdapter.Somme(cursor);
		TextView Ttotal = (TextView)findViewById(R.id.TextVieweur);
		Ttotal.setText(total);
	}
 
	private final void Notification(String titre, String message){ 
		//Récupération du notification Manager 
	    final NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
 
	    //Création de la notification avec spécification de l'icône de la notification et le texte qui apparait à la création de la notification 
	    final Notification notification = new Notification(R.drawable.ic_launcher, titre, System.currentTimeMillis()); 
 
	    //Définition de la redirection au moment du clic sur la notification. Dans notre cas la notification redirige vers notre application 
	    final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); 
 
	    //Récupération du titre et description de la notification 
	    //final String notificationTitle = getResources().getString(R.string.); 
	    //final String notificationDesc = getResources().getString(R.string.notification_desc);        
 
	    //Notification & Vibration 
	    notification.setLatestEventInfo(this, titre, message, pendingIntent); 
	    //notification.vibrate = new long[] {0,200,100,200,100,200}; 
 
	    notificationManager.notify(NOTIFICATION_ID, notification); 
	   }
 
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
 
	}
}
Merci pour vos réponses.