博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
谈中型项目下的编码技巧二
阅读量:5749 次
发布时间:2019-06-18

本文共 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);
        
}
    
}
}

一些全局变量的使用能减少好多代码。

本文转自lilin9105 51CTO博客,原文链接:http://blog.51cto.com/7071976/1232365,如需转载请自行联系原作者
你可能感兴趣的文章
OpenMediaVault 搭建git,ssh无法连接问题
查看>>
java多线程之:Java中的ReentrantLock和synchronized两种锁定机制的对比 (转载)
查看>>
【Web动画】SVG 实现复杂线条动画
查看>>
使用Wireshark捕捉USB通信数据
查看>>
《树莓派渗透测试实战》——1.1 购买树莓派
查看>>
Apache Storm 官方文档 —— FAQ
查看>>
iOS 高性能异构滚动视图构建方案 —— LazyScrollView
查看>>
Java 重载、重写、构造函数详解
查看>>
【Best Practice】基于阿里云数加·StreamCompute快速构建网站日志实时分析大屏
查看>>
【云栖大会】探索商业升级之路
查看>>
HybridDB实例新购指南
查看>>
C语言及程序设计提高例程-35 使用指针操作二维数组
查看>>
华大基因BGI Online的云计算实践
查看>>
深入理解自定义Annotation,实现ButterKnif小原理
查看>>
排序高级之交换排序_冒泡排序
查看>>
Cocos2d-x3.2 Ease加速度
查看>>
[EntLib]关于SR.Strings的使用办法[加了下载地址]
查看>>
中小型网站架构分析及优化
查看>>
写shell的事情
查看>>
负载均衡之Haproxy配置详解(及httpd配置)
查看>>