无法在膨胀布局中实现onclicklistener

时间:2011-02-18 20:00:34

标签: android android-layout

我对Android很新。我正在做一个购物车应用程序。在我的购物车页面中,我显示每个项目的产品图片,名称,ID,数量,价格,总计和删除按钮。

我在table.xml中使用了表格布局来进行上述视图,并在cart.xml中的另一个tablelayout中将其展开。

问题在于,当我将onclick监听器添加到删除按钮时,它仅适用于第一行而不是相应的行。

我是以正确的方式做到的吗?请帮我。提前谢谢。

//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>

我的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()

1 个答案:

答案 0 :(得分:0)

嘿,我认为您的代码在您的代码中运行正常,而不是使用

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

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

        }

使用

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

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

        }

在您的实例中,由于您尚未指定视图上下文,因此您始终会获得与id匹配的第一个视图,在您的情况下,该视图是第一行。