这几天又看了点《代码大全2》,想着这东西要运用一下啊,于是结合作者软件构建的思维加Android的技术来练练手呗~虽然说系统有大有小,大系统大架构大考虑,小系统不需考虑那么多。
开发过程中遇到了各种问题,看书时觉得挺简单的东西,写起来就会出各种bug,开发工具又不是太会用,解决个问题要费老大的劲啊,真是抓狂!做完了现在记录下下~有的问题解决了,有的问题还是不知道怎么回事……(′д` )…彡…彡
按照软件开发过程,开始前的准备工作三部曲:问题定义、需求分析、软件架构。
一、问题定义
从客户的角度来看问题,用客户的语言来描述问题:开发一个个人密码管理工具
二、需求分析
通过口令(密码)进入密码本;
可以新增账号,保存到数据库;
可以查看全部账号密码信息,全部展示出来,就在一个页面,类似记事本;
可以修改账号信息,CRUD(增删改查);
安全:暂时不做要求
三、系统架构
package com.thsware.secretbook; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.widget.Toast; /** * Created by 世祥 on 2015/9/4. */ public class MyDataBaseHelper extends SQLiteOpenHelper { private static final String CREATE_SECRETBOOK="create table secretbook(" +"id integer primary key autoincrement," +"site text," +"account text," +"password text," +"note text," +"deleteFlag integer" +")"; private Context mContext; public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); mContext=context; } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL(CREATE_SECRETBOOK); Toast.makeText(mContext,"secretbook数据表创建成功~",Toast.LENGTH_SHORT).show(); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { switch (oldVersion){ case 1: sqLiteDatabase.execSQL("alter table secretbook add column deleteFlag integer"); default: } } }
package com.thsware.secretbook; import android.app.Activity; import java.util.ArrayList; import java.util.List; /** * Created by 世祥 on 2015/9/4. */ public class ActivityCollector { private static List<Activity> activities=new ArrayList<Activity>(); public static void addActivity(Activity activity){ activities.add(activity); } public static void removeActivity(Activity activity){ activities.remove(activity); } public static void finishAll(){ for (Activity ac : activities){ if (!ac.isFinishing()){ ac.finish(); } } } }
我还是不知道为什么,我想可能是startActivity(intent)出的问题吧,也可能是类加载的机制问题,看来我又忘了Java类加载的机制问题了吧~要找时间将没看完的《Thinking in Java》再好好学学了,,当时还明明白白的,现在好久没弄就忘了,,,,衰( ⊙ o ⊙ )啊!private MyDataBaseHelper dbHelper=new MyDataBaseHelper(AddActivity.this, DATABASE_NAME, null, 2); private SQLiteDatabase db= dbHelper.getWritableDatabase();
肯定是不好的,但是目前我也只能写成这样了,以后再慢慢优化吧!@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add); ActivityCollector.addActivity(this); siteEdit= (EditText) findViewById(R.id.site); accountEdit= (EditText) findViewById(R.id.account); passwordEdit= (EditText) findViewById(R.id.password); noteEdit= (EditText) findViewById(R.id.note); saveButton= (Button) findViewById(R.id.save_button); dbHelper=new MyDataBaseHelper(AddActivity.this, DATABASE_NAME, null, 2); db = dbHelper.getWritableDatabase(); Intent intent=getIntent(); //未检测intent是否为空,此处id无数据时为null id=intent.getStringExtra("id"); if (id!=null && !id.equals("")){ Cursor cursor=db.query(SECRETBOOK_TABLE, null, "id=?", new String[]{id}, null, null, null); if (cursor.moveToFirst()){ String site=cursor.getString(cursor.getColumnIndex("site")); String account=cursor.getString(cursor.getColumnIndex("account")); String password=cursor.getString(cursor.getColumnIndex("password")); String note=cursor.getString(cursor.getColumnIndex("note")); siteEdit.setText(site); accountEdit.setText(account); passwordEdit.setText(password); noteEdit.setText(note); } } //保存数据 saveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String site = siteEdit.getText().toString().trim(); if (site.equals("")) { Toast.makeText(AddActivity.this, R.string.sitename + "必须填写", Toast.LENGTH_SHORT).show(); return; } ContentValues values = new ContentValues(); values.put("site", site); values.put("account", accountEdit.getText().toString()); values.put("password", passwordEdit.getText().toString()); values.put("note", noteEdit.getText().toString()); //如果id为空,则是新增数据 if (id==null || id.equals("")){ values.put("deleteFlag", 0); db.insert(SECRETBOOK_TABLE, null, values); }else{ db.update(SECRETBOOK_TABLE,values,"id=?",new String[]{id}); } //未做判断,就提示成功了 Toast.makeText(AddActivity.this, "保存成功!", Toast.LENGTH_SHORT).show(); } }); }