Scala&Android下的开发trait经验分享
首先说明一下,我在android下是采用scala语言开发的。并不想讨论太多语言的好坏。只是把我开发时的经验与大家分享一下。
1 class ScalaAndroidActivity extends Activity with AdMobAdvertising with TestDataSource1 with TestDataSource2
大家先不要晕scala语言的继承关系是可以这样写的。with...
1、trait AdMobAdvertising
01 package org.noahx.common
02
03 import FindView._
04 import android.app.Activity
05 import android.widget.LinearLayout
06 import com.google.ads.AdView
07 import com.google.ads.AdSize
08 import com.google.ads.AdRequest
09 import android.os.Bundle
10
11 trait AdMobAdvertising extends Activity with FindView {
12
13 def adLinearLayout: LinearLayout
14
15 def adUnitId:String="a14xxxxxxxxxx"
16
17 lazy val adView = new AdView(this, AdSize.BANNER, adUnitId)
18
19 override def onCreate(savedInstanceState: Bundle) = {
20 super.onCreate(savedInstanceState)
21
22 adLinearLayout.addView(adView)
23 adView.loadAd(new AdRequest())
24 }
25
26 override def onDestroy() = {
27 if (adView != null) {
28 adView.destroy()
29 }
30 super.onDestroy()
31 }
32
33 }
以上就是我写的trait,这样我们可以不用太多考虑AdMob本身,只要实现adLinearLayout这个方法就可以了。来看看Activity www.zzzyk.com
01 package org.noahx.scalaandroid
02
03 import android.app.Activity
04 import android.os.Bundle
05 import android.widget.Button
06 import android.widget.TextView
07 import android.view.View
08 import org.noahx.common.FindView._
09 import android.widget.LinearLayout
10 import com.google.ads.AdView
11 import com.google.ads.AdSize
12 import com.google.ads.AdRequest
13 import org.noahx.common.AdMobAdvertising
14
15 class ScalaAndroidActivity extends Activity with AdMobAdvertising {
16
17 lazy val text = findView[TextView](R.id.text1)
18 lazy val button = findView[Button](R.id.button1)
19
20 override def adLinearLayout = findView[LinearLayout](R.id.adLinearLayout) //注意这里决定AdMob放在哪个LinearLayout中
21
22 override def onCreate(savedInstanceState: Bundle) = {
23 setContentView(R.layout.main) //这个要放在super之前
24
25 super.onCreate(savedInstanceState)
26
27 button.onClick { view: View =>
28 text.setText("hello scala1!!!")
29 }
30
31 }
32
33 }
用这样一个AdMobAdvertising的trait只要在想加入AdMob的Activity中with一下,指定一个LinearLayout就可以了。
是不是看上去很干净
当然如果你的adUnitId号是变动的也可以override掉,在Activity中加入如下代码
1 override def adUnitId="xxxxxxxxxxxxxxxx"
2、trait BaseDataSource
在做完上面的模式后我在想,是不是访问sqlite数据时也可以这样来写呢
我们先看看BaseDataSource
01 package org.noahx.common
02
03 import android.app.Activity
04 import android.database.sqlite.SQLiteDatabase
05 import android.database.sqlite.SQLiteOpenHelper
06 import android.os.Bundle
07
08 trait BaseDataSource extends Activity {
09
10 def getSQLiteOpenHelper(): SQLiteOpenHelper
11
12 val sqliteOpenHelper = getSQLiteOpenHelper()
13
14 var database: SQLiteDatabase = null
15
16 def getDatabase()=database
17
18 private def open() = {
19 database = sqliteOpenHelper.getWritableDatabase()
20 }
21
22 private def close() = {
23 database.close()
24 }
25
26 override def onCreate(savedInstanceState: Bundle) = {
27 super.onCreate(savedInstanceState)
28 open()
29 }
30
31 override def onResume() = {
32 open()
33 super.onResume()
34 }
35
36 override def onPause() {
37 close()
38 super.onPause()
39 }
40
41 override def onDestroy() = {
42 close()
43 super.onDestroy()
44 }
45
46 }
我们可以看到这个也是继承Activity,里面已经定义好了何时创建连接,何时关闭连接。缺少的只是getSQLiteOpenHelper没有实现
下面我们来看看SQLiteOpenHelper,这个目前本身没什么特点。
01 package org.noahx.scalaandroid
02 import android.database.sqlite.SQLiteOpenHelper
03 import android.database.sqlite.SQLiteDatabase.CursorFactory
04 import android.content.Context
05 import android.database.sqlite.SQLiteDatabase
06 import android.util.Log
07
08 class TestSQLiteOpenHelper(context: Context, dbName: String, cFactory: CursorFactory, ver: Int) extends SQLiteOpenHelper(context:Context, dbName: String, cFactory: CursorFactory, ver: Int) {
09
10 val databaseCreateTest1 = "create table test1 (id integer primary key autoincrement, name text not null)"
11
12 val databaseCreateTest2 = "create table test2 (id integer primary key autoincrement, name text not null)"
13
14 def this(context: Context) = {
15 this(context, "test.db", null, 1)
16 }
17
18 override def onCreate(database: SQLiteDatabase) = {
19 database.execSQL(databaseCreateTest1)
20 database.execSQL(databaseCreateTest2)
21 }
22
23 override def onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) = {
24 Log.w(classOf[TestSQLiteOpenHelper].getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data")
25 db.execSQL("DROP TABLE IF EXISTS " + "test1")
26 db.execSQL("DROP TABLE IF EXISTS " + "test2")
27 onCreate(db)
28
29 }
30 }
下面我们要基于BaseDataSource写自己的DataSource了,这才是重点
(scala语言的定义与文件名无关,可以一个.scala文件中写多个class,trait,object,甚至全部程序写一个.scala文件中)
01 package org.noahx.scalaandroid
02
03 import org.noahx.common.BaseDataSource
04 import scala.collection.mutable.ListBuffer
05 import android.content.ContentValues
06
07 trait TestBaseDataSource extends Bas
补充:移动开发 , Android ,