从方法onclickbuynow()插入记录时获取值为false,提前感谢 [英] Getting value false while inserting record from method onclickbuynow(), thanks in advance

查看:75
本文介绍了从方法onclickbuynow()插入记录时获取值为false,提前感谢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公共类MainActivity扩展AppCompatActivity {





TextView pizza_name,price,datalayoutaddress;

EditText数量;

RelativeLayout realtive,parentlayout,datalayout;

RadioGroup rg;

RadioButton rb;

按钮接下来,返回;

String registeration_name =xyz,registeration_username =abc,registeration_password =sdgf,registeration_phone =dghf,pizza_name1 =Paneer Pizza,pizza_price =89,mode_of_trnf =拿起,reg_address,time_,reg_qty;

私人RadioButton radioButton1;

TimePicker timePicker2;

InsertDatabaseHandler db;

ImageView图像;



@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate (savedInst anceState);

setContentView(R.layout.activity_main);



db = new InsertDatabaseHandler(this);

timePicker2 =(TimePicker)findViewById(R.id.timePicker2);

datalayoutaddress =(TextView)findViewById(R.id.address);

qty =(EditText )findViewById(R.id.qty);

}



public void onClickBuyNow(查看视图){

reg_address = datalayoutaddress.getText()。toString();

if(Build.VERSION.SDK_INT> = Build.VERSION_CODES.M){

time_ = timePicker2。 getHour()+:+ timePicker2.getMinute();

reg_qty = qty.getText()。toString();

System.out.println(购买时间:+ timePicker2.getHour()+:+ timePicker2.getMinute());

System.out.print ln(registeration_name + registeration_username + registeration_password + registeration_phone + pizza_name1 + pizza_price + mode_of_trnf + reg_address + time_ + reg_qty);

boolean result = db.insertCustRecord(registeration_name,registeration_phone,pizza_name1,pizza_price,mode_of_trnf,reg_address, time_,reg_qty);

System.out.println(结果);



if(result == true){

System.out.println(插入的客户数据......);

Toast.makeText(PizzaDetailSubActivity.this,谢谢你再次访问...,Toast.LENGTH_SHORT) .show();

}

else {

System.out.println(无法插入客户数据......);

}







}

}





public void onClickAddToCart(查看视图){

reg_address = datalayoutaddress.getText()。toString();

if(Build.VERSION.SDK_INT> = Build.VERSION_CODES.M){

Toast.makeText(this ,timePicker2.getHour()+:+ timePicker2.getMinute(),Toast.LENGTH_SHORT)。show();

time_ = timePicker2.getHour()+:+ timePicker2.getMinute( );

reg_qty = qty.getText()。toString();

System.out.println(购物车时间:+ timePicker2.getHour()+: + timePicker2.getMinute());

System.out.println(Quantity:+ reg_qty);

System.out.println(registeration_name + registeration_username + registeration_password + registeration_phone + pizza_name1 + pizza_price + mode_of_trnf + reg_address + time_ + reg_qty);

}

}





public void onClickOffers(查看视图){



}



public void onClickBack(查看视图){



}



}









// activity_main

<?xml version =1.0encoding =utf-8?>

< android.support.constraint。 ConstraintLayout xmlns:android =http://schemas.android.com/apk/res/android

xmlns:app =http://schemas.android.com/apk/res-auto

xmlns:tools =http://schemas.android.com/tools

android:layout_width =match_parent

android :layout_height =match_parent

工具:context =com.example.puja.demo.MainActivity>



< RelativeLayout

android:id =@ + id / datalayout

android:layout_width =368dp

android:layout_height =495dp

android:alpha =1

android:background =@ android:color / holo_orange_dark

工具:layout_editor_absoluteY =8dp

工具:layout_editor_absoluteX =8dp>



< EditText

android:id =@ + id / qty

android:layout_width =wrap_content

android:layout_height =wrap_content

android:layout_alignParentLeft =true

android:layout_alignParentStart =true

android:layout_alignTop =@ + id / image

android:layout_marginTop =65dp

android:ems =10

android:hint =输入数量

android:inputType =number/>



<按钮

android:id =@ + id / back_

=android:layout_width =wrap_contentandroid:layout_height =wrap_contentandroid:layout_marginleft =230dpandroid:layout_marginstart =230dpandroid:layout_margintop =450dpandroid:background =@ android:color / holo_red_lightandroid:onclick =onClickBackandroid:text =返回android:textcolor =@ android:color / background_light>



< button

android:id =@ + id / add_to_cart

=android:layout_width =wrap_contentandroid:layout_height =wrap_contentandroid:layout_marginleft =100dpandroid:layout_marginstart =100dpandroid:layout_margintop =450dpandroid :background =@ android:color / holo_red_lightandroid:onclick =onClickAddToCartandroid:text =Add to Ca rtandroid:textcolor =@ android:color / background_light>



