标签:
磕磕绊绊的写了第一个程序,电脑报价单。工作需要,经常做些报价单,所有就有了这个小程序。虽然能运行了,但其中的一些代码不甚了解,在这里做个学习总结。
基本界面。
用到SQLite数据库
package com.example.diyquote; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class MySQLiteOpenHelper extends SQLiteOpenHelper { // 定义数据库的版本 public static final int DB_VERSION = 1; // 定义数据库的名字 public static final String DB_NAME = "mydata.db"; public MySQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(StrName.STR_MAIN_LIST); //StrName为另外定义的包含全部需要用的常量的类,以方便使用。这个是建立首页ListView需要的数据库,用来存储客户和渠道信息 db.execSQL(StrName.DETAILED_TEABLE);//另外的表,存储所有的配件的报价。 } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
定义了一个需要常用的常量的类StrName,方便使用
package com.example.diyquote; import java.text.SimpleDateFormat; import java.util.Date; public class StrName { public static final String TEABLE_NAME_MAINLIST="mainlist"; //用来存储客户和渠道信息的表的名字 public static final String TABLE_NAME_DETAILED="detailed"; //存储 配件信息价格之类的表的名字。 public static final String DETAILED_TEABLE= "create table detailed (" //建立表 +" _id integer primary key autoincrement," //_id字段,ListView需要 +" imgsrc text," //配件的IMG,并未使用 +" peijian text," //配件的名字,CPU,主板,显卡 之类 +" xinghao text," //配件的型号 +" baojia integer," //报价 +" jinjia integer," //进价 +" shuliang integer," //数量 +" lirun integer," //利润 +" xiaoji integer," //利润小计 +" mainid integer)"; //maiId,与mainlist表中的_id对应,每个客户和渠道信息对应一个mainlist中的_id,也就是这里的mainid public static final String STR_MAIN_LIST="create table mainlist (" //建表 +" _id integer primary key autoincrement," //_id字段 +" textlist text," //客户和渠道信息 +" date text)"; //存储时的日期 public static String getDate() //获取系统日期时间 { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Date curDate = new Date(System.currentTimeMillis()); String date = formatter.format(curDate); return date; } }
MainActvity
package com.example.diyquote; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.view.View.OnClickListener; import android.view.Window; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; public class MainActivity extends Activity { private MySQLiteOpenHelper dbHelper = new MySQLiteOpenHelper(this, MySQLiteOpenHelper.DB_NAME, null, MySQLiteOpenHelper.DB_VERSION); private TextView add_textView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); SQLiteDatabase db = dbHelper.getReadableDatabase(); add_textView = (TextView) findViewById(R.id.add_textView); ListView listView = (ListView) findViewById(R.id.main_listview); add_textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(MainActivity.this, AddActivity.class); intent.putExtra("updataoradd", 1); // 传递过去数值用来swich判断是哪个启动的addActivity 修改和增加都调用addActivity startActivityForResult(intent, 1); } }); listviewShow(); // 定义Listview的长按菜单 registerForContextMenu(listView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("菜单"); menu.add(0, 1, 0, "详细"); menu.add(0, 2, 0, "修改"); menu.add(0, 3, 0, "删除"); } @Override public boolean onContextItemSelected(MenuItem menu) { // TODO Auto-generated method stub SQLiteDatabase db = dbHelper.getWritableDatabase(); AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menu.getMenuInfo(); int id = (int) info.id;// 此id 对应数据库里的_id Cursor cursor = db.query("detailed", null, "mainid=?", new String[] { id + "" }, null, null, null); //查询 detailed中 字段 mainid 中值为 mainlist表的_id 的所有项目 switch (menu.getItemId()) { case 1: //点击 长按菜单的 详细 Intent intent = new Intent(MainActivity.this, DetailedActivity.class); intent.putExtra("mainidIsnull", cursor.moveToFirst());// 判断点击的ID对应的mainId是不是存在,以便在DetailedActivity中是不是调用listview() // (如果mainid不存在,listview将产生错误) intent.putExtra("mainid", id); startActivity(intent); break; case 2: //修改 Intent intent1 = new Intent(MainActivity.this, AddActivity.class); intent1.putExtra("updataoradd", 2); // 传递过去数值用来swich判断是哪个启动的addActivity 增加和修改都是调用addActvity intent1.putExtra("_id", id); startActivityForResult(intent1, 1); break; case 3: //删除 // 删除一行数据 db.delete("mainlist", "_id=?", new String[] { id + "" }); //删除mainlist中的一行同时,也删除了它对应的 detailed里所有关联的数据, db.delete("detailed", "mainid=?", new String[] { id + "" }); listviewShow(); break; } return super.onContextItemSelected(menu); } private void listviewShow() { SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.query(StrName.TEABLE_NAME_MAINLIST, null, null, null, null, null, null); SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.main_listveiw_show, cursor, new String[] { "textlist", "date" }, new int[] { R.id.main_listview_title, R.id.main_listview_date }, 0); ListView listView = (ListView) findViewById(R.id.main_listview); listView.setAdapter(adapter); db.close(); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub String str_show = ""; // 定义字符串,用来显示在主页上的Listview里 super.onActivityResult(requestCode, resultCode, data); switch (resultCode) // 判断 AddActivit是按 Button还是直接按 返回键返回的。 { case RESULT_OK: listviewShow(); break; case RESULT_CANCELED: listviewShow(); break; } } }
addActvity
package com.example.diyquote; import android.app.Activity; import android.content.ContentValues; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class AddActivity extends Activity { private EditText name_customer_EditText, name_supplier_EditText; private Button button; private String str_customer, str_supplier; private MySQLiteOpenHelper dbHelper = new MySQLiteOpenHelper(this, MySQLiteOpenHelper.DB_NAME, null, MySQLiteOpenHelper.DB_VERSION); protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main_add); // 获取EditText的控件 name_customer_EditText = (EditText) findViewById(R.id.name_customer); name_supplier_EditText = (EditText) findViewById(R.id.name_supplier); str_customer = name_customer_EditText.getText().toString(); str_supplier = name_supplier_EditText.getText().toString(); // 获取EditText上输入的字符串 button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { str_customer = name_customer_EditText.getText().toString(); str_supplier = name_supplier_EditText.getText().toString(); SQLiteDatabase db = dbHelper.getReadableDatabase(); ContentValues values = new ContentValues(); Intent intent = new Intent(); if ("".equals(str_customer) || "".equals(str_supplier)) //客户和渠道信息都为必添项目 { Toast.makeText(AddActivity.this, "请输入客户信息和渠道信息", Toast.LENGTH_LONG).show(); } else { switch (getIntent().getIntExtra("updataoradd", 1)) //判断一下是 增加 还是 修改 哪个来启动的addActivity, { case 1: //增加 values.put("textlist", str_customer + "-----" + str_supplier); values.put("date", StrName.getDate()); db.insert("mainlist", null, values); values.clear(); setResult(RESULT_OK, intent); finish(); break; case 2: //修改 values.put("textlist", str_customer + "-----" + str_supplier); //getIntent().getIntExtra("_id", 0) 为 修改 传递过来的_id db.update(StrName.TEABLE_NAME_MAINLIST, values, "_id=?", new String[]{getIntent().getIntExtra("_id", 0)+""}); values.clear(); setResult(RESULT_OK, intent); finish(); break; } } } }); } public void onBackPressed() //当用户直接 按 返回键 时,直接返回。不做任何保存修改 { // TODO Auto-generated method stub super.onBackPressed(); // 直接按 返回键 时 返回空值 Intent intent = new Intent(); setResult(RESULT_CANCELED, intent); finish(); } }
DetailedActivity
package com.example.diyquote; import android.annotation.SuppressLint; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.view.View.OnClickListener; import android.view.View.OnCreateContextMenuListener; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; import android.widget.Toast; public class DetailedActivity extends Activity { MySQLiteOpenHelper dbHelper = new MySQLiteOpenHelper(this, MySQLiteOpenHelper.DB_NAME, null, MySQLiteOpenHelper.DB_VERSION); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_detailed); TextView detailed_add_TextView = (TextView) findViewById(R.id.detailed_add); ListView detailed_listview = (ListView) findViewById(R.id.detailed_listview); detailed_add_TextView.setOnClickListener(new OnClickListener() //屏幕左上角的 + 号的 TextView 的点击事件 { @Override public void onClick(View arg0) { LinearLayout layout_edit = (LinearLayout) findViewById(R.id.layout_edittext); EditTextData(layout_edit.getVisibility(), 0); //layout_edit.getVisibility(),为判断这个layout是不是可见, } }); detailed_listview.setOnCreateContextMenuListener(new OnCreateContextMenuListener() //ListView的长按菜单 { @Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo info) { menu.setHeaderTitle("菜单"); menu.add(0, 1, 0, "修改"); menu.add(0, 2, 0, "删除"); } }); if (getIntent().getBooleanExtra("mainidIsnull", false)) // 判断mainid是不是存在 { listviewShow(); } } @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub SQLiteDatabase db = dbHelper.getWritableDatabase(); AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); int id = (int) info.id; switch (item.getItemId()) { case 1: //修改数据 EditTextData(-1, id); //id 点击了哪个数据,对应数据库里的_id break; case 2: //删除数据 db.delete("detailed", "_id=?", new String[] { id + "" });// 这里为什么用StrName.DETAILED_NAME会出错误????????????????????????????? listviewShow(); break; } return super.onContextItemSelected(item); } private void listviewShow() { SQLiteDatabase db = dbHelper.getWritableDatabase(); ListView listView = (ListView) findViewById(R.id.detailed_listview); int mainId = getIntent().getIntExtra("mainid", 0); Cursor cursor = db.query("detailed", null, "mainid=?", new String[] { mainId + "" }, null, null, null); SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.detailed_listview_show, cursor, new String[] { "peijian", "xinghao", "baojia", "jinjia", "shuliang", "lirun", "xiaoji" }, new int[] { R.id.peijian, R.id.xinghao, R.id.baojia, R.id.jinjia, R.id.shuliang, R.id.lirun, R.id.xiaoji }, 0); listView.setAdapter(adapter); db.close(); } boolean EditTextData(int viewIsSo, int id) //定义了一个方法,用来处理很多EditText,参数viewIsSo:包含7个EditText的Layout是否可见 int id 参数 : 为长按菜单上对应的的id, { boolean flag = true;// 定义一个boolean的变量,用来函数返回,以便以后用? LinearLayout layout_edit = (LinearLayout) findViewById(R.id.layout_edittext); TextView detailed_add_TextView = (TextView) findViewById(R.id.detailed_add); EditText peijia_EditText = (EditText) findViewById(R.id.edit_peijian); EditText xinghao_EditText = (EditText) findViewById(R.id.edit_xinghao); EditText baojia_EditText = (EditText) findViewById(R.id.edit_baojia); EditText jinjia_EditText = (EditText) findViewById(R.id.edit_jinjia); EditText shuliang_EditText = (EditText) findViewById(R.id.edit_shuliang); EditText lirun_EditText = (EditText) findViewById(R.id.edit_lirun); EditText xiaoji_EditText = (EditText) findViewById(R.id.edit_xiaoji); SQLiteDatabase db = dbHelper.getReadableDatabase(); ContentValues values = new ContentValues(); switch (viewIsSo) { case View.VISIBLE: // 控件 可见时,按钮为 确定 时,执行这个。提取EditText里的内容到数据库 layout_edit.setVisibility(View.GONE);// 使Layout edittext 控件可见 // 插入数据 if (!("".equals(peijia_EditText.getText().toString()))) // 配件的EditText里不为空,才能插入数据库。避免插入整个空行进数据库 { values.put("peijian", peijia_EditText.getText().toString()); values.put("xinghao", xinghao_EditText.getText().toString()); values.put("baojia", baojia_EditText.getText().toString()); values.put("jinjia", jinjia_EditText.getText().toString()); values.put("shuliang", shuliang_EditText.getText().toString()); values.put("lirun", lirun_EditText.getText().toString()); values.put("xiaoji", xiaoji_EditText.getText().toString()); values.put("mainid", getIntent().getIntExtra("mainid", 0)); db.insert(StrName.TABLE_NAME_DETAILED, null, values); values.clear(); listviewShow(); } detailed_add_TextView.setText("[ + ]"); flag = true; break; case View.GONE:// 控件不可见,按钮为 + 时,执行这个 layout_edit.setVisibility(View.VISIBLE); // 使控件可见 detailed_add_TextView.setText("确定"); // 清空EdtiText控件里的内容 peijia_EditText.setText(""); xinghao_EditText.setText(""); baojia_EditText.setText(""); jinjia_EditText.setText(""); shuliang_EditText.setText(""); lirun_EditText.setText(""); xiaoji_EditText.setText(""); flag = false; break; case -1: //修改数据。有个问题,不是之直接修改,而是原数据基础上增加,还需要手动删除对应的数据。 Cursor cursor = db.query("detailed", null, "_id=?", new String[] { id + "" }, null, null, null); if (cursor.moveToFirst()) { do { { peijia_EditText.setText(cursor.getString(cursor.getColumnIndex("peijian"))); xinghao_EditText.setText(cursor.getString(cursor.getColumnIndex("xinghao"))); xiaoji_EditText.setText(cursor.getString(cursor.getColumnIndex("xiaoji"))); baojia_EditText.setText(cursor.getString(cursor.getColumnIndex("baojia"))); jinjia_EditText.setText(cursor.getString(cursor.getColumnIndex("jinjia"))); shuliang_EditText.setText(cursor.getString(cursor.getColumnIndex("shuliang"))); lirun_EditText.setText(cursor.getString(cursor.getColumnIndex("lirun"))); } } while (cursor.moveToNext()); } layout_edit.setVisibility(View.VISIBLE); // 使控件可见 detailed_add_TextView.setText("确定"); flag=true; break; } return flag; } }
几个布局文件
activity_main.XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#3f51b5" android:orientation="horizontal" > <TextView android:id="@+id/add_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="[ + ]" android:textColor="#FFFFFF" android:textSize="25sp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="电脑报价单 " android:textColor="#FFFFFF" android:textSize="25sp" android:textStyle="bold" /> </LinearLayout> <ListView android:id="@+id/main_listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#0000ff" android:dividerHeight="2px" android:footerDividersEnabled="true" > </ListView> </LinearLayout>
activity_detailed.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/layout_detailed_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:background="#3f51b5" android:orientation="horizontal" > <TextView android:id="@+id/detailed_add" android:layout_width="60dp" android:layout_height="wrap_content" android:text="[ + ]" android:textColor="#FFFFFF" android:textSize="25sp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="电脑报价单 " android:textColor="#FFFFFF" android:textSize="25sp" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:id="@+id/layout_detailed_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/layout_detailed_title" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="10" android:gravity="center" android:text="配件" android:textStyle="bold" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="15" android:ellipsize="marquee" android:gravity="center" android:marqueeRepeatLimit="-1" android:singleLine="true" android:text="型号" android:textStyle="bold" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:gravity="center" android:text="报价" android:textStyle="bold" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:gravity="center" android:text="进价" android:textStyle="bold" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:gravity="center" android:text="数量" android:textStyle="bold" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:gravity="center" android:text="利润" android:textStyle="bold" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="6" android:gravity="center" android:text="小计" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:id="@+id/layout_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" android:visibility="gone" > <EditText android:id="@+id/edit_peijian" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="10" android:hint="配件" android:singleLine="true" android:textSize="10sp" /> <EditText android:id="@+id/edit_xinghao" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="15" android:hint="型号" android:singleLine="true" android:textSize="10sp" /> <EditText android:id="@+id/edit_baojia" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:inputType="number" android:singleLine="true" android:textSize="10sp" /> <EditText android:id="@+id/edit_jinjia" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:inputType="number" android:singleLine="true" android:textSize="10sp" /> <EditText android:id="@+id/edit_shuliang" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:inputType="number" android:singleLine="true" android:textSize="10sp" /> <EditText android:id="@+id/edit_lirun" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:inputType="number" android:singleLine="true" android:textSize="10sp" /> <EditText android:id="@+id/edit_xiaoji" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="6" android:inputType="number" android:singleLine="true" android:textSize="10sp" /> </LinearLayout> <ListView android:id="@+id/detailed_listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/layout_edittext" android:layout_below="@id/layout_detailed_textview" android:divider="#000000" android:footerDividersEnabled="true" > </ListView> </RelativeLayout>
detailed_listview_show.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/peijian" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="10" android:gravity="left" /> <TextView android:id="@+id/xinghao" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="15" android:gravity="left" /> <TextView android:id="@+id/baojia" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:gravity="left" /> <TextView android:id="@+id/jinjia" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:gravity="left" /> <TextView android:id="@+id/shuliang" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:gravity="left" /> <TextView android:id="@+id/lirun" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:gravity="left" /> <TextView android:id="@+id/xiaoji" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="6" android:gravity="left" /> </LinearLayout>
main_listview_show.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/main_listview_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="7" android:gravity="left" android:paddingLeft="15dp" /> <TextView android:id="@+id/main_listview_date" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" android:gravity="left" android:paddingRight="15dp" /> </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/qq28902581/p/4600665.html