views:

360

answers:

2

I'm writing a Blackberry app. I have a custom list field where I can select an item in the list which pushes the edit screen onto the stack. I edit the item and save, and when I pop that screen off so I am back on my list screen, I want to view the update I just made reflected in the list. I have done this on other screens which just had LabelFields and it worked fine. However, with the list screen, calling invalidate() seems to do nothing. I know the value has saved correctly through print lines, and I see the paint() method in the listfield is getting called. But the only way I can get the list field to update is to delete it from the screen and re-add it. That seems wrong. What am I doing wrong?

public class ListTasksScreen extends MainScreen{    
    private TaskList tasks;
    private CustomListField taskListField;

    public ListTasksScreen (TaskList tasks){
        super();
        this.tasks = tasks;     
        Vector incompleteTasks = tasks.getIncompleteTasks();
        taskListField = new CustomListField(incompleteTasks, tasks);
        add(taskListField);     
    }

    public void updateTaskList(TaskList t)
    {
        Vector incompleteTasks = t.getIncompleteTasks();
        taskListField= new TaskListField(incompletetTasks, t);
            //I just want to call taskListField.invalidate() here.
            //the only thing that seems to work is deleting taskListField 
            //and re-adding
            this.delete(taskListField);
        add(taskListField);
    }
}
A: 

to add item and update list:

  • add item to array/vector of list items
  • perform insert new row (listField.insert(listField.getSize());)
Max Gontar
+1  A: 

Is there a typo in your code above? in the updateTaskList method you do:

taskListField= new TaskListField(incompletetTasks, t);

should it be:

taskListField= new CustomListField(incompletetTasks, t);

Anyway, I think the reason you are having problems is because when you update your task list you are actually creating a new CustomListField object. When you first do add(taskListField) you are passing a reference to the field to the screen, so it has its own reference. When you call taskListField= new CustomListField(incompletetTasks, t); you are only updating your own reference, not the one in the screen. So if you call invalidate the screen will repaint using the original reference, which must be using references to the original versions of incompleteTasks and tasks too.

The reason it works the other way is because you are actually removing the old reference and adding the new one, so the screen now knows of the updated data.

What you should do is add a method to your CustomListField that allows you to update the task list object. Then when you call that method on the existing reference to taskListField and then call invalidate, your paint method should now use the new values when it calls drawListRow in the callback.

DaveJohnston
Yes, that was a typo, sorry. That makes complete sense about the reference to the listfield. Thank you! Going to try it out now.
Michaela