iOS UITableView cell and section height

TableView在iOS4所寫相同的Code,在iOS5 build起來卻有不同的結果,最後找出原因在iOS4以前TableView預設的section高度為0(簡單來說就是看不到),但在iOS5卻為預設高度(好像是20,剛好一行字)。上架(Apple Store)後並不會出現這問題,所以若已經確定為上架版本可以放心。

iOS5上需要特別去修正過section的高度來讓他消失,此function回傳值即為section的高度。
//section height
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 0.0;
}


修改Cell高度即為
//cell height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;{
//set cell size
return 100.0;
}


相關連結
iOS Developer Library
iOS Developer Library - UITableView
iOS Developer Library - UITableViewController

iOS UITableView seleted blue background

TableView預設在點選同時會產生藍色的背景作為Highlight,在某些狀況這是好事,可以清楚的標示出目前所點到的那行,但部份狀況下是不需要他的,其實只要簡單的一行就可以讓他消失掉。

只需要在UITableView的Delegate內加入這行,也就是下面這function內加入

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {}


// cell 為cell的名稱,也就是所定義的"UITableViewCell *cell"
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];


相關連結
iOS Developer Library
iOS Developer Library - UITableView
iOS Developer Library - UITableViewController

Android Gallery switch to position移動至指定位置

需要將Gallery、ListView、GridView移動至指定位置皆可使用此語法。

setSelection(position, animate)

Sample
imageGallery.setSelection(position, animate)


相關連結
Android Developer
Android Developer - Gallery
Android Developer - ListView
Android Developer - GridView

MySQL Client on Android

現在App對於網路的依賴度越來越高,畢竟資料如果是死的,沒辦法分享給朋友,不能按讚、+1、Tweet、Plurk......等的因素實在很痛苦,Android的SDK對於SQLite有完整的支援,但SQLite畢竟還是個local的資料存取,想要更新資料還是需要透過網路,一般App在這種狀態下大概可以分為三種方式。

1. 每次開啟App將所有資料更新
優點:一次將所有資料更新完成,在後續使用上可以完全不必在進行網路存取,減少每次等候時間,常見於電子書
缺點:安全性倍受考驗,若資料有安全性考量應盡量避免,資料量若太大會佔用到過多的容量空間,Mobile Device不是電腦上的HDD,容量是用G來計算

2. 即時存取 - 使用Api界接
優點:由後台對資料整理過後再進行傳輸,減少掉不必要的資訊,快速達成所需,製作一次可供多種平台做使用
缺點:執行製作上必須多花一份工

3. 即時存取 - 直接Query資料庫
優點:省去後台多做一次工
缺點:每次使用都需要與資料庫連線才可以取得資料,Mobile Device上的網路狀況並不像電腦的網路環境,常常會遇上無法順利與資料庫產生連線導致無法取得資訊

不過今天所要談的重點為MySQL Client,所以當然是講"3. 即時存取 - 直接Query資料庫",在這邊我選擇直接使用JDBC來溝通,直接進入重點

Step 1.下載JDBC http://dev.mysql.com/downloads/connector/j/5.0.html#downloads
因為Android對於JAVA支援版本關係,無法使用最新版,經過測試後發現3.0.17這版本就以足夠使用


Step 2. 將JDBC的Jar檔案加入至專案中

Step 3. Sample code
private Connection connect = null;
private Statement statement = null;
private ResultSet resultSet = null;

public static final String MYSQL_IP = "192.168.0.100";
public static final String MYSQL_DBNAME = "Book";
public static final String MYSQL_USERNAME = "isken";
public static final String MYSQL_PASSWORD = "isken";

public ArrayList bookList() throws Exception {
ArrayList results = new ArrayList();
try {
String script = "SELECT id, name, URL FROM Book";
Log.e("Isken", "script = "+script);

// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");

Log.e("Isken", "jdbc:mysql://"+MYSQL_IP+"/+MYSQL_DBNAME+?"+ "user="+MYSQL_USERNAME+"&password="+MYSQL_PASSWORD);

// Setup the connection with the DB
connect = (Connection) DriverManager.getConnection("jdbc:mysql://"+MYSQL_IP+"/+MYSQL_DBNAME+?"+ "user="+MYSQL_USERNAME+"&password="+MYSQL_PASSWORD);

Log.e(PAGETAG, "connection is success");

// Statements allow to issue SQL queries to the database
statement = (Statement) connect.createStatement();

// Result set get the result of the SQL query
resultSet = statement.executeQuery(script);

while (resultSet.next()) {
MyObj obj = new MyObj();
String id = resultSet.getString("id");
String name = resultSet.getString("name");
String url = resultSet.getString("URL");
obj.setId(id);
obj.setName(name);
obj.setURL(url);
results.add(obj);
}
Log.e(PAGETAG, "results size = "+results.size());
} catch (Exception e) {
throw e;
} finally {
close();
}
return results;
}



相關連結
Android Developer
MySQL Developer Zone
Sample Code

Android Google Map Streetview 街景服務

街景服務也是個Google很強大的功能之一,但這功能如果只能使用在一般的PC上實在有點可惜,當然iOS、Android也有相關服務可以使用,在此介紹Android上的使用方式

double objLatitude = 25.066319;
double objLongitude =  121.557541;
Intent streetView = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("google.streetview:cbll="+ objLatitude+","+objLongitude+"&cbp=1,99.56,,1,-5.27&mz=21"));
startActivity(streetView);


相關連結
Android Developer
Android Developer - Activity