2015-12-18

[Golang] 用Go (golang) 寫Web Server! 使用go-restful package. Hello go-restful!

1. 安裝go
https://golang.org/doc/install
https://golang.org/dl/

    *如果有安裝舊版的要先移除
    *Mac版的pkg只要抓下來執行一直按下一步就安裝好。
        他會幫你把go安裝在/usr/local/go,
        並把/usr/local/go/bin加到環境變數PATH當中。
        記得要把Terminal關掉重開才會吃到新的PATH。

2.  準備一個HelloWebServer的Workspace
        1. 新增一個資料夾當作這個程式的Workspace, ex. ~/HelloWebServer/
        2. 把這個folder path加入環境變數,
             ex. 執行 $ export GOPATH=$HOME/HelloWebServer
        3. 在work folder底下加入src/HelloWebServer/hellowebserver.go
            內容為

2015-11-12

[Mokito] 使用Mokito寫測試的筆記 (Note for writing test cases by Mokito)


Test Class Template
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import static android.os.Build.VERSION_CODES.LOLLIPOP;

@Config(sdk = LOLLIPOP, constants = BuildConfig.class)
@RunWith(RobolectricGradleTestRunner.class)
public class MyTest {

    // something to mock
    @Mock TypeA A;
    @Spy TypeB B = new TypeB();
    // ...

    // Set up
    @Before public void setUp() throws Exception {
        initMocks(this);    // create mock objects, ex. A and B above
        // ... do so other init things
    }

    // Tear down
    @After public void tearDown() {
        // ... do so tear down things
    }

    // Test Case (which should not throw exception)
    @Test public void testCase1() throws Exception {
        // do something
    }

    // Test Case (expect exception)
    @Test(expected=YouExceptException.class) public void testException1() {
       // something would throw YouExceptException.class exception
    }

    // Test Case (expect exception)
    @Test public void testException2() throws Exception {
        try {
            // something should throw exception
            // import org.assertj.core.api.Assertions;
            Assertions.failBecauseExceptionWasNotThrown(YouExpectException.class);
        } catch (YouExpectException e) {
        }
        // verify other things here
    }

    // Test Case (with timeout)
    @Test(timeout=100) public void infinity() {
       // something should not run longer than 100ms
    }
    
}

2015-11-02

[Android][RxJava] Run something async on non-UI thread, then back to MainThread to do something after that.


當你有某些東西要Async地在background、而且是non-UI thread運作
(例如:Android要連接網路Network、http之類的時候)

但是做完又要回到Main Thread
(例如:從網路or anywhere拿到資料之後要更新UI)

就可以用以下的Pattern來寫


Async.fromCallable(new Callable() {
    // T is the type you want to send back to MainThread
    @Override public T call() throws Exception {
        // do something that you want it async run on Schedulers.io()
        return [object in type T];
    }
}, Schedulers.io())  
// specify the thread you want async work to run on it, here we use Schedulers.io()

    .observeOn(AndroidSchedulers.mainThread())  // then back to MainThread
    .subscribe(new Subscriber() {
        // run on MainThread
        @Override public void onCompleted() {

        }

        @Override public void onError(Throwable e) {

        }

        @Override public void onNext(T obj) {

        }
    });

2015-10-29

[Android] an Text search Adapter for AppCompatAutoCompleteTextView or AutoCompleteTextView

這是一個可以自動完成的TextView(AutoCompleteTextView)所需要的Adapter

原本的ArrayAdapter的自動完成只會搜尋到以已輸入的字串為開頭的字串。
(因為ArraryAdapter的Filter是用startWith去找字串)
例如:List當中有"安全"、"安心"、"保安"、"偏安"、"討厭"
輸入"安",只會搜尋出"安全"、"安心"
而"保安"、"偏安"就搜尋不到
"討厭"也是一樣搜尋不到

以下這個改寫過的Adapter則是只要有包含已輸入的字串都會顯示出來。
例如:List當中有"安全"、"安心"、"保安"、"偏安"、"討厭"
輸入"安",會搜尋出所有有"安"的String"安全"、"安心"、"保安"、"偏安"
而"討厭"裡面沒有"安"所以就搜尋不到



Usage:
AppCompatAutoCompleteTextView mAutoCompleteText;
List<String> list;

// find view by id
// ...

mAutoCompleteText.setAdapter(new AutoCompleteAdapter(getContext(),
            android.R.layout.simple_spinner_item, list));


2015-09-11

[Android] 如何改變DatePicker的Divider的顏色 (How to change the divider color of the DatePicker)


這是一個可以自己從xml指定Divider的顏色的DatePicker

可以把像下圖這樣的Spinner DatePicker的Divider從藍色換成別的自己指定的顏色
We can change the divider color of DatePicker from blue to any other color you want.



首先要先在resource的drawable當中加入一個shape
把"THE_COLOR_YOU_WANT"換成你想要的顏色