J2ME User Interface Architecture

|
MIDP 2.0 มี UI class อยู่ 2 package คือ javax.microedition.lcdui และ javax.microedition.lcdui.game ตามชื่อ lcd ก็คือ ui ทั้วไปส่วน game ก็คือเกม

lcdui แบ่งได้เป็น 2 กลุ่ม high-level กับ low-level, high เหมาะกับคนที่ต้องการให้โปรแกรมใช้ได้กล้าวขวาง เพราะมันไม่ได้ไปควบคุมว่าจะให้จอแสดงอะไรจริง ๆ แต่มันจะให้มือถือเป็นคนจัดการเองว่าจะแสดงผลยังไง ตามรูป

low-level ก็กำหนดไปเลยจะให้แสดงผลตำแหน่งไหน ยังไง มีแค่ 3 class

และมี GameCanvas อีกอันนึง

ถ้าเราอยากจะแสดงผล ไม่ว่า high หรือ low ต้อง implement Displayable interface

เห็นว่า Graphics ไม่ต้อง implement เพราะมันทำงาน 2-D low-level ที่ทำกับจอโดยตรงอยู่แล้ว

Displayable class จะเป็นส่วนแสดงผล มันจะมี method ให้ขอข้อมูลได้ โดย MIDlet แสดง UI โดยเรียก method ของ Display class ชื่อ setCurrent(Displayable element)

จากชื่อ method ก็น่าจะรู้ได้ Display สามารถแสดงผลได้ทีละ element โดยอันนั้นจะเป็น current สามารถเรียกถึงได้ด้วย getCurrent() จะได้ instance ของ Displayable element และมี static method ชื่อ getDisplay(MIDlet midlet) จะได้ display instance ที่อยู่กับ MIDlet นั้นๆ

import java.util.Date;

import javax.microedition.lcdui.Alert;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

