Scala & amp; Android development trait Experience Sharing

Source: Internet
Author: User
Tags admob

First, I developed it in scala language on android. I don't want to discuss the quality of many languages. I just want to share my development experience with you.
1 class ScalaAndroidActivity extends Activity with AdMobAdvertising with TestDataSource1 with TestDataSource2
Do not confuse the inheritance relationships of 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}
The above is the trait I wrote, so we don't need to think too much about AdMob itself, as long as we can implement the adLinearLayout method. Let's see Activity www.2cto.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) // determine the LinearLayout in which AdMob is placed.
21
22 override def onCreate (savedInstanceState: Bundle) = {
23 setContentView (R. layout. main) // before super
24
25 super. onCreate (savedInstanceState)
26
27 button. onClick {view: View =>
28 text. setText ("hello scala1 !!! ")
29}
30
31}
32
33}
To use such an AdMobAdvertising trait, you only need to specify a LinearLayout in the Activity that you want to add to AdMob.
Does it look clean?
Of course, if your adUnitId number is changed, you can override it. Add the following code to the Activity:
1 override def adUnitId = "xxxxxxxxxxxxxxxx"
 
2. trait BaseDataSource
After completing the above model, I was wondering if I could write data like this when accessing sqlite data.
Let's first look at 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}
We can see that this is also an inherited Activity, which defines when to create a connection and when to close the connection. What is missing is that getSQLiteOpenHelper is not implemented.
Next let's take a look at SQLiteOpenHelper, which has no features at present.
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.exe cSQL (databaseCreateTest1)
20 database.exe cSQL (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 + ", whwill destroy all old data ")
25 db.exe cSQL ("drop table if exists" + "test1 ")
26 db.exe cSQL ("drop table if exists" + "test2 ")
27 onCreate (db)
28
29}
30}
Now we need to write our own DataSource Based on BaseDataSource. This is the key
(The definition of scala language has nothing to do with the file name. You can write multiple classes, trait, object, or even all programs in A. scala file in one. scala file)
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 BaseDataSource {
08
09 def getSQLiteOpenHelper () = new TestSQLiteOpenHelper (this)
10
11}
12
13 trait testperformance1 extends TestBaseDataSource {
14 private val tableName = "test1"
15
16 def createTest1 (name: String) = {
17 val values = new ContentValues ()
18 values. put ("name", name)
19 val insertId = database. insert (tableName, null, values)
20}
21
22 def getTest1s (): List [String] = {
23 var words = new ListBuffer [String]
24
25 val cursor = database. query (tableName, Array ("name"), null)
26
27 cursor. moveToFirst ()
28 while (! Cursor. isAfterLast ()){
29 words + = cursor. getString (0)
30 cursor. moveToNext ()
31}
32 cursor. close ()
33
34 words. toList
35}
36}
37
38 trait TestDataSource2 extends TestBaseDataSource {
39 private val tableName = "test2"
40
41 def createTest2 (name: String) = {
42 val values = new ContentValues ()
43 values. put ("name", name)
44 val insertId = database. insert (tableName, null, values)
45}
46
47 def getTest2s (): List [String] = {
48 var words = new ListBuffer [String]
49
50 val cursor = database. query (tableName, Array ("name"), null)
51
52 cursor. moveToFirst ()
53 while (! Cursor. isAfterLast ()){
54 words + = cursor. getString (0)
55 cursor. moveToNext ()
56}
57 cursor. close ()
58
59 words. toList
60}
61}
First, TestBaseDataSource is defined to set helper in a unified manner. Then, testcece1 and TestDataSource2 can directly use the database object.
Let's take a look at the final Activity.
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 import android. util. Log
15
16 class ScalaAndroidActivity extends Activity with AdMobAdvertising with TestDataSource1 with TestDataSource2 {
17
18 lazy val text = findView [TextView] (R. id. text1)
19 lazy val button = findView [Button] (R. id. button1)
20
21 override def adLinearLayout = findView [LinearLayout] (R. id. adLinearLayout)
22
23 override def onCreate (savedInstanceState: Bundle) = {
24 setContentView (R. layout. main)
25
26 super. onCreate (savedInstanceState)
27
28 for (I <-0 to 10 ){
29 createTest1 ("test1." + I)
30 createTest2 ("test2." + I)
31}
32
33 button. onClick {view: View =>
34 text. setText ("hello scala !!! ")
35
36 Log. I (classOf [ScalaAndroidActivity]. getName (), getTest1s (). toString ())
37 Log. I (classOf [ScalaAndroidActivity]. getName (), getTest2s (). toString ())
38}
39
40}
41
42}
The effect is that if testcece1 is used for an Activity, it is directly added to the parent behavior. In this Activity, you can directly call methods such as createTest1 to directly operate the database.
Write the following code to the database:
1 for (I <-0 to 10 ){
2 createTest1 ("test1." + I)
3 createTest2 ("test2." + I)
4}
OnClick is output by Log
1 Log. I (classOf [ScalaAndroidActivity]. getName (), getTest1s (). toString ())
2 Log. I (classOf [ScalaAndroidActivity]. getName (), getTest2s (). toString ())
The result of background printing is as follows:

Author: Xiao guoying

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.