0

Is there any way to refresh Table Adapter Screen on button click. If i m callling table method again on button click then it is adding a new table adapter screen below the old on.....I s there any way to refresh the existing screen.The button is deleting elements from table after which i want to refresh it.

    public static void richlistshow(){
        int ik = target_list.size()-1;
       while(ik > 0 )
       {
           Bitmap logoBitmap = Bitmap.getBitmapResource("delete.png");       

           City aCity = (City)target_list.elementAt(ik);
           String modelNumber = aCity.get_city_name().toString();
           String modelName = " X ";
           //String ne = String.valueOf(ik);
           String date_time = "  Date-Time";

           Object[] row = {modelName, modelNumber,date_time,logoBitmap};
           _tableModel.addRow(row);
           ik--;
       }


     TableView tableView = new TableView(_tableModel);
     tableView.setDataTemplateFocus(BackgroundFactory.createLinearGradientBackground(Color.YELLOWGREEN, Color.LIMEGREEN, Color.SEAGREEN, Color.SANDYBROWN));
     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)
         {
             //final int i =modelRowIndex;
             Object[] data = (Object[]) (_tableModel.getRow(modelRowIndex));

             final String cname = (String)data[1];

             /****** Declaring button for deletion of record from database ******/
             ButtonField delete =new ButtonField("X",ButtonField.CONSUME_CLICK);

             /*******  Setting change listener and defining field change within *******/
             delete.setChangeListener(new FieldChangeListener() {

             /*******  defining what should happen when button is clicked ********/
                public void fieldChanged(Field field, int context) {

                    DatabaseHandler delete_row = new DatabaseHandler();
                    delete_row.deletetarget(cname);

            /****calling function to retrieve values from the databasse table.***/
                    delete_row.retrieveTarget();
            /****************calling method again to show the updated values*************/      
                richlistshow();//for showing refreshed data after deletion of a record

                }
            });

             Field[] fields = {delete, new LabelField((String) data[1]), new LabelField((String) data[2]),new BitmapField((Bitmap)data[3])};
             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);

     mainManager.add(tableView);

}

public final static class MyCity
{
    private String _name;
    private String _datetime;
    private String _button;
    private Bitmap _bitmap;

    MyCity(String name, String datetime, String button, Bitmap bitmap)
    {
        _name = name;
        _datetime = datetime;
        _button = button;
        _bitmap = bitmap;
    }

    public String getName()
    {
        return _name;
    }
    public String getDatetime()
    {
        return _datetime;
    }
    public String getButton()
    {
        return _button;
    }
    public Bitmap getBitmap()
    {
        return _bitmap;
    }
}


/****************TABLE CONTROLLER CLASS ******************/
 private class CityTableModelAdapter extends TableModelAdapter
    {
        public int getNumberOfRows()
        {
            return _cities.size();
        }
        public int getNumberOfColumns()
        {
            return NUM_COLUMNS;
        }
        protected boolean doAddRow(Object row)
        {
            Object[] arrayRow = (Object[]) row;

            System.out.println("! : "+arrayRow[0]+" @: "+arrayRow[1]+" #: "+arrayRow[2]);

            _cities.addElement(new MyCity((String) arrayRow[0], (String) arrayRow[1], (String) arrayRow[2], (Bitmap) arrayRow[3]));
            return true;
        }
        protected Object doGetRow(int index)
        {
            MyCity mycity = (MyCity) _cities.elementAt(index);

            Object[] row = {mycity.getName(),mycity.getDatetime(),mycity.getButton(),mycity.getBitmap()};

            return row;

        }


    }
optimus
  • 1
  • 1
  • Call `invalidate()` method of `MainScreen` instance. – Rupak May 22 '12 at 14:34
  • Im putting invalidate () method and below i m calling retrieve function and richlistshow () function again......but instead of refreshing it is adding new table Layout below the old one and if i delete all the element it still shows the last element which deleted in the last..... – optimus May 23 '12 at 07:24
  • Before add anything, e.g. `mainManager.add(tableView);` , make sure that you delete old `tableView` added to `mainManager`. And call `invalidate()` after all the update finished. – Rupak May 23 '12 at 07:50
  • if( tableView ){ mainManager.delete(tableView); mainManager.add(tableView); } else{ mainManager.add(tableView); } here is wat i m trying to do but it showing error in if(tableView) condition.....wat should i write here???? – optimus May 23 '12 at 08:56
  • `try { mainManager.delete(tableView); } catch (Exception exc) {}` use this code for deleting tableView always. And after this, add `mainManager.add(tableView);` – Rupak May 23 '12 at 09:32
  • exception occurs : java.lang.IllegalArgumentException: Attempt to delete a field that doesn't belong to the manager. – optimus May 23 '12 at 10:04
  • Can you paste your full code somewhere? try - catch block was added for that exception. – Rupak May 23 '12 at 10:07
  • and dat is why exception arised......the above code is full code except the retrieval function.......but that works fine.....there is problem when we delete the object but instead of reloading the TableModalAdapter it – optimus May 23 '12 at 10:18
  • it loads below the previous one.... – optimus May 23 '12 at 10:43
  • It's too hard to find problems on a partial code snippet. You need to paste all the code of the Screen where you added the `tableView`. And most of lines of your code isn't important. So, update your question with full code (from initialization of objects, ui event handling code). You don't need to expose the original code you are working, just paste a code that represents what are you working with. – Rupak May 23 '12 at 11:17
  • Link to detailed question including code..........................http://stackoverflow.com/questions/10733403/refreshing-table-model-adapter-on-deleting-record-from-it-blackberry – optimus May 24 '12 at 08:00
  • wat happened???....no solution???? – optimus May 29 '12 at 04:43

0 Answers0