public class DateTimeApp extends MIDlet {

Alert timeAlert;

public DateTimeApp() {
timeAlert = new Alert("Alert!");
timeAlert.setString(new Date().toString());
}

public void startApp() {
Display.getDisplay(this).setCurrent(timeAlert);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
}

[TIP] dot prefix หน้า Activity Name

|
บางทีจะเห็น . หน้าชื่อ Activity เอาไว้แสดงถึงว่า root

[TIP] local variable and global

|
protected void onListItemClick(ListView l, View v, int position, long id) {
      super.onListItemClick(l, v, position, id);
      Cursor c = mNotesCursor;
      c.moveToPosition(position);
      Intent i = new Intent(this, NoteEdit.class);
      i.putExtra(NotesDbAdapter.KEY_ROWID, id);
      i.putExtra(NotesDbAdapter.KEY_TITLE, c.getString(
      c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
      i.putExtra(NotesDbAdapter.KEY_BODY, c.getString(
      c.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
      startActivityForResult(i, ACTIVITY_EDIT);
}
Note: We assign the mNotesCursor field to a local variable at the start of the method. This is done as an optimization of the Android code. Accessing a local variable is much more efficient than accessing a field in the Dalvik VM, so by doing this we make only one access to the field, and five accesses to the local variable, making the routine much more efficient. It is recommended that you use this optimization when possible.
สรุป local ดีกว่า

[TIP] Context สำคัญอย่างไร

|
Context เป็นส่วนที่ใช้ติดต่อกับ Android os มักจำเป็นในการเรียกใช้ method ต่างๆ เช่น new Intent, new ListView, บลาๆๆ

Activity สืบทอดมาจาก Context ดังนั้นใน class Activity ของเราจะเรียกใช้ context ก็เรียก this ได้เลย

[TIP] ทำไมต้องตั้งชื่อตัวแปรขึ้นต้นด้วย m

|
เช่น mNotesCursor The m denotes a member field and is part of the Android coding style standards.

ย่อมากจาก member และเป็นมาตรฐานของ Anroid coding style แค่นั้นแหละ
edit: member คือเป็นตัวแปรของ class ใช้ร่วมกันกับ method อื่นๆใน class ได้

[TIP] list และ empty id

|
The list and empty IDs are provided for us by the Android platform, so, we must prefix the id with android: (e.g., @android:id/list).

ID ชื่อ list และ empty มักเคยเห็นกันบ่อยๆ คงจะเป็นมาตรฐานอะไรซักอย่าง เหมือนเคยอ่านเค้าแนะนำว่าถ้ามีการใช้ listview ควรจะมี id ชื่อ @android:id/list เอาไว้ด้วย

The View with the empty id is used automatically when the ListAdapter has no data for the ListView. The ListAdapter knows to look for this name by default. Alternatively, you could change the default empty view by using setEmptyView(View) on the ListView.

ถ้าเกิด listview ไม่มีข้อมูล มันจะไปใช้ view ที่มี id เป็น empty ให้อัตโนมัติ (ดีแฮะ)

More broadly, the android.R class is a set of predefined resources provided for you by the platform, while your project's R class is the set of resources your project has defined. Resources found in the android.R resource class can be used in the XML files by using the android: name space prefix (as we see here).

ตามนั้น

[TIP] เครื่องหมาย @ ใน tag id คืออะไร?

|

The @ symbol in the id strings of the ListView and TextView tags means that the XML parser should parse and expand the rest of the id string and use an ID resource.

คือมันจะไปเอา id มาจาก path นั้นๆ

[TIP] What XML namespace in Android layout file does?

|
XML namespace จะต้องถูกประกาศไว้ที่ component หรือ layout แรกสุด (top level) ไม่งั้นจะใช้เท็ก (tags) android: ไม่ได้ทั้งไฟล์เลย:

xmlns:android="http://schemas.android.com/apk/res/android"

S60 application framework and user interface

|

อ่าน Application Framework และ S60 UI ใน S60 3rd Edition C++ Developer's Library

application framework จะอธิบายว่าจะใช้ features ที่ framwork มีมาให้ได้ยังไงตอนที่เรา design และ implementing โปรแกรมของเรา

UI อธิบายวิธี implement application UI บน S60

แนะนำว่าควรอ่านไปพร้อมๆกัน เพราะเนื้อหาบางส่วนก็อ้างอิงกันอยู่ ตอนนี้คุณก็เข้าใจพื้นฐานการเขียน Symbian C++ แล้วล่ะ

What is Symbian C++?

|

การเขียนโปรแกรมภาษา Symbian C++ คล้ายกับ C++, แต่ถูกปรับปรุงเพื่อ อุปกรณ์พกหาซึ่งมีทรัพยากรต่ำ ประเด็นหลักๆ ของ Symbian C++ ได้แก่:

  • ควบุคม object’s life time ด้วย CleanupStack.
  • ใช้ exception handling ที่ดีกว่าด้วย Trap/Leave แทนที่จะเป็น try/catch.
  • String handling ด้วย descriptors.
  • Client/server model, asynchronous calls, and active object ตามนั้น ไม่รู้อะไรเหมือนกัน

ถ้าเป็นมือใหม่ก็ไปอ่านนี่ก่อน S60 Platform: Comparison of ANSI C++ and Symbian C++ document, จะอธิบายความแตกต่างของ Symbian C++ และ C++, จากนั้นก็ไปนี่ C++ Coding Conventions section ใน S60 3rd Edition C++ Developer's Library จะได้เขียนได้ตาม style ที่ดี

Tools and SDKs

|
  • Integrated development environments (IDEs) — ถ้าจะเขียนด้วย Symbian C++ ก็ใช้ Carbide.c++ ถ้า Java ก็ Netbeans หรือ Eclipse ส่วน WRT เค้าบอกว่าใช้พวก Aptana Studio, Adobe Dreamweaver หรือ Microsoft Visual Studio
  • Runtimes — ดาวน์โหลดพวก Qt, Web Runtime, Symbian C++, Flash Lite from Adobe, or Java™ technology บลาๆๆ ยังไม่ต้องสนใจ
  • Platforms — SDKs ของ S60 platform, Series 40 platform และ Maemo platform. ไปดาวน์โหลดมาลงซะ
  • Plug-ins — เผื่อถ้าอยากเขียนด้วย Qt, Open C++, Python บลาๆๆ มีสองแบบ
    • Extensions plug-ins เขียนด้วยภาษาอื่น หรือเพิ่ม APIs
    • Enablers plug-ins สำหรับเขียนโปรแกรมเฉพาะทาง เช่น SNAP Mobile games or WRT widgets, or perform certain tasks, such as examining the energy use of a device or creating settings for use in Nokia VoIP ประมาณนั้น

Nokia platforms

|
จะเริ่มพัฒนา app บนโนเกียก็ต้องเลือกก่อนว่าจะพัฒนาด้วยภาษาอะไร
  • Symbian C++

    ครบเครื่องที่สุดสำหรับการเขียน mobile application มี APIs เยอะ และ SDK ทั้งสำหรับ S60 และเฉพาะเครื่อง เช่น N97 เสถียรและระบบ secure access server รวมถึงเกม 3D ประสิทธิภาพสูง เรียกได้ว่าเป็น platform ที่มีสมรรถนะสูงที่สุดสำหรับคนทำ app Nokia ทั่วโลก
  • Qt

  • cross-platform application and UI framework มีข้อดีคือสามารถพอร์ท Desktop app ลง Mobile ได้ ใช้ C++ API และระบบ UI ที่ใช้งานง่าย ทำให้เขียนโปรแกรมครั้งเดียวก็สามารถโอนย้ายโปรแกรมจาก S60 ไป Maemo หรือไป Desktop ก็ได้
  • Web Runtime

  • Web Runtime (WRT) ใช้ HTML, CSS, and JavaScript เขียนโปรแกรมบนมือถือ app จะออกแนวคล้ายเว็บ
  • Java™

  • Java™ Platform, Micro Edition (Java™ ME) แทบทุกเครื่องต้องมี java runtime มีทั้งสำหรับมือถือรุ่นเก่า Series 40 จนถึง S60 เขียน code ครั้งเดียวก็ใช้ได้หลาย platform และสามารถทำงานได้หลากหลายทั้ง media, file, and network services
  • Flash Lite

  • S60 และ Series 40 จะมี Flash Lite 3.0 Player การตอบสนองจะราบลื่น ความสามารถด้าน online data-connection สูง รวมถึงความสามารถใหม่ streaming movie playback
http://www.forum.nokia.com/Technology_Topics/Development_Platforms/