2015年8月3日 星期一

[Android] ActionBar 顯示 app icon

API 21 之後預設移除 app icon。

android.support.v7.widget.Toolbar

The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.

android.support.v7.app.ActionBar

When using the Material themes (default in API 21 or newer) the navigation button (formerly "Home") takes over the space previously occupied by the application icon.

設定 app icon :

protected void onCreate(Bundle savedInstanceState) {
   ActionBar actionBar = getSupportActionBar();
   actionBar.setLogo(R.drawable.ic_laucher);
   actionBar.setDisplayUseLogoEnabled(true);
   actionBar.setDisplayShowHomeEnabled(true);
}

⚠ icon 需要在 drawable 目錄,不能使用 mipmap。
⚠ 3 個方法都要寫。

參考資料:StackOverflow: The application icon does not show on action bar

Android Studio 1.3
    compileSdkVersion 22
    buildToolsVersion "23.0.0 rc3"

    com.android.support:appcompat-v7:22.2.+

2015年7月28日 星期二

[Android] ScrollView 嵌套 ListView

預設 ListView 只會顯示一個子項,在 ScrollView 添加屬性即可:

android:fillViewport="true"

參考資料:http://www.oschina.net/question/163910_27133



⚠ 實際測試只有 ListView 也可以捲動。

此外,ScrollView 搭配 SwipeRefreshLayout 會發生任何時候下拉都會更新。 但是拿掉 ScrollView ,就只有在最頂端的時候下拉才會更新。

<android.support.v4.widget.SwipeRefreshLayout ...>
   <ScrollView ...>
      <ListView ...>
      </ListView>
   </ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

Android Studio 1.2.1.1
    compileSdkVersion 22
    buildToolsVersion "23.0.0 rc3"

    com.android.support:support-v4:22.2.+

2015年7月24日 星期五

[Android Studio] 使用 Lombok

Lombok 是在 POJO 寫些注解,然後在編譯時期幫忙產生冗余代碼。

在 build.gradle 設置:

dependencies {
    ...
    compile 'org.projectlombok:lombok:1.16.+'
}

在 Android Studio 編譯時,會出現:

    error: package javax.annotation does not exist
查過網路資料,需要先安裝 Lombok Plugin,再在 Project 根目錄建立 lombok.config,並且設定:
lombok.addGeneratedAnnotation = false

參考資料:https://projectlombok.org/setup/android
參考資料:StackOverflow: How to set up compile library in android studio. LOMBOK

Android Studio 1.2.1.1
    compileSdkVersion 22
    buildToolsVersion "23.0.0 rc3"

[Android] ActiveAndroid 入門

ActiveAndroid 是輕量型 ORM 可以存取 SQLite。 只要幾個步驟就可以簡單使用。

在 build.gradle 設置:

repositories {
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}

dependencies {
    ...
    compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'
}

在 AndroidManifest.xml 指定啓動點:

<manifest ...>
    <application android:name="com.activeandroid.app.Application" ...>
    </application>
</manifest>

定義 POJO:

@Table(name = "USER")
public class User extends Model
{
    @Column(name = "NAME")
    private String name;

    public User() {}

    public User(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }
}
List<User> data = new Select().from(User.class).execute();

⚠ 一定要有預設建構子。
⚠ #execute() 會回傳 null 值。



如果出現以下錯誤,

Suppressed: java.lang.ClassNotFoundException: android.support.v4.util.LruCache
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
        ... 17 more
在 build.gradle 設定:
    compile 'com.android.support:appcompat-v7:22.2.+'

參考資料:https://github.com/pardom/ActiveAndroid

Android Studio 1.2.1.1
    compileSdkVersion 22
    buildToolsVersion "23.0.0 rc3"

2015年1月9日 星期五

[PHP] is_array() vs. identical (===)

function calc($t0, $t1)
{
    $v0 = array_sum(explode(' ', $t0));
    $v1 = array_sum(explode(' ', $t1));
    $sub = $v1 - $v0;
    echo "$v1 - $v0 = $sub\n";
    return $sub;
}

$a = array();
$b = array(1);
$c = '';

$t0 = microtime();
for ($i = 0; $i < 1E6; $i++) {
    is_array($a);
    is_array($b);
    is_array($c);
}
$t1 = microtime();
$method1 = calc($t0, $t1);     // output: 1420785145.2006 - 1420785142.9861 = 2.2144329547882

$t0 = microtime();
for ($i = 0; $i < 1E6; $i++) {
    $a === (array) $a;
    $b === (array) $b;
    $c === (array) $c;
}
$t1 = microtime();
$method2 = calc($t0, $t1);     // output: 1420785142.986 - 1420785142.2001 = 0.78591012954712

echo ($method1 - $method2) / $method1; // output: 0.64509644428486

從測試中可以知道 is_array() 比較慢。

PHP 5.3.24 (cli) (built: Jun 10 2013 16:42:20)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies
    with Xdebug v2.2.0rc1, Copyright (c) 2002-2012, by Derick Rethans

2014年12月1日 星期一

[PHP] parent::__construct()

class A {}
class B extends A {
    public function __construct() {
        parent::__construct(); // PHP Fatal error:  Cannot call constructor in test.php on line 5
    }
}
new B();

執行上面的程式會發生錯誤。

在 Java 這樣寫是合法的,由此可知 PHP 把 __construct() 當成一種特殊方法使用, 因此既有方法的功能,有又建構子的功能。

PHP 5.3.24 (cli) (built: Jun 10 2013 16:42:20)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies
    with Xdebug v2.2.0rc1, Copyright (c) 2002-2012, by Derick Rethans

2014年7月8日 星期二

[PHP] 轉換 HTML entity 在字串中

在處理 BIG5 轉換 UTF-8 的過程中,字串還存在一些 HTML entity 需要額外處理。 以下還順便處理缺省分號造成沒有轉換的問題。

function entity2utf8($s)
{
   return preg_replace_callback('|&#\d+;?|', function ($matches) {
         $match = $matches[0];
         if (mb_strpos($match, ';') === false) {
             $match .= ';';
         }
         return mb_convert_encoding($match, 'UTF-8', 'HTML-ENTITIES');
      }, $s);
}

$s ='英國皇冠♔';
echo entity2utf8($s); // output: 英國皇冠♔
PHP 5.3.24 (cli) (built: Jun 10 2013 16:42:20)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies
    with Xdebug v2.2.0rc1, Copyright (c) 2002-2012, by Derick Rethans

[Java] Invalid HTTP method: PATCH

最近系統需要使用 Netty4,所以把衝突的 Netty3 拆掉,然後就出現了例外。 pom.xml <dependency> <groupId>com.ning</groupId> <artifactId>as...