更新UI
开发遇到的问题:显示实时时间
解决方法: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
27private Handler handleTime;
private TextView third_text;
third_text = (TextView)findViewById(R.id.third_text);
handleTime = new Handler(){
public void handleMessage(android.os.Message msg) {
third_text.setText((String)msg.obj);
};
};
Thread threadDate = new Thread(new Runnable() {
GetDateTime getDateTime;
public void run() {
// TODO Auto-generated method stub
while(true){
getDateTime = new GetDateTime();
time = getDateTime.dealDate();
handleTime.sendMessage(handleTime.obtainMessage(100,time));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
threadDate.start();
图表按钮之GridView
pictureModel.java类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
46public class PictureModel {
private String title;
private int imageID;
/**
* 默认构造函数
*/
public PictureModel(){}
/**
*
* @param 图像标注
* @param 图像ID
*/
public PictureModel(String title,int imageID){
this.title = title;
this.imageID = imageID;
}
/**
*
* @return 返回图像标注
*/
public String getTitle() {
return title;
}
/**
*
* @param 设置图像标注
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* @return 返回图像ID
*/
public int getImageID() {
return imageID;
}
/**
*
* @param 设置图像ID
*/
public void setImageID(int imageID) {
this.imageID = imageID;
}
}
pictureAdapter.java类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
51public class PictureAdapter extends BaseAdapter{
private LayoutInflater inflater;
private List<PictureModel> pictures;
public PictureAdapter(String[] titles,int[] imageIDs,Context context){
super();
inflater = LayoutInflater.from(context);
pictures = new ArrayList<PictureModel>();
for(int i = 0;i<titles.length;i++){
PictureModel picture = new PictureModel(titles[i],imageIDs[i]);
pictures.add(picture);
}
}
public int getCount() {
// TODO Auto-generated method stub
if(pictures!=null){
return pictures.size();
}else{
return 0;
}
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return pictures.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
class ViewHolder{
public TextView text;
public ImageView image;
}
ViewHolder viewHolder = new ViewHolder();
if(convertView == null){
convertView = inflater.inflate(R.layout.picture_item, null);
viewHolder.text = (TextView) convertView.findViewById(R.id.ItemTitle);
viewHolder.image = (ImageView) convertView.findViewById(R.id.ItemImage);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(pictures.get(position).getTitle());
viewHolder.image.setImageResource(pictures.get(position).getImageID());
return convertView;
}
}
实现类,把view填充到GridView中1
2
3
4
5
6
7
8
9
10
11String[] titles = new String[6];
int[] images = new int[6];
images[0] = R.drawable.icon0;
images[1] = R.drawable.icon1;
images[2] = R.drawable.icon2;
images[3] = R.drawable.icon3;
images[4] = R.drawable.icon4;
images[5] = R.drawable.icon5;
gvInfo = (GridView) findViewById(R.id.gv_Info);
PictureAdapter adapter = new PictureAdapter(titles, images, this);
gvInfo.setAdapter(adapter);
构建Activity
1 | super.onCreate(savedInstanceState);//开始窗体的生命周期 |
总结(1)
做了一天,进去看了很多源码,最终看到的是Context,View,Adapter。就这三个,说一下目前的了解,Context在我看来就是像浏览器引擎的上下文context,可以通过它做很多东西。而View大多是一个view组件,像xml文件里面的组成都是view。而adapter则可以做一个view然后填充到另一个view。或许三者合在一起就有了一个这么个窗体。