English 中文(简体)
删除记录时刷新表格模型适配器:黑莓
原标题:Refreshing Table Model Adapter on deleting record from it : Blackberry
  • 时间:2012-05-24 07:58:49
  •  标签:
  • blackberry

我在 Blackberry 中创建了一个表型适应器 。 (< strong_ / strong_ em> > 这是 i m doing < / em_ > 的样本 ) I 添加了按钮字段和两个字符串。 I m 将数据从矢量中插入字符串 。 现在按下按钮我就想删除对按钮的行 。 数据从数据库中删除, 但单击按钮时不会从屏幕视图中删除 。 当我调用 Retreetive () 函数来调用更新的数据库并再次绘制表格模型适配器时... 它会在旧的表格下添加新的表格 。... 而不是刷新它 。 是否有任何解决方案可以在同一表格中显示更新的数据 。

    package mypackage;

   import java.util.Vector;
   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.ButtonField;
   import net.rim.device.api.ui.component.LabelField;
   import net.rim.device.api.ui.component.table.DataTemplate;
   import net.rim.device.api.ui.component.table.TableController;
   import net.rim.device.api.ui.component.table.TableModelAdapter;
   import net.rim.device.api.ui.component.table.TableView;
   import net.rim.device.api.ui.component.table.TemplateColumnProperties;
   import net.rim.device.api.ui.component.table.TemplateRowProperties;
   import net.rim.device.api.ui.container.MainScreen;
   import net.rim.device.api.ui.decor.BackgroundFactory;

  public final class MyScreen extends MainScreen implements FieldChangeListener 
 {
private DeviceTableModelAdapter _tableModel;
private Vector _cities;
private static final int NUM_ROWS = 1;
    private static final int ROW_HEIGHT = 50;
    private static final int NUM_COLUMNS = 3;
    public ButtonField btn;

   public MyScreen(){
    super(Manager.NO_VERTICAL_SCROLL);

    _cities = new Vector();
    _tableModel = new DeviceTableModelAdapter();

    Vector sample =new Vector();
    sample.addElement("Newyork");
    sample.addElement("NewDelhi");
    sample.addElement("NewOrleans");
    int ik = 0;
       while(ik < sample.size())
       {
           String modelNumber = sample.elementAt(ik).toString();
           String modelName = "-Earth-";
           String ne = String.valueOf(ik);
           Object[] row = {modelName, modelNumber, ne};
           _tableModel.addRow(row);
           ik++;
       }

     TableView tableView = new TableView(_tableModel);
         tableView.setDataTemplateFocus(BackgroundFactory.createLinearGradientBackground(Color.WHITE, Color.WHITE, Color.BLUEVIOLET, Color.BLUEVIOLET));
     TableController tableController = new TableController(_tableModel, tableView);
     tableController.setFocusPolicy(TableController.ROW_FOCUS);
     tableView.setController(tableController);

     // Specify a simple data template for displaying 3 columns
     DataTemplate dataTemplate = new DataTemplate(tableView, NUM_ROWS, NUM_COLUMNS)
     {
         public Field[] getDataFields(int modelRowIndex)
         {
             Object[] data = (Object[]) (_tableModel.getRow(modelRowIndex));
             Field[] fields = {getButtonFieldObject((String)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);
}
public void fieldChanged(Field arg0, int arg1) {

/***    tableView.DeleteAll();  ****/

   /**** calling Class again to draw the table Modal Adapter again with updated value *******/

}
private final static class City
{
    private String _name;
    private String _region;
    private String _image;
    City(String name, String region, String image)
    {
        _name = name;
        _region = region;
        _image = image;
    }

    public String getName()
    {
        return _name;
    }
    public String getRegion()
    {
        return _region;
    }
    public String getImage()
    {
        return _image;
    }
}

 private class DeviceTableModelAdapter extends TableModelAdapter
    {
        public int getNumberOfRows()
        {
            return _cities.size();
        }
        public int getNumberOfColumns()
        {
            return NUM_COLUMNS;
        }
        protected boolean doAddRow(Object row)
        {
            Object[] arrayRow = (Object[]) row;
            _cities.addElement(new City((String) arrayRow[0], (String) arrayRow[1], (String) arrayRow[2]));
            return true;
        }
        protected Object doGetRow(int index)
        {
            City city = (City) _cities.elementAt(index);

            Object[] row = {city.getImage(), city.getRegion(), city.getName()};

            return row;

        }

    }
 public ButtonField getButtonFieldObject(String arg){

     btn = new ButtonField(arg,ButtonField.CONSUME_CLICK);
     btn.setChangeListener(this);
     return btn;
 }
     }
问题回答

What you have to consider is the fact that Blackberry table UI follows the MVC design pattern. So it means that data are updated in the model.

例如:

Object yes = (Object)"Yes";
// gets the Model attached to this View
TableModel tm = (TableModel)view.getModel();
// updates the data attached to the row 
tm.setElement(rowIndex, columnIndex, yes); 
tm.modelReset();




相关问题
How does AlertListener work on Blackberry API 5.0?

Does the AlertListener interface works globally on all alerts on the blackberry device, or does it only work with alerts generated by your application? In other words, can I use this interface to ...

How to buffer audio in Blackberry?

I need to learn how to buffer audio stream from a remote server in Blackberry. There is a sample buffered playback app with Blackberry api but can you tell what url may I use to test the application?

Blackberry push notification implementation

How does one implement a push notification for a blackberry app? I heard that in order to do so I need to purchase a Blackberry Enterprise Server which costs me 1400 per year. Is this true? Where is ...

热门标签