< button

android:id =@ + id / buy_nows

=android:layout_width =wrap_contentandroid:layout_height =wrap_contentandroid:layout_margintop =450dpandroid:background =@ android:color / holo_red_lightandroid: onclick =onClickBuyNowandroid:text =立即购买android:textcolor =@ android:color / background_light>



< TimePicker

android:id =@ + id / timePicker2

android:layout_width =wrap_content

android:layout_height =wrap_content

android:layout_marginTop =130dp/>



< EditText

android:id =@ + id / address

android:layout_width =wrap_content

android:layout_h 8 =50dp

android:layout_alignParentLeft =true

android:layout_alignParentStart =true



android:layout_marginTop =10dp

android:background =@ android:color / holo_orange_light

android:elevation =5dp

android:ems =10

android:hint =输入邮政地址

android:inputType =textPostalAddress/>

b / b










/ / Database Handler具有insertCustRecord方法从活动中获取所有值并返回false值





公共类InsertDatabaseHandler扩展SQLiteOpenHelper {

private final static String TABLE_NAME2 =customer;





private final static String DATABASE_NAME =PIZZA.DB;

private final static String CartCOL1 =ID;

private final static String CartCol2 =NAME;

private final static String CartCol3 =USERNAME;

private final static String CartCol4 =PASSWORD;

private final static String CartCol5 =PHONE;

private final static String CartCol6 =PIZZA_NAME;

private final static String CartCol7 = 价格;

私人最终静态字符串CartCol8 =MODE_OF_TRANS;

私人最终静态字符串CartCol9 =地址;

私人最终static String CartCol10 =TIME;

private final static String CartCol11 =QUANTITY;





public InsertDatabaseHandler(Context context){

super(context,DATABASE_NAME,null,1);

$



@Override

public void onCreate(SQLiteDatabase db){

db.execSQL( CREATE TABLE+ TABLE_NAME2 +(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PHONE INTEGER,PIZZA_NAME TEXT,PRICE INTEGER,MODE_OF_TRANS TEXT,ADDRESS TEXT,TIME TEXT,QUANTITY INTEGER));

系统.out.println(Customer Table Created ...);

}



@Override

public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){

db.execSQL(DROP TABLE IF EXISTS+ TABLE_NAME2);

}



public boolean insertCustRecord(String name,String phone,String pizza_name,String price,String mode,String address,String time_,String qty){

系统。 out.println(name + phone);

SQLiteDatabase db = this.getWritableDatabase();

内容值contentValues = new ContentValues();

contentValues.put(CartCol2,name);

contentValues.put(CartCol5,phone);

contentValues.put(PIZZA_NAME,pizza_name);

contentValues.put(PRICE,price);

contentValues.put(MODE_OF_TRANS,mode);

contentValues.put(ADDRESS,地址);

contentValues.put(TIME,time_);

contentValues.put( QUANTITY,qty);

long value = db.insert(TABLE_NAME2,null,contentValues);

db.close();

if(value == -1)

返回false;

else

返回true;

}





public Cursor getCustData(){

SQLiteDatabase db = this.getWritableDatabase();

光标曲线sor = db.rawQuery(select * from+ TABLE_NAME2,null);

返回光标;

}



}



我的尝试:



我的活动具有onClickBuyNow()方法的类从数据库返回false值所有字段都有值没有空值已经传递给数据库

public class MainActivity extends AppCompatActivity {


TextView pizza_name,price,datalayoutaddress;
EditText qty;
RelativeLayout realtive,parentlayout,datalayout;
RadioGroup rg;
RadioButton rb;
Button next,back;
String registeration_name="xyz",registeration_username="abc",registeration_password="sdgf",registeration_phone="dghf",pizza_name1="Paneer Pizza",pizza_price="89",mode_of_trnf="pick up",reg_address,time_,reg_qty;
private RadioButton radioButton1;
TimePicker timePicker2;
InsertDatabaseHandler db;
ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

db=new InsertDatabaseHandler(this);
timePicker2=(TimePicker)findViewById(R.id.timePicker2);
datalayoutaddress=(TextView)findViewById(R.id.address);
qty=(EditText)findViewById(R.id.qty);
}

public void onClickBuyNow(View view) {
reg_address = datalayoutaddress.getText().toString();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
time_ = timePicker2.getHour() + ":" + timePicker2.getMinute();
reg_qty = qty.getText().toString();
System.out.println("Buy Time:" + timePicker2.getHour() + ":" + timePicker2.getMinute());
System.out.println(registeration_name + registeration_username + registeration_password + registeration_phone + pizza_name1 + pizza_price + mode_of_trnf + reg_address + time_ + reg_qty);
boolean result = db.insertCustRecord(registeration_name,registeration_phone,pizza_name1,pizza_price,mode_of_trnf,reg_address,time_,reg_qty);
System.out.println(result);

if (result == true){
System.out.println("Cust data inserted ...");
Toast.makeText(PizzaDetailSubActivity.this,"Thank You Visit Again...",Toast.LENGTH_SHORT).show();
}
else {
System.out.println("Cust data cannot be inserted ...");
}



}
}


