J'ai un ListView d'éléments. Lorsque je clique sur un élément, je veux afficher un simple message toast pour informer l'utilisateur de l'élément cliqué. Cependant, pour une raison quelconque, le code ci-dessous ne fonctionne pas pour moi:ListView onListItemClick ne fonctionne pas correctement
Code du projet actuel: - ne fonctionne pas.
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getApplicationContext(), l.getItemAtPosition(position).toString() + " added to order", Toast.LENGTH_SHORT).show();
}
C'est très étrange car cela a fonctionné dans le passé. Chaque élément de la liste contient un ImageView, deux TextViews, un Button et un widget NumberPicker. Ils fonctionnent tous correctement, mais pour une raison quelconque, le code onListItemClick() n'est pas exécuté.
Des idées sur pourquoi? Merci beaucoup!
EDIT: Je viens de vérifier un ancien projet où j'ai utilisé un code similaire, testé et cela a bien fonctionné, je n'arrive pas à comprendre pourquoi j'ai ce problème, est-ce quelque chose à voir avec la complexité?
Voici le code d'un projet précédent: - Fonctionne correctement.
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getApplicationContext(), l.getItemAtPosition(position).toString() + " added to order", Toast.LENGTH_SHORT).show();
// add clicked item to orderData....
MenuItem m = (MenuItem) l.getItemAtPosition(position);
// create new item
orderData.add(m);
subTotal += m.getPrice();
calc();
}
EDIT: Méthode getView() de la classe Adaptateur
public View getView(final int position, View convertView, ViewGroup parent){
View v = convertView;
if(v == null){
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.menuitem_row, null);
}
//assign values to view
final MenuItem item = this.menuItems.get(position);
TextView nameView = (TextView) v.findViewById(R.id.item_name);
final TextView priceView = (TextView) v.findViewById(R.id.item_price);
nameView.setText(item.getName() + " ");
priceView.setText("€"+ String.valueOf(item.getPrice()));
//number picker
np = (NumberPicker)v.findViewById(R.id.numpick);
np.setMaxValue(99);
np.setMinValue(0);
np.setValue(0);
//calculation occurs when values are changed...
np.setOnValueChangedListener(new OnValueChangeListener() {
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
Toast.makeText(picker.getContext(), "dish: " + item.getName() + " amount: " + picker.getValue(), Toast.LENGTH_SHORT).show();
Toast.makeText(picker.getContext(), "new Value: " + newVal + " old Value: " + oldVal, Toast.LENGTH_SHORT).show();
// amount += item.getPrice() * picker.getValue();
if(newVal > oldVal){
total = (item.getPrice() * newVal) - item.getPrice() * oldVal;
amount += total;
}
if(newVal < oldVal){
total = (item.getPrice() * oldVal) - item.getPrice() * newVal;
amount -= total;
}
price.setText("€" + String.valueOf(amount));
}
});
return v;
}
fichier Menu_item_row.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:orientation="horizontal" >
<NumberPicker
android:id="@+id/numpick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<ImageView
android:id="@+id/dishpic"
android:layout_width="140dp"
android:layout_height="150dp"
android:layout_alignBottom="@+id/numpick"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/reviewBtn"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/item_price"
android:text="ITEM NAME"
android:textSize="30sp" />
<TextView
android:id="@+id/item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/reviewBtn"
android:layout_below="@+id/item_name"
android:layout_marginTop="17dp"
android:layout_toRightOf="@+id/dishpic"
android:text="ITEM PRICE"
android:textSize="40sp" />
<Button
android:id="@+id/reviewBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/dishpic"
android:layout_toLeftOf="@+id/numpick"
android:layout_toRightOf="@+id/dishpic"
android:text="@string/reviewBtn" />
</RelativeLayout>
Parfois, le fait d'avoir quelque chose qui nécessite également une entrée de l'utilisateur (comme votre sélecteur) interfère avec 'onListItemClick()'. –
Je pensais que c'était peut-être le problème. Y a-t-il quelque chose que je puisse faire pour résoudre ce problème? Ou peut-être devrais-je ajouter le Toast lorsque le sélecteur est changé ... – Javacadabra
Appelez-vous 'setContentView()' ou utilisez-vous le ListView par défaut? – Sam