Search code examples
androidandroid-activitybookmarks

Appropriate storage and display of my bookmarks/history activity?


I want a simple bookmarks and/or history for my app, and I'm wondering what the most appropriate storage would be? A text in a text file or preference, or perhaps a database? Which would be most flexible across updates, and efficient for space and lookup time?

For the display, I'm thinking this would be a good starting point, but would it be easy to add an icon to some items?

Edit:

I finally set up a Bookmark activity that should connect to a database:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bookmarkview);
    Cursor cursor = managedQuery(getIntent().getData(), new String[] {Bookmark.TITLE, Bookmark.URL},
            null, null, Bookmark.DEFAULT_SORT_ORDER);

    setListAdapter(new SimpleCursorAdapter(this, R.layout.bookmarkitem, cursor,
            new String[] { Bookmark.TITLE }, new int[] { android.R.id.text1 }));
    findViewById(R.id.addBookmark).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ContentValues values = new ContentValues();
            values.put("url", _url);
            values.put("title", _title);

            // When the update completes,
            // the content provider will notify the cursor of the change, which will
            // cause the UI to be updated.
            getContentResolver().update(_myuri, values, null, null);
        }
    });
}

Bookmark.java:

package com.tunes.viewer.Bookmarks;

import android.net.Uri;
import android.provider.BaseColumns;

/*
 * Database will have:
 * pk - primary key
 * title - the name of the bookmark.
 * url - the url.
 */
public class Bookmark implements BaseColumns{

    public static final String AUTHORITY = "com.tunes.viewer";

    /**
     * The content:// style URL for this table
     */
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/Bookmarks");

    /**
     * The MIME type of {@link #CONTENT_URI} providing a directory of notes.
     */
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.google.note";

    /**
     * The MIME type of a {@link #CONTENT_URI} sub-directory of a single note.
     */
    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.google.note";

    /**
     * The default sort order for this table
     */
    public static final String DEFAULT_SORT_ORDER = "title";

    /**
     * The title of the note
     * <P>Type: TEXT</P>
     */
    public static final String TITLE = "title";

    /**
     * The url
     * <P>Type: TEXT</P>
     */
    public static final String URL = "url";

}

I seem to have fixed most of the problems I was having, but unfortunately it doesn't add to the database when I click the Add button (calling the onclick above). Furthermore, I added data to the database, but it doesn't show up in the view. What's wrong with the cursor/adapter here? Full source is here.


Solution

  • i would suggest, you go with database. It will be easy and efficient solution for your requirement.

    A single table in sqlite will suffice to your requirements. as you will need to maintain a list of url you visited. this table will also serve your requirement of storing bookmark.

    your table format could be something like this.

    _____________________________________________________________________________________
    Id(Auto-increment) | Title of page | Url of Page |name of icon(if needed) |isBookmark |
    _____________________________________________________________________________________
    

    This could be a good structure to achieve you requirement. set isBookmark to 0/1 to set specific link as bookmark or unbookmark it.

    EDIT

    • I did not suggest you to use SharedPreferences and i wont (though it is straight forword and easy to implement) and reason lies in very definition of SharedPreferences which says:
      "The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings."
      Now i can not imagine a single way to store ArrayList<String>(Urls) in one of these primitive datatypes.

    • There is one more work around.and it is Object Serialization. you can save your complete arraylist instance to a file and next time when you need this object, deseralize it similarly.. Here is the sample code for Serialization.

    .

    public void serializeMap(ArrayList<String> list) {
        try {
            FileOutputStream fStream = openFileOutput(namefile.bin, Context.MODE_PRIVATE) ;
            ObjectOutputStream oStream = new ObjectOutputStream(fStream);
            oStream.writeObject(list);        
            oStream.flush();
            oStream.close();
            Log.v("Serialization success", "Success");
        } catch (Exception e) {
            Log.v("IO Exception", e.getMessage());
        }
    }  
    

    But this approach is not much recommended though.