Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SAP Sybase SQL Anywhere 16.0 » Ultra Light Java プログラミング » チュートリアル:BlackBerry アプリケーションの構築 » チュートリアルのコードリスト

 

HomeScreen.java



// ***************************************************************************
// Copyright © 2013 SAP AG or an SAP affiliate company. All rights reserved.
// ***************************************************************************
// This sample code is provided AS IS, without warranty or liability
// of any kind.
//
// You may use, reproduce, modify and distribute this sample code
// without limitation, on the condition that you retain the foregoing
// copyright notice and disclaimer as to the original code.
//
// *********************************************************************
package myapp;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import java.util.*;

class HomeScreen extends MainScreen {

    HomeScreen() {
        
        // Set the window title
        LabelField applicationTitle = new LabelField("Hello BlackBerry");
        setTitle(applicationTitle);
        
        // Add a label to show application status
        _statusLabel = new LabelField("Status: Started");
        add(_statusLabel);
        
        // Add an edit field for entering new names
        _nameEditField = new EditField( "Name: ", "", 50, EditField.USE_ALL_WIDTH );
        add (_nameEditField );

        // Add an ObjectListField for displaying a list of names
        _nameListField = new ObjectListField();
        add( _nameListField );

        // Add a menu item
        addMenuItem(_addToListMenuItem);
        
        // Add sync menu item
        addMenuItem(_syncMenuItem);
        
        // Create database and connect
        try{
            _da = DataAccess.getDataAccess(true);
            _statusLabel.setText("Status: Connected");
        }
        catch(Exception ex)
        {
            _statusLabel.setText("Exception: " + ex.toString());
        }
        // Fill the ObjectListField
        refreshNameList();
    }

    private LabelField _statusLabel;
    private DataAccess _da;
    private EditField _nameEditField;
    private ObjectListField _nameListField;

    private MenuItem _addToListMenuItem = new MenuItem("Add", 1, 1){
        public void run() {
        	onAddToList();
        }
    };
    private MenuItem _syncMenuItem = new MenuItem("Sync", 2, 1){
        public void run() {
            onSync();
        }
    };
    
    public void refreshNameList() {
        //Clear the list
        _nameListField.setSize(0);

        //Refill from the list of names
        Vector nameVector = _da.getNameVector();
        for( Enumeration e = nameVector.elements(); e.hasMoreElements(); ){
            NameRow nr = ( NameRow )e.nextElement();
            _nameListField.insert(0, nr);
        }
    }

    private void onAddToList(){
        String name = _nameEditField.getText();
        _da.insertName(name);
        this.refreshNameList();
        _nameEditField.setText("");
        _statusLabel.setText(name + " added to list");
    }
    
    private void onSync() {
        try {
            if(_da.sync()) {
                _statusLabel.setText("Synchronization succeeded");
            } else {
                _statusLabel.setText("Synchronization failed");
            }
            refreshNameList();
        } catch (Exception ex) {
            Dialog.alert(ex.toString());
        }
    }
}