Monday 5 March 2012

Using Solution DataSetObserver in Android.

Part of the key functionality of my application was the task sliders, a way of representing progress towards task completion with a highly graphical interface. Allowing the user to look at a progress bar rather than a simple number or text summary. I was having difficulty reflecting the changed progress in the summary
I found the best way to implement this was by registering an observer that would be notified of changes to the items in the List.
1:  final TaskAdapter taskAdapter = new TaskAdapter(this, R.id.list, taskArrayList);  
2:      final DataSetObserver observer = new DataSetObserver() {  
3:        @Override  
4:        public void onChanged() {  
5:          populateOverviewText();  
6:        }  
7:      };  
8:      taskAdapter.registerDataSetObserver(observer);  
Here I'm defining a new DataSetObserver and calling the method to update the textview from inside the onChanged listener. I need to fire this listener when I edit one of the progress bars. Here's the code for notifying the observer of a change.
1:    private void attachProgressUpdatedListener(SeekBar seekBar,  
2:        final int position) {  
3:      seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
4:        public void onStopTrackingTouch(SeekBar seekBar) {  
5:          int progress = seekBar.getProgress();  
6:          int task_id = (Integer) seekBar.getTag();  
7:          TaskHandler taskHandler = new TaskHandler(DBAdapter  
8:              .getDBAdapterInstance(getContext()));  
9:          taskHandler.updateTaskProgress(task_id, progress);  
10:          mList.get(position).setProgress(progress);  
11:          //need to fire an update to the activity  
12:          notifyDataSetChanged();  
13:        }  
14:        public void onStartTrackingTouch(SeekBar seekBar) {  
15:          // empty as onStartTrackingTouch listener not being used in  
16:          // current implementation  
17:        }  
18:        public void onProgressChanged(SeekBar seekBar, int progress,  
19:            boolean fromUser) {  
20:          // empty as onProgressChanged listener not being used in  
21:          // current implementation  
22:        }  
23:      });  

Wednesday 4 January 2012

Changes to UI code.

So I've made some change to the basic design of the application. I thought it would be worth justifying the here so when I come to write up why I remember. Originally I had a List item in my activity, I moved beyond this to using ArrayLists With an Adaptor So the new changes I've put in are more in keeping with what seems best - practices. I've got an project_list_layout.xml and a project_list_item.xml These are passed to the constructor of of the UI component which inflates the xml. I then moved beyond this to a ListActivity, which doesn't need a specific list element in the layout as one is provided as part of the subclass.