-1
我想解決的問題,其中AdapterClass有方法的值不插入到database.insert("CUS_MENU", null, ocv)
和問題也顯示在這行數據不存儲在sqlite數據庫。我想在SQLite數據庫中存儲數據
public class AdapterClass
{
private Context context;
private SQLiteDatabase database;
CreateDB dbHelper;
private static String DATABASE_TABLE=" ";
public AdapterClass(Context pcontext, String pDBTableName)
{
// TODO Auto-generated constructor stub
this.context = pcontext;
this.DATABASE_TABLE = pDBTableName;
}
public AdapterClass Open() throws SQLException
{
dbHelper=new CreateDB(context);
database=dbHelper.getWritableDatabase();
return this;
}
// To Insert Record
public long insertquery(String[] pColumnNames, String[] pColumnValues) {
ContentValues initialValues = createContentValues(pColumnNames,pColumnValues);
return database.insert(DATABASE_TABLE, null, initialValues);
}
// Split the sting into two parts
public void addOrder(String s, int qty){
ContentValues ocv = new ContentValues();
String a[] = s.split("-");
String price = a[1].replaceAll("[^0-9]", "");
int total = Integer.parseInt(price)*qty;
ocv.put("PRICE", price);
ocv.put("NAME", a[0]);
ocv.put("QTY", qty);
ocv.put("TOTAL", total);
//db=this.getWritableDatabase();
database.insert("CUS_MENU", null, ocv);
database.close();
}
}
這orderscreen類錯誤,在顯示這些線order = new AdapterClass(this)
和order.addOrder(s,i)
。請任何人幫助解決這個問題。
public class OrderScreen extends Activity implements OnClickListener {
TextView orderitem;
EditText Quantity,devide_quanity;
Button Place_Order;
String tempstring;
Integer i;
String s;
String quanityforsoup;
StringBuilder orderdata=null;
AdapterClass order = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order_screen);
orderitem =(TextView)findViewById(R.id.orderitem);
Quantity =(EditText)findViewById(R.id.Quantity);
Place_Order =(Button)findViewById(R.id.Place_Order_btn);
order = new AdapterClass(this);
Place_Order.setOnClickListener(this);
s= getIntent().getStringExtra("order");
String a[] = s.split("-");
}
@Override
public void onClick(View v)
{
//Place_Order.setClickable(false);
tempstring=Quantity.getText().toString();
i=Integer.parseInt(tempstring);
Toast.makeText(OrderScreen.this, s, Toast.LENGTH_LONG);
//String temps=s;
// adding the data
order.addOrder(s,i);
Intent mIntent = new Intent(OrderScreen.this,MenuScreen.class);
startActivity(mIntent);
finish();
orderitem.setText("You have ordered "+s);
}
這是DB Helper類:
public class CreateDB extends SQLiteOpenHelper
{
SQLiteDatabase db;
public CreateDB(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
private static final String DATABASE_NAME = "restaurant";
private static final int DATABASE_VERSION = 1;
private static final String MENU= "create table CUS_MENU(_id integer primary key autoincrement, " + "" + "NAME text,"+"QTY text,"+"PRICE text,TOTAL text);";
@Override public void onCreate(SQLiteDatabase db)
{
db.execSQL(MENU);
}
}
顯示的分貝輔助類 –
任何異常堆棧跟蹤? – laalto
@laalto at com.example.myapprestaurant.OrderScreen.onClick(OrderScreen.java:55) – user3467384