Reputation: 184
I have a ADD TO CART Button to add the products to cart and in database.I would like to show the same button as ALREADY IN CART if the product is already in cart.I tried using if else,but it is not working.It does n't crash VM.But no effect on clicking.
My Code
add2cart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String nm=name.getText().toString();
String rate=price.getText().toString();
mydb=Product_Details.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
Cursor cur=mydb.rawQuery("select * from add2cart where pnme='"+nm+"'",null);
while (cur.moveToNext())
{
String name=cur.getString(cur.getColumnIndex("pnme"));
if (nm.equals(name))
{
add2cart.setText("Already in Cart");
}
else {
mydb.execSQL("INSERT INTO add2cart (pnme,prate)VALUES('"+nm+"','"+prprice+"')");
Toast.makeText(getApplicationContext(),"add to cart",Toast.LENGTH_SHORT).show();
Intent in=new Intent(Product_Details.this,add2cart.class);
startActivity(in);
} }
}
});
Upvotes: 0
Views: 783
Reputation: 2227
Perhaps you should try to rename one of your "name" variables. You could use this.name to specify which one you are referring to but I don't see the need to do that. The problem in fact might be that when you do if(nm.equals(name)) you are referring to the global variable name and not the one you set in the previous line. Hope I could help
Upvotes: 1