package com.milan.inzaghi09.engine;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CommonNumberDao {
	
	
	private String path;
	/**
	 * 查询常用号码的Group
	 * @param ctx 上下文
	 * @return 返回封装了Group的集合
	 */
	public List<Group> getGroup(Context ctx) {
		
		path = ctx.getFilesDir() + File.separator + "commonnum.db";

		// 1获取数据库操作对象
		SQLiteDatabase  db = SQLiteDatabase.openDatabase(path, null,SQLiteDatabase.OPEN_READONLY);
		// 2查询
		List<Group> groupList = new ArrayList<Group>();
		Cursor cusor = db.query("classlist", new String[]{"name","idx"}, null, null, null, null, null);
		while (cusor.moveToNext()) {
			Group group = new Group();
			group.name = cusor.getString(0);
			group.idx = cusor.getString(1);
			group.childList = getChild(group.idx);
			groupList.add(group);
		}
		
		cusor.close();
		db.close();
		return groupList;
	}
	
	/**查询常用号码的Child
	 * @param idx 常用号码classlist表的idx列
	 * @return 返回封装了Child的集合
	 */
	public List<Child> getChild(String idx) {
//		数据库操作对象要重新获取，因为在getGroup方法中关闭了数据库，如果复用会导致异常
		SQLiteDatabase  db = SQLiteDatabase.openDatabase(path, null,SQLiteDatabase.OPEN_READONLY);
		// 1查询
		List<Child> childList = new ArrayList<Child>();
		Cursor cusor = db.query("table"+idx, new String[]{"_id","number","name"}, null, null, null, null, null);
		while (cusor.moveToNext()) {
			Child child = new Child();
			child._id = cusor.getString(0);
			child.number = cusor.getString(1);
			child.name =  cusor.getString(2);
			
			childList.add(child);
		}
		
		cusor.close();
		db.close();
		return childList;
	}
	
	public class Group{
		public String name;
		public String idx;
		public List<Child> childList;
	}
	public class Child{
		public String _id;
		public String number;
		public String name;
	}

}

@@ -1158,12 +1158,14 @@
  %7B%0D%0A
-//
 %09%09
+// 1
 %E6%95%B0%E6%8D%AE%E5%BA%93%E6%93%8D
@@ -1214,33 +1214,32 @@
 %09SQLiteDatabase 
- 
 db = SQLiteDatab
@@ -1258,32 +1258,38 @@
 base(path, null,
+%0D%0A%09%09%09%09
 SQLiteDatabase.O
@@ -1309,17 +1309,17 @@
 ;%0D%0A%09%09// 
-1
+2
 %E6%9F%A5%E8%AF%A2%0D%0A%09%09Li

add annotation