public void onClickAddToCart(View view){
reg_address=datalayoutaddress.getText().toString();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Toast.makeText(this,timePicker2.getHour()+":"+timePicker2.getMinute(),Toast.LENGTH_SHORT).show();
time_=timePicker2.getHour()+":"+timePicker2.getMinute();
reg_qty=qty.getText().toString();
System.out.println("Cart Time:"+timePicker2.getHour()+":"+timePicker2.getMinute());
System.out.println("Quantity : "+reg_qty);
System.out.println( registeration_name+registeration_username+registeration_password+registeration_phone+pizza_name1+pizza_price+mode_of_trnf+reg_address+time_+reg_qty);
}
}


public void onClickOffers(View view){

}

public void onClickBack(View view){

}

}




//activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.puja.demo.MainActivity">

<RelativeLayout
android:id="@+id/datalayout"
android:layout_width="368dp"
android:layout_height="495dp"
android:alpha="1"
android:background="@android:color/holo_orange_dark"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">

<EditText
android:id="@+id/qty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/image"
android:layout_marginTop="65dp"
android:ems="10"
android:hint="Enter Quantity"
android:inputType="number" />

<button
android:id="@+id/back_"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="230dp" android:layout_marginstart="230dp" android:layout_margintop="450dp" android:background="@android:color/holo_red_light" android:onclick="onClickBack" android:text="Back" android:textcolor="@android:color/background_light">

<button
android:id="@+id/add_to_cart"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="100dp" android:layout_marginstart="100dp" android:layout_margintop="450dp" android:background="@android:color/holo_red_light" android:onclick="onClickAddToCart" android:text="Add To Cart" android:textcolor="@android:color/background_light">

<button
android:id="@+id/buy_nows"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="450dp" android:background="@android:color/holo_red_light" android:onclick="onClickBuyNow" android:text="Buy Now" android:textcolor="@android:color/background_light">

<TimePicker
android:id="@+id/timePicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="130dp" />

<EditText
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"

android:layout_marginTop="10dp"
android:background="@android:color/holo_orange_light"
android:elevation="5dp"
android:ems="10"
android:hint="Enter Postal Address"
android:inputType="textPostalAddress" />






//Database Handler having insertCustRecord method fetching all values from activity and returning false value


public class InsertDatabaseHandler extends SQLiteOpenHelper{
private final static String TABLE_NAME2="customer";


private final static String DATABASE_NAME = "PIZZA.DB";
private final static String CartCOL1 = "ID";
private final static String CartCol2="NAME";
private final static String CartCol3="USERNAME";
private final static String CartCol4="PASSWORD";
private final static String CartCol5="PHONE";
private final static String CartCol6="PIZZA_NAME";
private final static String CartCol7="PRICE";
private final static String CartCol8="MODE_OF_TRANS";
private final static String CartCol9="ADDRESS";
private final static String CartCol10="TIME";
private final static String CartCol11="QUANTITY";


public InsertDatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME2 + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PHONE INTEGER,PIZZA_NAME TEXT,PRICE INTEGER,MODE_OF_TRANS TEXT,ADDRESS TEXT,TIME TEXT,QUANTITY INTEGER)");
System.out.println("Customer Table Created ...");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME2);
}

public boolean insertCustRecord(String name,String phone,String pizza_name ,String price,String mode,String address,String time_,String qty){
System.out.println(name+phone);
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(CartCol2, name);
contentValues.put(CartCol5, phone);
contentValues.put("PIZZA_NAME", pizza_name);
contentValues.put("PRICE", price);
contentValues.put("MODE_OF_TRANS", mode);
contentValues.put("ADDRESS", address);
contentValues.put("TIME", time_);
contentValues.put("QUANTITY", qty);
long value=db.insert(TABLE_NAME2, null, contentValues);
db.close();
if (value == -1)
return false;
else
return true;
}


public Cursor getCustData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from " + TABLE_NAME2, null);
return cursor;
}

}

What I have tried:

my activity class having onClickBuyNow() method returning false value from database all fields are having values no null values have been passed to database

推荐答案

我们无法帮助你解决这个问题 - 它需要您的数据,并在代码运行该数据时查看正在发生的事情。我们没有您的数据,我们没有您的整个应用程序 - 如果我们这样做,我们也不知道如何使用它!



所以,它取决于你。

在函数的第一行放置断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
We can't help you with this - it needs your data, and to be looking at what is happening while the code is running on that data. And we don't have your data, we don't have your whole app - and we don't know how to use it if we did!

So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


这篇关于从方法onclickbuynow()插入记录时获取值为false,提前感谢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