Refreshing Table Model Adapter on deleting record from it : Blackberry

∥☆過路亽.° 提交于 2019-12-13 04:42:15

问题


I m creating a Table Model Adapter in Blackberry.(**This is a sample what i m doing****)I have added button field and two Strings.I m putting data in String from vector.Now on button click i want to delete the row against button.The Data is deleted from the database but not removed from Screen view when button is clicked.When i calld the Retrieve() function to call updated database and draw the Table model adapter again....it is adding new Table below old one....not refreshing it. Is there any solution to show the refreshed data in same table.

    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;
 }
     }

回答1:


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.

Like for example:

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();


来源:https://stackoverflow.com/questions/10733403/refreshing-table-model-adapter-on-deleting-record-from-it-blackberry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!