ListView小用法
列表标题:照片名,编号,照片得分
根据ArrayList<Photo> photos 依次添加列表项。关键点设置为adapter的六个参数,并且用getData()方法得到 List<HashMap<Object, Object>>类型的对象
1 package cqu.wm;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.List;
7 import tool.Photo;
8 import tool.PhotoService;
9 import android.app.ListActivity;
10 import android.os.Bundle;
11 import android.os.Environment;
12 import android.widget.SimpleAdapter;
13
14 public class myListActivity extends ListActivity {
15
16 /** Called when the activity is first created. */
17 public static final String TAG = "dbtest";
18 PhotoService photoservice = null;
19 ArrayList<Photo> photos = new ArrayList<Photo>();
20
21 @Override
22 public void onCreate(Bundle savedInstanceState) {
23
24 super.onCreate(savedInstanceState);
25
26 photoservice = new PhotoService(this);
27 initiallize();
28 SimpleAdapter adapter = new SimpleAdapter(this, getData(),
29 R.layout.listview, new String[] { "id", "name", "score" },
30 new int[] { R.id.idview, R.id.nameview, R.id.scoreview });
31 setListAdapter(adapter);
32
33 }
34
35 private void initiallize() {
36 File[] dir = new File(Environment.getExternalStorageDirectory()
37 .getPath() + "/DCIM").listFiles();
38 File[] photolist = null;
39 for (int i = 0; i < dir.length; i++) {
40 if (dir[i].getName().startsWith("100")) {
41 photolist = dir[i].listFiles();
42 break;
43 }
44 }
45 for (File file : photolist) {
46 Photo photo = new Photo(file.getName(), 100);
47 photos.add(photo);
48 }
49 photoservice.saveAll(photos);
50 photoservice.find("ali_washing.jpg");
51 photoservice.save(new Photo(1, "ali_help.jpg", 100));
52 photoservice.update(new Photo(1, "ali_help.jpg", 20));
53 photoservice.delete(photoservice.find("ali_help.jpg"));
54 photoservice.clear();
55
56 }
57
58 private List<HashMap<String, String>> getData() {
59
60 List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
61 HashMap<String, String> title = new HashMap<String, String>();
62 title.put("id", "编号");
63 title.put("name", "照片名");
64 title.put("score", "照片得分");
65 data.add(title);
66 for (Photo photo : photos) {
67 HashMap<String, String> map = new HashMap<String, String>();
68 map.put("id", String.valueOf(photo.getId()));
69 map.put("name", photo.getName());
70 map.put("score", String.valueOf(photo.getScore()));
71 data.add(map);
72 }
73 return data;
74 }
75 }
-------------------------------~问世间情为何物,敲敲代码停不住~
-------------------------------