Android 仅ListView列表项单击功能不正常

Android 仅ListView列表项单击功能不正常,android,listview,toast,Android,Listview,Toast,我有一个项目列表视图。当我点击一个项目时,我想显示一条简单的toast消息,通知用户点击的项目。但是由于某些原因,下面的代码对我不起作用: 来自当前项目的代码:-不工作 @Override protected void onListItemClick(ListView l, View v, int position, long id) { Toast.makeText(getApplicationContext(), l.getItemAtPosition(position).toStr

我有一个项目列表视图。当我点击一个项目时,我想显示一条简单的toast消息,通知用户点击的项目。但是由于某些原因,下面的代码对我不起作用:

来自当前项目的代码:-不工作

@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();
}
这是非常奇怪的,因为它已经在过去的工作。列表中的每个项目都包含一个ImageView、两个TextView、一个按钮和一个NumberPicker小部件。它们都正常工作,但由于某些原因,onListItemClick()代码没有执行

你知道为什么吗?非常感谢

编辑:我刚刚检查了一个旧项目,我使用了类似的代码,测试了它,它工作得很好,我不明白为什么我会有这个问题,这与视图的复杂性有关吗

以下是以前项目的代码:-工作良好

@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();
}
编辑:适配器类的getView()方法

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;

}
public View getView(最终int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)getContext().getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.布局.菜单项,空);
}
//为视图指定值
最终菜单项=this.menuItems.get(位置);
TextView name视图=(TextView)v.findViewById(R.id.item_name);
最终文本视图价格视图=(文本视图)v.findviewbyd(R.id.item_价格);
nameView.setText(item.getName()+);
priceView.setText(“€”+String.valueOf(item.getPrice());
//数字选择器
np=(NumberPicker)v.findViewById(R.id.numpick);
np.setMaxValue(99);
np.setMinValue(0);
np.setValue(0);
//值更改时会进行计算。。。
np.setOnValueChangedListener(新的OnValueChangeListener(){
public void onValueChange(NumberPicker选择器、int-oldVal、int-newVal){
Toast.makeText(picker.getContext(),“dish:+item.getName()+”amount:+picker.getValue(),Toast.LENGTH_SHORT).show();
Toast.makeText(picker.getContext(),“新值:”+newVal+“旧值:”+oldVal,Toast.LENGTH_SHORT).show();
//amount+=item.getPrice()*picker.getValue();
如果(newVal>oldVal){
总计=(item.getPrice()*newVal)-item.getPrice()*oldVal;
金额+=总额;
}
if(newVal
菜单项\u 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>

尝试使用
按钮和
NumberPicker
getView()中调用
setFocus(false)
试试这个

只需从listRow中删除按钮和NumberPicker,看看它是否工作。如果它起作用,那么这种行为应该是因为视图

在按钮和NumberPicker出现的情况下,您无法单击ListviewItem,这就是您无法获得任何祝酒词的原因

所以,请尝试一下,让我知道


请随时发表评论。

有时,有些东西也需要用户输入(例如您的选择器)会干扰
onListItemClick()
。我想这可能就是问题所在,我能做些什么来解决这个问题吗?或者,当选择器被更改时,我可能不得不添加Toast…您是调用
setContentView()
还是使用默认的ListView?您应该使用
this
getContext()
而不是
getApplicationContext()
…我的类扩展到ListActivity。所以,当然没有必要这样设置一个侦听器?这是一个很好的观点,我现在不在我的电脑前,但我会尽快给它一个机会!