本文共 5200 字,大约阅读时间需要 17 分钟。
良好的代码编写风格,写好的代码几乎不用调试。
方法:
1.适当的用全局变量,如控件,adapter,DB,其他定义的类。
2.多建立initSomething 函数
全局变量如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class MainActivity extends Activity { Button sure; EditText login; EditText pws; CheckBox checkButton; LoginDB mydb; ProductDB myProductDB; ToAddrDB myToAddrDB; HistoryDB myHistoryDB; TextView pwstext; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); } |
初始化全局变量构造一个init()函数来初始话,这样的话一个activity有时候想换布局就容易多了。
而如果要给数据赋值构造一个initData或initDB的函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | /* 展开看接口, * * */ package com.sqlitedb; import java.io.File; import com.ProjectClass.Client; import com.ProjectClass.Product; import com.ProjectClass.ToAddr; import com.projectConstant.ProjectConstant; import com.sqtest.R; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { Button sure; EditText login; EditText pws; CheckBox checkButton; LoginDB mydb; ProductDB myProductDB; ToAddrDB myToAddrDB; HistoryDB myHistoryDB; TextView pwstext; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } public void init(){ sure = (Button)findViewById(R.id.button1); login = (EditText)findViewById(R.id.editText2); pws = (EditText)findViewById(R.id.editText1); checkButton = (CheckBox)findViewById(R.id.checkBox1); pwstext = (TextView)findViewById(R.id.textview1); mydb = new LoginDB( this ); myProductDB = new ProductDB( this ); myToAddrDB = new ToAddrDB( this ); myHistoryDB = new HistoryDB( this ); //sure.setOnClickListener(productClick); InitDB(); //sure.setOnClickListener(Clientclick); sure.setOnClickListener(HistoryClick); } public void InitDB(){ markDir(); Product myproductInfo; String tcprices[] = { "30" , "20" , "15" , "30" , "90" , "50" }; String tcproductNames[] = { "烤鸭" , "烤鸡" , "烤牛" , "烤猪" , "烤狗" , "烤鼠" }; String tcnumbers[] = { "1" , "2" , "3" , "4" , "5" , "6" }; //String types[] = {"1", "2", "1", "1", "3","2"}; for ( int i = 0 ; i < tcprices.length; i++){ myproductInfo = new Product(); //必须这样在这里new String price = tcprices[i]; myproductInfo.setProduct( "贪吃吧" , tcproductNames[i], "null" , price, tcnumbers[i]); myProductDB.operateProduct(myproductInfo); //myHistoryDB.operateHProduct("lilin", types[i], myproductInfo); } String kxprices[] = { "30" , "20" , "15" , "30" , "90" , "50" }; String kxproductNames[] = { "溜冰" , "滑雪" , "吹牛" , "自恋" , "遛狗" , "溜鼠" }; String kxnumbers[] = { "1" , "2" , "3" , "4" , "5" , "6" }; //String types[] = {"1", "2", "1", "1", "3","2"}; for ( int i = 0 ; i < kxprices.length; i++){ myproductInfo = new Product(); //必须这样在这里new String price = kxprices[i]; myproductInfo.setProduct( "开心吧" , kxproductNames[i], "null" , price, kxnumbers[i]); myProductDB.operateProduct(myproductInfo); //myHistoryDB.operateHProduct("lilin", types[i], myproductInfo); } String lyprices[] = { "30" , "20" , "15" , "30" , "90" , "50" }; String lyproductNames[] = { "井冈山" , "青原山" , "庐山" , "泰山" , "江郎山" , "老虎山" }; String lynumbers[] = { "2" , "2" , "2" , "2" , "2" , "2" }; //String types[] = {"1", "2", "1", "1", "3","2"}; for ( int i = 0 ; i < lyprices.length; i++){ myproductInfo = new Product(); //必须这样在这里new String price = lyprices[i]; myproductInfo.setProduct( "健康吧" , lyproductNames[i], "null" , price, lynumbers[i]); myProductDB.operateProduct(myproductInfo); //myHistoryDB.operateHProduct("lilin", types[i], myproductInfo); } String tgprices[] = { "30" , "20" , "15" , "30" , "90" , "50" }; String tgproductNames[] = { "IPhone5" , "u8500" , "z470" , "y480" , "诺基亚" , "诺亚信" }; String tgnumbers[] = { "1" , "2" , "3" , "4" , "5" , "6" }; //String types[] = {"1", "2", "1", "1", "3","2"}; for ( int i = 0 ; i < tgprices.length; i++){ myproductInfo = new Product(); //必须这样在这里new String price = tgprices[i]; myproductInfo.setProduct( "团购吧" , tgproductNames[i], "null" , price, tgnumbers[i]); myProductDB.operateProduct(myproductInfo); //myHistoryDB.operateHProduct("lilin", types[i], myproductInfo); } String toName[] = { "老张" , "老莫" , "老吴" , "老应" , "老周" }; String toAddr[] = { "31栋127" , "31栋130" , "31栋119" , "31栋110" , "31栋129" }; String toTel[] = { "18903067930" , "18103067930" , "18206067930" , "18307067430" , "18707067430" }; ToAddr myToAddr = new ToAddr(); for ( int i = 0 ; i < 5 ; i++ ){ myToAddr.setAddrAndTel( "lilin" , toName[i], toAddr[i], toTel[i], "false" , "false" ); myToAddrDB.operateToAddrDB(myToAddr); } } } |
一些全局变量的使用能减少好多代码。