Reputation: 5758
I have a ListView of items. When I click on an item I want to display a simple toast message to inform user of the item clicked. However for some reason the code below is not working for me:
Code from current project: - not working.
@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();
}
It's very strange as it has worked in the past. Each item in the list contains an ImageView, two TextViews, a Button and one NumberPicker widget. They all function correctly but for some reason the onListItemClick() code is not executed.
Any ideas as to why? Many thanks!
EDIT: I just checked over an older project whereby I used similar code, tested it and it worked fine, I cant figure out why I am having this issue, is it something to do with the complexity of the view??
Below is code from a previous project: - Worked fine.
@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: getView() method of Adapter class
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;
}
Menu_item_row.xml file:
<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>
Upvotes: 0
Views: 209
Reputation: 23596
Try with this.
Just remove the Button and NumberPicker from the listRow and see whether its working or not. If it is working then that behaviour should be because of the View.
In presence of the Button and NumberPicker, you are not able to click on the ListviewItem that's why you are not getting any toast.
So, Please try that and let me know.
Feel free for any comments.
Upvotes: 0