Bug 885590: BookmarksListView should use a CursorAdapter. [r=margaret]
authorSriram Ramasubramanian <sriram@mozilla.com>
Thu, 20 Jun 2013 16:40:45 -0700
changeset 143349 78e379237afba52edd8b5be357cba5984b1b2148
parent 143348 42c9ee1b0a158b5970d89f00c831827bf5cf99ef
child 143350 e8acb32b766ac31eb6bb126f98718efc6b4f09bb
push id25130
push userlrocha@mozilla.com
push dateWed, 21 Aug 2013 09:41:27 +0000
treeherdermozilla-central@b2486721572e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs885590
milestone24.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 885590: BookmarksListView should use a CursorAdapter. [r=margaret]
mobile/android/base/home/BookmarksListView.java
--- a/mobile/android/base/home/BookmarksListView.java
+++ b/mobile/android/base/home/BookmarksListView.java
@@ -17,18 +17,18 @@ import android.content.Context;
 import android.database.Cursor;
 import android.os.AsyncTask;
 import android.util.AttributeSet;
 import android.util.Pair;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
+import android.widget.CursorAdapter;
 import android.widget.ListView;
-import android.widget.SimpleCursorAdapter;
 
 import java.util.LinkedList;
 
 /**
  * A ListView of bookmarks.
  */
 public class BookmarksListView extends HomeListView
                                implements AdapterView.OnItemClickListener{
@@ -66,17 +66,17 @@ public class BookmarksListView extends H
         mFolderView.open();
     }
 
     @Override
     public void onAttachedToWindow() {
         super.onAttachedToWindow();
 
         // Intialize the adapter.
-        mCursorAdapter = new BookmarksListAdapter(getContext());
+        mCursorAdapter = new BookmarksListAdapter(getContext(), null);
 
         // We need to add the header before we set the adapter, hence make it null
         refreshListWithCursor(null);
 
         setOnItemClickListener(this);
         setOnKeyListener(GamepadUtils.getListItemClickDispatcher());
 
         mQueryTask = new BookmarksQueryTask();
@@ -129,17 +129,17 @@ public class BookmarksListView extends H
         position -= headerCount;
 
         cursor.moveToPosition(position);
 
         int type = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks.TYPE));
         if (type == Bookmarks.TYPE_FOLDER) {
             // If we're clicking on a folder, update adapter to move to that folder
             final int folderId = cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks._ID));
-            final String folderTitle = mCursorAdapter.getFolderTitle(position);
+            final String folderTitle = mCursorAdapter.getFolderTitle(cursor);
             mCursorAdapter.moveToChildFolder(folderId, folderTitle);
         } else {
             // Otherwise, just open the URL
             final String url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL));
             OnUrlOpenListener listener = getOnUrlOpenListener();
             if (listener != null) {
                 listener.onUrlOpen(url);
             }
@@ -171,29 +171,29 @@ public class BookmarksListView extends H
 
         // Reset the task.
         mQueryTask = null;
     }
 
     /**
      * Adapter to back the ListView with a list of bookmarks.
      */
