 Thread Author
# 1

05-30-2011, 02:47 AM
|
| CrackBerry Newbie | | Join Date: May 2011 Posts: 1 Likes Received: 0
Thanked 0 Times in 0 Posts
| |
Tables with event handling
i create a sample program for tables black berry. i am facing problem while trying to implement Listeners like FieldChangeListener on tables.
My sample demo has two classes.
One is the main class and other is the screen class.
// class having main function:
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.container.MainScreen;
// main ui class
public class MainExec extends UiApplication
{
/**
* Entry point for application
* @param args Command line arguments (not used)
*/
public static void main(String[] args)
{
MainExec meapps = new MainExec();
meapps.enterEventDispatcher();
}
public MainExec()
{
pushScreen(new FirstView());
}
}
class FirstView extends MainScreen implements FieldChangeListener
{
BasicEditField name, pass;
CheckboxField checkBox1;
ButtonField btn1, btn2;
FirstView()
{
setTitle("Editable Text Field Demo");
// basic edit field objects
name = new BasicEditField("User Name: ","",10,BasicEditField.FILTER_LOWERCASE);
pass = new PasswordEditField("Password: ","",15,BasicEditField.FILTER_LOWERCASE);
checkBox1 = new CheckboxField("Terms & Conditions", false);
btn1 = new ButtonField("Reset");
btn2 = new ButtonField("Submit");
//
add(name);
add(pass);
add(checkBox1);
// add buttons
add(btn1);
add(btn2);
// add field change listener to buttons.
btn1.setChangeListener(this);
btn2.setChangeListener(this);
}
public void fieldChanged(Field field, int context)
{
//name.getText()
// coding for reset button
if(field.equals(btn1))
{
// clear check boxes
name.setText("");
pass.setText("");
// check off the "check-box"
checkBox1.setChecked(false);
}
// coding for reset button
if(field.equals(btn2))
{
// clear check boxes
String userStr = name.getText();
String passStr = pass.getText();
int l1 = name.getTextLength();
int l2 = pass.getTextLength();
boolean status = checkBox1.getChecked();
// check the strings of text-fields
// and status of check-box
if((userStr.equals("test") && passStr.equals("test")) && (!status))
{
String msg = "Please select the \'"+checkBox1.getLabel()+"\' check box.";
Dialog.ask(Dialog.D_OK, msg);
}
else if((l1>0 || l2>0))
{
if(!(userStr.equals("test")) || !(passStr.equals("test")))
{
String msg = "Please enter valid UserName and Password.";
Dialog.ask(Dialog.D_OK, msg);
}
else
{
UiApplication.getUiApplication().pushScreen(new MyScreen1());
// UiApplication.getUiApplication().pushScreen(new SecondView());
}
}
else
{
String msg = "Please enter UserName and Password.";
Dialog.ask(Dialog.D_OK, msg);
}
}
}
//override the onClose() method to display a dialog box to the user
//with "Goodbye!" when the application is closed
public boolean onClose()
{
Dialog.alert("Goodbye!");
System.exit(0);
return true;
}
}
// class having tabel screen::
import java.util.Vector;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.table.*;
import net.rim.device.api.ui.decor.BackgroundFactory;
public class MyScreen1 extends MainScreen implements FieldChangeListener
{
private Vector _devices;
private DeviceTableModelAdapter _tableModel;
private static final int NUM_ROWS = 1;
private static final int ROW_HEIGHT = 50;
private static final int NUM_COLUMNS = 3;
public MyScreen1()
{
super(Manager.NO_VERTICAL_SCROLL);
setTitle("Table Adapter Screen");
add(new LabelField("BlackBerry Devices", LabelField.FIELD_HCENTER));
add(new SeparatorField());
_devices = new Vector();
// create object of table-model
_tableModel = new DeviceTableModelAdapter();
// create first row.
String modelNumber = "9800";
String modelName = "Blackberry 9800";
Bitmap bitmap = Bitmap.getBitmapResource("icon.png");
Object[] row = {modelName, modelNumber, bitmap};
_tableModel.addRow(row);
// create second row.
String modelNumber2 = "9900";
String modelName2 = "Blackberry 9900";
Bitmap bitmap2 = Bitmap.getBitmapResource("icon.png");
Object[] row2 = {modelName2, modelNumber2, bitmap2};
_tableModel.addRow(row2);
// Set up table view and controller
TableView tableView = new TableView(_tableModel);
tableView.setDataTemplateFocus(BackgroundFactory.c reateLinearGradientBackground(Color.WHITE, Color.WHITE, Color.BLUEVIOLET, Color.BLUEVIOLET));
TableController tableController = new TableController(_tableModel, tableView);
tableController.setFocusPolicy(TableController.ROW _FOCUS);
tableView.setController(tableController);
DataTemplate dataTemplate = new DataTemplate(tableView, NUM_ROWS, NUM_COLUMNS)
{
/**
* @see DataTemplate#getDataFields(int)
*/
public Field[] getDataFields(int modelRowIndex)
{
Object[] data = (Object[]) (_tableModel.getRow(modelRowIndex));
Field[] fields = {new BitmapField((Bitmap) data[0]), new LabelField((String) data[1]), new LabelField((String) data[2])};
return fields;
}
};
dataTemplate.useFixedHeight(true);
// Define regions and row height
dataTemplate.setRowProperties(0, new TemplateRowProperties(ROW_HEIGHT));
for(int i = 0; i < NUM_COLUMNS; i++)
{
dataTemplate.createRegion(new XYRect(i, 0, 1, 1));
dataTemplate.setColumnProperties(i, new TemplateColumnProperties(Display.getWidth() / NUM_COLUMNS));
}
// Apply the template to the view
tableView.setDataTemplate(dataTemplate);
add(tableView);
tableView.setChangeListener(this);
}
public void fieldChanged(Field field, int context)
{
// TableView tb_View = (TableView)field;
// DataModel temp_tbmodel = tb_View.getModel();
Dialog.alert("enter into dialog.");
}
private class DeviceTableModelAdapter extends TableModelAdapter
{
public int getNumberOfRows()
{
return _devices.size();
}
public int getNumberOfColumns()
{
return NUM_COLUMNS;
}
protected boolean doAddRow(Object row)
{
Object[] arrayRow = (Object[]) row;
_devices.addElement(new BlackBerryDevice((String) arrayRow[0], (String) arrayRow[1], (Bitmap) arrayRow[2]));
return true;
}
protected Object doGetRow(int index)
{
BlackBerryDevice device = (BlackBerryDevice) _devices.elementAt(index);
Object[] row = {device.getImage(), device.getModel(), device.getName()};
return row;
}
}
private final static class BlackBerryDevice
{
private String _name;
private String _model;
private Bitmap _image;
BlackBerryDevice(String name, String model, Bitmap image)
{
_name = name;
_model = model;
_image = image;
}
public String getName()
{
return _name;
}
public String getModel()
{
return _model;
}
public Bitmap getImage()
{
return _image;
}
}
public void close()
{
// Display a farewell message before closing the application
Dialog.alert("Goodbye!");
super.close();
}
}
after giving user-name and password as "test", click the check-box and click the submit button table will show. now i want to implement some event action like if click on any row-column value, then a alert message will be shown.
Please give me solution if any body have.
|