要把資料從DB讀出來加在ListView裡

可以用SimpleAdapter或SimpleCursorAdapter

前者要自己管理Cursor

後者只要設好資料來源就可以

而SimpleAdapter的好處可以對資料進行處理過後再放入list中

SimpleCursorAdapter

    	useDB.open();
    	useCursor = useDB.getListProject(ac);
    	startManagingCursor(useCursor);
    	
    	SimpleCursorAdapter adapter = new SimpleCursorAdapter(
    			this,
    			R.layout.listlayout,
    			useCursor,
    			new String[]{DB.KEY_PROJECT, DB.KEY_MODIFYDATE},
    			new int[]{R.id.txt_name, R.id.txt_date});
    	setListAdapter(adapter);
    	useDB.close();
    	getListView().setEmptyView(findViewById(R.id.txt_list_empty));

SimpleAdapter

    		useCursor = useDB.getProjectDetailByItem(pid);
    		useCursor.moveToFirst();
        	if(useCursor.getCount() > 0){
        		int one = useCursor.getColumnIndex(DB.KEY_OBJECT);
        		int two = useCursor.getColumnIndex("LAST");
        		int thr = useCursor.getColumnIndex("SUM");
        		int four = useCursor.getColumnIndex(DB.KEY_UNIT);
        		
                do {
                    Map map = new HashMap();
                    map.put(DB.KEY_OBJECT, useCursor.getString(one));
                    map.put("LAST", useCursor.getString(two));
                    map.put("SUM", useCursor.getString(thr)+useCursor.getString(four));
                    list.add(map);
                } while (useCursor.moveToNext());
                
                String[] fromDB = new String[]{DB.KEY_OBJECT, "LAST", "SUM"};
        		int[] toLayout = new int[]{R.id.txt_name, R.id.txt_date, R.id.txt_num};
            	
            	SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.listlayout, fromDB, toLayout);
            	setListAdapter(adapter);
        	}
    	useDB.close();
創作者介紹

PaaKJaT的日記本

PaaKJaT 發表在 痞客邦 PIXNET 留言(0) 人氣()