Android 无法在膨胀布局中实现onclicklistener

Android 无法在膨胀布局中实现onclicklistener,android,android-layout,Android,Android Layout,我对安卓系统还很陌生。我正在做一个购物车应用程序。在我的购物车页面中,我为每个项目显示产品图像、名称、id、数量、价格、总计和删除按钮 我在item.xml中为上述视图使用了表布局,并在cart.xml中的另一个表布局中对其进行了扩展 问题是,当我将onclick listener添加到remove按钮时,它只对第一行有效,而不是对应的行 我这样做对吗?请帮帮我。提前谢谢 //item.xml <TableLayout android:id="@+id/Item" android:lay

我对安卓系统还很陌生。我正在做一个购物车应用程序。在我的购物车页面中,我为每个项目显示产品图像、名称、id、数量、价格、总计和删除按钮

我在item.xml中为上述视图使用了表布局,并在cart.xml中的另一个表布局中对其进行了扩展

问题是,当我将onclick listener添加到remove按钮时,它只对第一行有效,而不是对应的行

我这样做对吗?请帮帮我。提前谢谢

//item.xml

<TableLayout
android:id="@+id/Item"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
>  
<TableRow
android:id="@+id/tr"
android:layout_width="fill_parent"
android:layout_height="97px"
android:padding="2dip"
android:orientation="horizontal"
>
<LinearLayout
android:id="@+id/linear1"
android:layout_width="75px"
android:layout_height="95px"
android:orientation="vertical"
>
<ImageView
android:id="@+id/product"
android:layout_width="60px"
android:layout_height="60px"

>
</ImageView>
<TextView
android:id="@+id/pname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginLeft="5px"
android:textColor="#000000"
>
</TextView>
<TextView
android:id="@+id/pid"
android:layout_width="wrap_content"
android:layout_height="16px"
android:textSize="12sp"
android:layout_marginLeft="5px"
android:textColor="#000000"
>
</TextView>
</LinearLayout>

<LinearLayout
android:id="@+id/linear2"
android:layout_width="80px"
android:layout_height="74px"
android:orientation="vertical"
>
<TextView
android:id="@+id/qty_t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="11px"
android:layout_marginLeft="20px"
android:text="Qty: "
android:textSize="12sp"
android:layout_gravity="right"
android:textColor="#000000"
>

</TextView>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price: "
android:textSize="12sp"
android:layout_gravity="right"
android:textColor="#000000"
>
</TextView>
<TextView
android:id="@+id/itemtot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item Total: "
android:textSize="12sp"
android:layout_gravity="right"
android:textColor="#000000"
>
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linear3"
android:layout_width="95px"
android:layout_height="110px"
android:orientation="vertical"
>
<EditText
android:id="@+id/qty"
android:numeric="integer" 
android:layout_width="55px"
android:layout_height="29px"
 android:textSize="10sp">
</EditText>
<TextView
android:id="@+id/price_amt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:textSize="12sp"
android:textColor="#000000"
>
</TextView>
<TextView
android:id="@+id/item_amt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:textSize="12sp"
android:textColor="#000000"
>
</TextView>
<CheckBox
android:id="@+id/gift_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Gift Options"
android:textSize="12sp"
android:textColor="#000000"

>
</CheckBox>
</LinearLayout>
<LinearLayout
android:id="@+id/linear4"
android:layout_width="68px"
android:layout_height="69px"
android:orientation="vertical"
android:layout_gravity="right"

>
<ImageView
android:id="@+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10px"
android:src="@drawable/x"
>

</ImageView>
</LinearLayout>

</TableRow>
<View
        android:layout_height="3dip"
        android:background="#FF909090" />
</TableLayout>


//cart.xml
<ScrollView
android:id="@+id/widget129"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:id="@+id/widget120"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TableLayout
android:id="@+id/cart_item_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</TableLayout>
</LinearLayout>
</ScrollView>
//item.xml
//cart.xml
我的java代码是:

public class cart3 extends Activity  {

    /** Called when the activity is first created. */
    String[] product = new String[]{
            "Ipod 1",
            "ipod 2",
            "ipod 3",
            "ipod 4",
            "ipod 5"
                    };
    String[] pid = new String[]{
            "#1000",
            "#1001",
            "#1002",
            "#1003",
            "#1004"
                    };
   Integer[] pimg = {
            R.drawable.sample_2, R.drawable.sample_1,
            R.drawable.i2, R.drawable.i1,
            R.drawable.i3, R.drawable.i4,
    };
   String[] Qty =  {
           "1","2",
           "1","3",
           "1","2",
    };
   String[] price =  {
           "5000","7500",
           "8000","5500",
           "7200","5000",
    };



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cart);
        addCartContent();

    }
