OnClickListener()不起作用

时间:2013-12-07 13:52:34

标签: android onclicklistener

请帮助我,我知道为什么我的代码不起作用。当我点击按钮时,没有任何反应。

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class Meat1Detail extends Activity {
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        setContentView(R.layout.meat1detail);
        final TextView title=(TextView) findViewById(R.id.meat1detailtitle);
        final EditText qty=(EditText)findViewById(R.id.meat1quantity);
        final TextView price=(TextView)findViewById(R.id.meat1price) ;
        Button btnmeat1= (Button) findViewById(R.id.meat1addtocart);



        btnmeat1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                try {
                    SQLiteDatabase database = openOrCreateDatabase("ORCL", MODE_PRIVATE, null);
                    database.execSQL("CREATE TABLE IF NOT EXISTS CART(title VARCHAR,qty INT,price INT);");
                    database.execSQL("INSERT INTO CART VALUES(" + title.getText().toString() + ",'" + Integer.parseInt(qty.getText().toString())+",'"
                            + Integer.parseInt(price.getText().toString()));

                    database.close();
                    Toast.makeText(getApplicationContext(),"Add To Cart List.",Toast.LENGTH_LONG).show();
                }
                catch (Exception ex){
                    Toast.makeText(getApplicationContext(),ex.getMessage(),Toast.LENGTH_LONG).show();
                }
            }
        }



    );

}
}

meat1detail.xml

ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="250dp"
              android:layout_height="fill_parent"
        android:background="#525150" android:foregroundGravity="center">
    <RelativeLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="8dp">
       <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:id="@+id/meat1detailtitle"
               android:text="Great Value Naturally Hardwood Smoked Bacon "
               android:textColor="#2aff0c"
               android:textStyle="bold">

       </TextView>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/meat1ingredient"
                android:text="Ingredients: "
                android:textColor="#2aff0c"
                android:textStyle="bold"
                android:layout_marginTop="20dp"
                android:layout_below="@id/meat1detailtitle">

        </TextView>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/meat1ingredientdetail"
                android:text="Pork Cured With: Water, Salt, Sodium Phosphate, Brown Sugar, Sodium Erythorbate, Sodium Nitrite."
                android:textColor="#2aff0c"
                android:layout_below="@id/meat1ingredient">

        </TextView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/meat1pricetitle"
            android:text="Price: $ "
            android:textColor="#2aff0c"
            android:textStyle="bold"
            android:layout_marginTop="20dp"
            android:layout_below="@id/meat1ingredientdetail">

    </TextView>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/meat1price"
                android:text="8"
                android:textColor="#2aff0c"
                android:textStyle="bold"
                android:layout_below="@id/meat1ingredientdetail"
                android:layout_marginTop="20dp"
                android:layout_toRightOf="@id/meat1pricetitle"
                android:layout_marginLeft="5dp">

        </TextView>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/meat1titlequantity"
                android:text="Qantity:"
                android:textColor="#2aff0c"
                android:textStyle="bold"
                android:layout_below="@id/meat1pricetitle">

        </TextView>
        <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/meat1quantity"
                android:textColor="@android:color/black"
                android:hint="Enter the Qauntity"
                android:background="@android:color/white" android:textSize="9dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/meat1titlequantity"
                android:layout_below="@id/meat1pricetitle"
                android:layout_marginLeft="5dp">

        </EditText>
        <ImageView
                android:layout_width="15dp"
                android:layout_height="20dp"
                android:background="@drawable/nextpageleft"
                android:id="@+id/meat1arrow"
                android:layout_alignRight="@+id/meat1detailtitle" android:layout_alignParentTop="true"
                android:layout_marginTop="198dp">

        </ImageView>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/meat1addtocart"
                android:background="@drawable/addtocart"
                android:layout_alignLeft="@+id/meat1detailtitle" android:layout_below="@+id/meat1titlequantity">

        </Button>
</RelativeLayout>

</ScrollView>

1 个答案:

答案 0 :(得分:0)

try 
{
    SQLiteDatabase database = openOrCreateDatabase("ORCL", MODE_PRIVATE, null);
    database.execSQL("CREATE TABLE IF NOT EXISTS CART(title VARCHAR(150),qty INT(10),price INT(10));");
    database.execSQL("INSERT INTO CART(title,qty,price) VALUES('"
        + title.getText().toString() + "',"
        + Integer.parseInt(qty.getText().toString())+","
        + Integer.parseInt(price.getText().toString())+");");    
    database.close();
    Toast.makeText(getApplicationContext(),"Add to Cart",Toast.LENGTH_LONG).show();
}
catch (Exception ex)
{
    Toast.makeText(getApplicationContext(),ex.getMessage(),Toast.LENGTH_LONG).show();
}