Core Data浅谈系列之一 : 基础结构
实现代码如下:
1、main_activity.xml,MainActivity使用的xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dp" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:id="@+id/tvProvince" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="省份" /> <Spinner android:id="@+id/spnProvince" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:dropDownSelector="@android:layout/simple_spinner_dropdown_item" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:id="@+id/tvProvinceInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="地区" /> <Spinner android:id="@+id/spnProvinceInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:dropDownSelector="@android:layout/simple_spinner_dropdown_item" /> </LinearLayout> </LinearLayout>2、main_spinner_item,用于显示Spinner的每一项
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > </TextView>3、MainActivity
package forrest.forprovince; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import forrest.forprovince.db.MySQLiteDatabase; import forrest.forprovince.db.MySQLiteOpenHelper; public class MainActivity extends Activity implements OnItemSelectedListener { private Spinner spnProvince; private Spinner spnProvinceInfo; private boolean isFirst = true; private ArrayAdapter<String> spnInfoAdapter; private List<String> areas = new ArrayList<String>(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); // 将城市信息,写入数据库 MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this); helper.getWritableDatabase(); helper.close(); init(); setAdapter(); setListener(); } /** 设置适配器 */ private void setAdapter() { ArrayAdapter<CharSequence> spnAdapter = ArrayAdapter.createFromResource(this, R.array.province, R.layout.main_spinner_item); spnProvince.setAdapter(spnAdapter); } /** 设置监听器 */ private void setListener() { spnProvince.setOnItemSelectedListener(this); } /** 获取控件 */ private void init() { spnProvince = (Spinner) findViewById(R.id.spnProvince); spnProvinceInfo = (Spinner) findViewById(R.id.spnProvinceInfo); } /**注意:例如,现在已经选择了北京,如果再次点开Spinner,选择北京,不会调用此方法*/ public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // 这里要用parent,而不能用view.getId(); 因为parent才是Spinner switch (parent.getId()) { case R.id.spnProvince: System.out.println("----------" + position); MySQLiteDatabase sqLite = new MySQLiteDatabase(this); sqLite.getAerasList(position + "", areas); // 如果是第一次,则设置适配器,以后就更新数据 if (isFirst) { spnInfoAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.main_spinner_item, areas); spnProvinceInfo.setAdapter(spnInfoAdapter); isFirst = false; } else { spnInfoAdapter.notifyDataSetChanged(); } sqLite.closeDB_Cursor(); break; case R.id.spnProvinceInfo: break; } } public void onNothingSelected(AdapterView<?> parent) { } }上面需要注意的是:notifyDataSetChanged
(1)、new一个Adapter时要设置它显示的数据源,而这个数据源所存放的地址,会被对应的adapter监听,如果数据发生变化,调用notifyDataSetChanged方法,有效。
(2)、如果,重新new了一个数据源,此时的地址已经不再是以前那个,再调用notifyDataSetChanged方法,无效。
(3)、自定义Adapter,也适用以上两点,但是可以写一个设置数据源的方法,重新设置Adapter的数据源,此时无论是否还是以前的地址,调用notifyDataSetChanged方法,都有效。
public class AppManagerAdapter extends BaseAdapter { private List<AppPackageInfo> data; // 指向需要显示的数据 /**更改Adapter中的数据源*/ public void setData(List<AppPackageInfo> data) { this.data = data; }
Activity中的代码
adapter.setData(data); adapter.notifyDataSetChanged();另外可参考:http://www.cnblogs.com/xesam/archive/2012/05/31/2529056.html
4、MySQLiteOpenHelper
package forrest.forprovince.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MySQLiteOpenHelper extends SQLiteOpenHelper { private static final int VERSION = 1; private static final String NAME = "Province"; public MySQLiteOpenHelper(Context context) { super(context, NAME, null, VERSION); } /**通过21行的输出语句可知,onCreate方法只会执行一次*/ public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE IF NOT EXISTS Province (_id integer primary key autoincrement, area text)"); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{0, "请选择"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{1, "安庆市,蚌埠市,亳州市,巢湖市,池州市,滁州市,阜阳市,合肥市,淮北市,淮南市,黄山市,六安市,马鞍山市,宿州市,铜陵市,芜湖市,宣城市"}); System.out.println("SQLiteDatabase------------------------"); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{2, "昌平区,朝阳区,大兴区,房山区,海淀区,怀柔区,门头沟区,密云县,平谷区,石景山区,顺义区,通州区,西城区,宣武区,延庆县"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{3, "巴南区 ,北碚区,长寿区,达州市,涪陵区,江北区,九龙坡区,南岸区,黔江区,沙坪坝区,双桥区,万盛区,万州区,渝北区,渝中区,璧山县,城口县,大足县,垫江县,丰都县,奉节县,合川市,江津市,开县,梁平县,南川市,彭水苗族土家族自治县,荣昌县,石柱土家族自治县,铜梁县,巫山县,巫溪县,武隆县,秀山土家族苗族自治县,永川市,酉阳土家族苗族自治县,云阳县,忠县"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{4, "城厢区,福州市,涵江区,荔城区,龙岩市,南平市,宁德市,莆田市,泉州市,三明市,秀屿区,秀屿区,漳州市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{5, "白银市,定西市,甘南州,海南州,金昌市,酒泉市,兰州市,临夏州,陇南市,平凉市,庆阳市,天水市,武威市,张掖市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{6, "潮州市,东莞市,佛山市,广州市,河源市,惠州市,江门市,揭阳市,茂名市,梅州市,清远市,汕头市,汕尾市,韶关市,深圳市,阳江市,云浮市,湛江市,肇庆市,中山市,珠海市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{7, "百色市,北海市,,防城港市,贵港市,桂林市,河池市,贺州市,来宾市,柳州市,南宁市,钦州市,梧州市,玉林市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{8, "安顺市,毕节地区,贵阳市,六盘水市,黔东南苗族侗族自治州,黔南布依族苗族自治州,黔西南布依族苗族自治州,铜仁地区,遵义市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{9, "海口市,海南沿革,三亚市,大渡口区"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{10, "保定市,沧州市,承德市,邯郸市,衡水市,廊坊市,秦皇岛市,石家庄市,唐山市,邢台市,张家口市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{11, "安阳市,鹤壁市,焦作市,开封市,洛阳市,漯河市,南阳市,平顶山市,濮阳市,三门峡市,商丘市,新乡市,信阳市,许昌市,郑州市,周口市,驻马店市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{12, "大庆市,大兴安岭地区,哈尔滨市,鹤岗市,黑河市,鸡西市,佳木斯市,牡丹江市,七台河市,齐齐哈尔市,双鸭山市,绥化市,伊春市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{13, "鄂州市,恩施土家族苗族自治州,黄冈市,黄石市,荆门市,荆州市,十堰市,随州市,武汉市,咸宁市,襄樊市,孝感市,宜昌市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{14, "长沙市,常德市,郴州市,衡阳市,怀化市,娄底市,邵阳市,天门市,湘潭市,湘西土家族苗族自治州,益阳市,永州市,岳阳市,张家界市,株洲市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{15, "白城市,白山市,长春市,吉林市,辽源市,四平市,松原市,通化市,延边朝鲜族自治州"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{16, "常州市,淮安市,连云港市,南京市,南通市,苏州市,宿迁市,泰州市,无锡市,徐州市,盐城市,扬州市,镇江市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{17, "抚州市,赣州市,吉安市,景德镇市,九江市,南昌市,萍乡市,上饶市,新余市,宜春市,鹰潭市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{18, "鞍山市,本溪市,朝阳市,大连市,丹东市,抚顺市,阜新市,葫芦岛市,锦州市,辽阳市,盘锦市,沈阳市,铁岭市,营口市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{19, "阿拉善盟,巴彦淖尔市,包头市,赤峰市,鄂尔多斯市,呼和浩特市,呼伦贝尔市,通辽市,乌海市,乌兰察布市,锡林郭勒盟,兴安盟"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{20, "固原市,石嘴山市,吴忠市,博尔塔拉州,中卫市,银川市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{21, "果洛州,海北州,海东地区,银川市,海西州,黄南州,西宁市,玉树州"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{22, "滨州市,德州市,东营市,菏泽市,济南市,济宁市,莱芜市,聊城市,临沂市,青岛市,日照市,泰安市,威海市,潍坊市,烟台市,枣庄市,淄博市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{23, "长治市,大宁县,大同市,晋城市,晋中市,临汾市,吕梁市,朔州市,太原市,忻州市,阳泉市,运城市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{24, "安康市,宝鸡市,汉中市,嘉峪关市,铜川市,渭南市,西安市,咸阳市,延安市,榆林市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{25, "宝山区,长宁区,崇明县,奉贤区,虹口区,黄浦区,嘉定区,金山区,静安区,卢湾区,闵行区,南汇区,浦东新区,普陀区,青浦区,松江区,徐汇区,杨浦区,闸北区"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{26, "阿坝藏族羌族自治州,巴中市,成都市,六盘水市,德阳市,甘孜藏族自治州,广安市,广元市,乐山市,凉山彝族自治州,泸州市,眉山市,绵阳市,内江市,南充市,攀枝花市,遂宁市,雅安市,宜宾市,资阳市,自贡市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{27, "和平区,河东区,河西区,南开区,河北区,红桥区,塘沽区,汉沽区,大港区,东丽区,西青区,津南区,北辰区,武清区,宝坻区,宁河县,静海县,蓟县"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{28, "阿里地区,昌都地区,拉萨市,商洛市,那曲地区,日喀则地区,山南地区"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{29, "阿克苏地区,阿勒泰地区,巴音郭楞州,观塘区,昌吉州,哈密地区,和田地区,喀什地区,克拉玛依市,克孜勒苏州,塔城地区,吐鲁番地区,乌鲁木齐市,伊犁州"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{30, "保山市,楚雄彝族自治州,大理白族自治州,德宏傣族景颇族自治州,迪庆藏族自治州,红河哈尼族彝族自治州,昆明市,丽江市,临沧市,怒江傈傈族自治州,曲靖市,思茅市,文山壮族苗族自治州,西双版纳傣族自治州,玉溪市,昭通市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{31, "杭州市,湖州市,嘉兴市,金华市,丽水市,宁波市,衢州市,绍兴市,台州市,温州市,舟山市"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{32, "台湾"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{33, "香港"}); db.execSQL("INSERT INTO Province (_id, area) VALUES(?, ?)", new Object[]{34, "澳门"}); } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }5、MySQLiteDatabase
package forrest.forprovince.db; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class MySQLiteDatabase { private SQLiteDatabase db; private Cursor cursor; private MySQLiteOpenHelper mHelper; public MySQLiteDatabase(Context mContext) { mHelper = new MySQLiteOpenHelper(mContext); db = mHelper.getReadableDatabase(); } public void getAerasList(String _id, List<String> areasList) { // 查询相应_id的信息 cursor = db.rawQuery("SELECT * FROM Province WHERE _id=?", new String[] { _id }); // 清空List areasList.clear(); // 为List添加数据 if (cursor.moveToFirst()) { String areaStr = cursor.getString(cursor.getColumnIndex("area")); String[] areasStr = areaStr.split(","); for (String area : areasStr) { areasList.add(area); } } } /** close DataBase */ public void closeDB() { if (mHelper != null) { mHelper.close(); mHelper = null; } } /** close DataBase,Cursor */ public void closeDB_Cursor() { if (cursor != null) { cursor.close(); cursor = null; } if (mHelper != null) { mHelper.close(); mHelper = null; } } }6、arrays.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="province"> <item>请选择</item><item>安徽</item><item>北京</item><item>重庆</item><item>福建</item><item>甘肃</item><item>广东</item><item>广西</item><item>贵州</item><item>海南</item><item>河北</item><item>河南</item><item>黑龙江</item><item>湖北</item><item>湖南</item><item>吉林</item><item>江苏</item><item>江西</item><item>辽宁</item><item>内蒙</item><item>宁夏</item><item>青海</item><item>山东</item><item>山西</item><item>陕西</item><item>上海</item><item>四川</item><item>天津</item><item>西藏</item><item>新疆</item><item>云南</item><item>浙江</item><item>台湾</item><item>香港</item><item>澳门</item> </string-array> </resources>
完整代码:http://download.csdn.net/detail/wo334499/5025524
Objective-C 2.0增加了class extensions用于解决两个问题:
允许一个对象可以拥有一个私有的interface,且可由编译器验证。
支持一个公有只读,私有可写的属性。私有接口(Private Interface)
Objective-C 2.0以前, 若要定义私有函数,通常是在实现文件中声明一个"Private" category:
@interface MyClass (Private)
- (id)awesomePrivateMethod;
@end
然而, 类的私用方法通常是希望实现在类的@implementation块中的,而不是像上面的Category的方法那样实现在独立的@implementation区块中。事实上,Category仅仅是弥补了Objective-C缺少public/private限定的不足。
真正的问题是Objective-C编译器会认为在Category中声明的方法将会在别处实现,所以编译器并不会尝试确认它们是不是真得都被实现了。也就是说,开发者声明的方法有可能并未实现,而且编译器也不会有什么警告。编译会以为它们将在别的地方或独立的文件中实现。
使用class exteionsion,在其中声明的方法和属性的实现将放在class的@implementation区块中。否则,编译器就会报错。// someClass.m
@interface someClass ()
-(void)extend;
@end
@implementation someClass
// 所有声明在头文件或父类中方法的实现
// 或者一些私有函数
-(void)extend {
// implement private method here;
}
@end
公有可读、私有可写的属性(Publicly-Readable, Privately-Writeable Properties)
实现一个不可变(immutable)的数据结构通常有一个好处是外部代码不能用setter修改对象的状态。然而,可能又希望它在内部又是一个可写的属性。Class extensions可以做到这一点:在公共接口(类的声明中)中,开发者可以声明一个属性是只读的,随后在类扩展中声明为可写。这样,对外部代码而言,该属性将是只读的,而内部代码却可以使用它setter方法。
@interface MyClass : NSObject
@property (retain, readonly) float value;
@end
// 私有的extension, 隐藏在主实现文件中.
@interface MyClass ()
@property (retain, readwrite) float value;
@end
看起来相似,其实不同
Class extension常常被误解为一个匿名的category。它们的语法的确很相似。虽然都可以用来为一个现有的类添加方法和属性,但它们的目的和行为却是不同的。
游戏开发论坛:http://jiushun8.com/forum.php?mod=viewthread&tid=2833&extra=page%3D1