**//here is the problem**
    private OnClickListener delimg = new OnClickListener() { 


        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            TextView tv=(TextView) findViewById(R.id.pname);
            tv.setText("Deleted Item!");    

        } 
    }; 

    public void addCartContent()
    {

    int m_amt=0;
    View itemView;
    // "cart_item_table" is the id of the item table in cart.xml  
    TableLayout tl = (TableLayout)findViewById(R.id.cart_item_table);
    // we will use service from inflater   
     LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);  
    //doing a loop to add many times the cart xml  
          for(int i = 0; i < product.length; i++) { 
             // "item" is the name of the xml file "item.xml".   
            // Create the itemView who will be added.  
           // itemView = cart.xml  

           itemView = inflater.inflate(R.layout.item,null);
           //get the image id and set it to image in array pimg
           ImageView im = (ImageView) itemView.findViewById(R.id.product);
           im.setImageDrawable(getResources().getDrawable(pimg[i]));
          // get the product name, and set the text to element from array  
            TextView t1 = (TextView) itemView.findViewById(R.id.pname);   
            t1.setText(product[i]);  
         // get the product id, and set the text to element from array  
             TextView t2 = (TextView) itemView.findViewById(R.id.pid);   
             t2.setText(pid[i]);  
          // get the product qty, and set the text to element from array  
              EditText t3 = (EditText) itemView.findViewById(R.id.qty);   
              t3.setText(Qty[i]); 
          // get the product price, and set the text to element from array  
               TextView t4 = (TextView) itemView.findViewById(R.id.price_amt);   
               t4.setText("Rs."+price[i]+".00");
         // get the item total, and set the text to product of price and qty  
               TextView t5 = (TextView) itemView.findViewById(R.id.item_amt);
               int pr = Integer.parseInt(price[i].trim());
               int quan=Integer.parseInt(Qty[i].trim());
               int item_amt=pr*quan;
                String i_amt = Integer.toString(item_amt);
              //Calculate the merchandise total by adding all item_amt

                m_amt=m_amt+item_amt;
                t5.setText("Rs."+i_amt+".00"); 
                ImageView x= (ImageView)itemView.findViewById(R.id.remove);
                x.setOnClickListener(delimg);

                //add the itemView  
                tl.addView(itemView); 

                  }//end of for loop
    }//end of addCartContent()
公共类cart3扩展活动{
/**在首次创建活动时调用*/
字符串[]产品=新字符串[]{
“Ipod 1”,
“ipod 2”,
“ipod 3”,
“ipod 4”,
“ipod 5”
};
字符串[]pid=新字符串[]{
"#1000",
"#1001",
"#1002",
"#1003",
"#1004"
};
整数[]pimg={
R.drawable.sample_2,R.drawable.sample_1,
R.drawable.i2,R.drawable.i1,
R.drawable.i3,R.drawable.i4,
};
字符串[]数量={
"1","2",
"1","3",
"1","2",
};
字符串[]价格={
"5000","7500",
"8000","5500",
"7200","5000",
};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.cart);
addCartContent();
}
**//问题就在这里**
private OnClickListener delimg=new OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
TextView tv=(TextView)findViewById(R.id.pname);
tv.setText(“已删除项!”);
} 
}; 
public void addCartContent()
{
国际货币单位金额=0;
查看项目视图;
//“cart\u item\u table”是cart.xml中项目表的id
TableLayout tl=(TableLayout)findViewById(R.id.cart\u item\u table);
//我们将使用充气机提供的服务
LayoutFlater充气机=(LayoutFlater)getSystemService(布局充气机服务);
//执行循环多次添加购物车xml
对于(int i=0;i
嘿,我认为您的代码运行良好,在您的代码中,而不是使用

public void onClick(View arg0) {
            // TODO Auto-generated method stub

            TextView tv=(TextView) findViewById(R.id.pname);
            tv.setText("Deleted Item!");    

        }
使用


在您的实例中,由于您没有指定视图上下文,因此始终会获得与id匹配的第一个视图,在您的示例中,它是第一行。

我尝试了更改。它给我一个空指针异常。这是否意味着未传递视图?
 public void onClick(View view) {
            // TODO Auto-generated method stub

            TextView tv=(TextView) view.findViewById(R.id.pname);
            tv.setText("Deleted Item!");    

        }