-    private class BookmarksListAdapter extends SimpleCursorAdapter {
+    private class BookmarksListAdapter extends CursorAdapter {
         private static final int VIEW_TYPE_ITEM = 0;
         private static final int VIEW_TYPE_FOLDER = 1;
 
         private static final int VIEW_TYPE_COUNT = 2;
 
         // mParentStack holds folder id/title pairs that allow us to navigate
         // back up the folder heirarchy.
         private LinkedList<Pair<Integer, String>> mParentStack;
 
-        public BookmarksListAdapter(Context context) {
+        public BookmarksListAdapter(Context context, Cursor cursor) {
             // Initializing with a null cursor.
-            super(context, -1, null, new String[] {}, new int[] {});
+            super(context, cursor);
 
             mParentStack = new LinkedList<Pair<Integer, String>>();
 
             // Add the root folder to the stack
             Pair<Integer, String> rootFolder = new Pair<Integer, String>(mFolderId, mFolderTitle);
             mParentStack.addFirst(rootFolder);
         }
 
@@ -237,45 +237,53 @@ public class BookmarksListView extends H
 
         /**
          * {@inheritDoc}
          */
         @Override
         public int getItemViewType(int position) {
             Cursor c = getCursor();
 
-            if (c.moveToPosition(position) &&
-                c.getInt(c.getColumnIndexOrThrow(Bookmarks.TYPE)) == Bookmarks.TYPE_FOLDER) {
+            if (!c.moveToPosition(position)) {
+                throw new IllegalStateException("Couldn't move cursor to position " + position);
+            }
+
+            return getItemViewType(c);
+        }
+
+        /**
+         * Returns the type of the item at the given position in the cursor.
+         *
+         * @param cursor A cursor moved to the required position.
+         * @return The type of the item.
+         */
+        public int getItemViewType(Cursor cursor) {
+            if (cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks.TYPE)) == Bookmarks.TYPE_FOLDER) {
                 return VIEW_TYPE_FOLDER;
             }
 
-            // Default to retuning normal item type
+            // Default to returning normal item type.
             return VIEW_TYPE_ITEM;
         }
 
         /**
          * {@inheritDoc}
          */
         @Override
         public int getViewTypeCount() {
             return VIEW_TYPE_COUNT;
         }
 
         /**
-         * Get the title of the folder for a given position.
+         * Get the title of the folder given a cursor moved to the position.
          *
-         * @param position The position of the view.
+         * @param cursor A cursor moved to the required position.
          * @return The title of the folder at the position.
          */
-        public String getFolderTitle(int position) {
-            Cursor c = getCursor();
-            if (!c.moveToPosition(position)) {
-                return "";
-            }
-
+        public String getFolderTitle(Cursor c) {
             String guid = c.getString(c.getColumnIndexOrThrow(Bookmarks.GUID));
 
             // If we don't have a special GUID, just return the folder title from the DB.
             if (guid == null || guid.length() == 12) {
                 return c.getString(c.getColumnIndexOrThrow(Bookmarks.TITLE));
             }
 
             // Use localized strings for special folder names.
@@ -289,45 +297,41 @@ public class BookmarksListView extends H
                 return getResources().getString(R.string.bookmarks_folder_unfiled);
             }
 
             // If for some reason we have a folder with a special GUID, but it's not one of
             // the special folders we expect in the UI, just return the title from the DB.
             return c.getString(c.getColumnIndexOrThrow(Bookmarks.TITLE));
         }
 
-        /**
-         * {@inheritDoc}
-         */
         @Override
-        public View getView(int position, View convertView, ViewGroup parent) {
-            final int viewType = getItemViewType(position);
+        public void bindView(View view, Context context, Cursor cursor) {
+            final int viewType = getItemViewType(cursor);
 
-            if (convertView == null) {
-                if (viewType == VIEW_TYPE_ITEM) {
-                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_item_row, null);
-                } else {
-                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.bookmark_folder_row, null);
-                }
+            if (viewType == VIEW_TYPE_ITEM) {
+                TwoLinePageRow row = (TwoLinePageRow) view;
+                row.updateFromCursor(cursor);
+            } else {
+                BookmarkFolderView row = (BookmarkFolderView) view;
+                row.setText(getFolderTitle(cursor));
+            }
+        }
+
+        @Override
+        public View newView(Context context, Cursor cursor, ViewGroup parent) {
+            final int viewType = getItemViewType(cursor);
+
+            final int resId;
+            if (viewType == VIEW_TYPE_ITEM) {
+                resId = R.layout.home_item_row;
+            } else {
+                resId = R.layout.bookmark_folder_row;
             }
 
-            Cursor cursor = getCursor();
-            if (!cursor.moveToPosition(position)) {
-                throw new IllegalStateException("Couldn't move cursor to position " + position);
-            }
-
-            if (viewType == VIEW_TYPE_ITEM) {
-                TwoLinePageRow row = (TwoLinePageRow) convertView;
-                row.updateFromCursor(cursor);
-            } else {
-                BookmarkFolderView row = (BookmarkFolderView) convertView;
-                row.setText(getFolderTitle(position));
-            }
-
-            return convertView;
+            return LayoutInflater.from(parent.getContext()).inflate(resId, null);
         }
     }
 
     /**
      * AsyncTask to query the DB for bookmarks.
      */
     private class BookmarksQueryTask extends AsyncTask<Integer, Void, Cursor> {
         @Override