package com.example.order;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.widget.Toast;
public class DBHelper extends SQLiteOpenHelper{
private Context mContext;
public DBHelper(Context context,String name,CursorFactory factory,int version)
{
super(context,name,factory,version);
mContext=context;
}
public void onCreate(SQLiteDatabase db)
{
String DATABASE_CREATE="create table orderdetail(_id integer primary key autoincrement,orderid text,detailName text,num text,detailid text,menuid text,sate text,price text);";
db.execSQL(DATABASE_CREATE);
Toast.makeText(mContext,"创建成功!",Toast.LENGTH_LONG).show();
}
public void onUpgrade(SQLiteDatabase db,int arg1,int arg2)
{
String sql="DROP TABLE IF EXISTS orderdetail";
db.execSQL(sql);
this.onCreate(db);
}
}
MainActivity.java
package com.example.order;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private EditText tdetailid,torderid,tmenuid,tdetailName,tnum,tsate,tprice,morderid;
private Button insert,search;
DBHelper dbHelper;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper=new DBHelper(this,"orderdetail.db",null,2);
insert=(Button)findViewById(R.id.insert);
insert.setOnClickListener(this);
search=(Button)findViewById(R.id.search);
search.setOnClickListener(this);
tdetailid=(EditText)findViewById(R.id.et1);
torderid=(EditText)findViewById(R.id.et2);
tmenuid=(EditText)findViewById(R.id.et3);
tdetailName=(EditText)findViewById(R.id.et4);
tnum=(EditText)findViewById(R.id.et5);
tsate=(EditText)findViewById(R.id.et6);
tprice=(EditText)findViewById(R.id.et7);
morderid=(EditText)findViewById(R.id.et8);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.insert:
String forderid=torderid.getText().toString().trim();
String fdetailName=tdetailName.getText().toString().trim();
String fnum=tnum.getText().toString().trim();
String fdetailid=tdetailid.getText().toString().trim();
String fmenuid=tmenuid.getText().toString().trim();
String fsate=tsate.getText().toString().trim();
String fprice=tprice.getText().toString().trim();
db=dbHelper.getWritableDatabase();
db.execSQL("insert into orderdetail(forderid,fdetailName,fnum,fdetailid,fmenuid,fsate,fprice) values(?,?,?,?,?,?,?)",new String[]{forderid,fdetailName,fnum,fdetailid,fmenuid,fsate,fprice});
Toast.makeText(this, "添加成功!", Toast.LENGTH_LONG).show();
break;
case R.id.search:
String norderid=morderid.getText().toString().trim();
Bundle data=new Bundle();
Cursor curso = dbHelper.getReadableDatabase().rawQuery("select * from orderdetail where orderID="+norderid,null );
List<OrderDetail> OrderList = new ArrayList<OrderDetail>();
while(curso.moveToNext()){
int detailid = curso.getInt(curso.getColumnIndex("orderdetialid"));
String orderid = curso.getString(curso.getColumnIndex("orderID"));
int menuid = curso.getInt(curso.getColumnIndex("menuid"));
String detailName = curso.getString(curso.getColumnIndex("detailName"));
int num = curso.getInt(curso.getColumnIndex("quantity"));
int sate = curso.getInt(curso.getColumnIndex("detailsate"));
int price =curso.getInt(curso.getColumnIndex("price"));
OrderDetail od = new OrderDetail(detailid,orderid,menuid,detailName,num,sate,price);
OrderList.add(od);
}
data.putSerializable("orderid",norderid );
Intent intent=new Intent(MainActivity.this,OrderActivity.class);
intent.putExtras(data);
startActivity(intent);
break;
default:break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}