1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.android.locationtracker.data;
17 
18 import android.app.ListActivity;
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.view.View;
22 import android.widget.ResourceCursorAdapter;
23 import android.widget.TextView;
24 
25 import com.android.locationtracker.R;
26 
27 /**
28  * Used to bind Tracker data to a list view UI
29  */
30 public class TrackerListHelper extends TrackerDataHelper {
31 
32     private ListActivity mActivity;
33 
34     // sort entries by most recent first
35     private static final String SORT_ORDER = TrackerEntry.ID_COL + " DESC";
36 
TrackerListHelper(ListActivity activity)37     public TrackerListHelper(ListActivity activity) {
38         super(activity, TrackerDataHelper.CSV_FORMATTER);
39         mActivity = activity;
40     }
41 
42     /**
43      * Helper method for binding the list activities UI to the tracker data
44      * Tracker data will be sorted in most-recent first order
45      * Will enable automatic UI changes as tracker data changes
46      *
47      * @param layout - layout to populate data
48      */
bindListUI(int layout)49     public void bindListUI(int layout) {
50         Cursor cursor = mActivity.managedQuery(TrackerProvider.CONTENT_URI,
51                 TrackerEntry.ATTRIBUTES, null, null, SORT_ORDER);
52         // Used to map tracker entries from the database to views
53         TrackerAdapter adapter = new TrackerAdapter(mActivity, layout, cursor);
54         mActivity.setListAdapter(adapter);
55         cursor.setNotificationUri(mActivity.getContentResolver(),
56                 TrackerProvider.CONTENT_URI);
57 
58     }
59 
60     private class TrackerAdapter extends ResourceCursorAdapter {
61 
TrackerAdapter(Context context, int layout, Cursor c)62         public TrackerAdapter(Context context, int layout, Cursor c) {
63             super(context, layout, c);
64         }
65 
66         @Override
bindView(View view, Context context, Cursor cursor)67         public void bindView(View view, Context context, Cursor cursor) {
68             final TextView v = (TextView) view
69                     .findViewById(R.id.entrylist_item);
70             String rowText = mFormatter.getOutput(TrackerEntry
71                     .createEntry(cursor));
72             v.setText(rowText);
73         }
74     }
75 }
76