Created
January 3, 2012 08:58
-
-
Save jcla1/1554163 to your computer and use it in GitHub Desktop.
Using content providers in Android post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jcla1.android.test.bookmarksgetter; | |
import java.util.ArrayList; | |
import java.util.List; | |
import android.app.Activity; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.provider.Browser; | |
import android.provider.Browser.BookmarkColumns; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
public class BookmarkPosterAktivity extends Activity { | |
Button getBookmarksButton; | |
Uri bookmarks = Browser.BOOKMARKS_URI; | |
List<String> bookmarksList = new ArrayList<String>(); | |
String[] columns = new String[] { | |
BookmarkColumns.URL | |
}; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
getBookmarksButton = (Button)this.findViewById(R.id.getBookmarksButton); | |
Cursor managedCursor = managedQuery( | |
bookmarks, // URI of the resource | |
columns, // Which columns to return | |
null, // Which rows to return (all rows) | |
null, // Selection arguments (none) | |
null); // Order the results (in the order they come) | |
getBookmarksButton.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (managedCursor.moveToFirst()) { | |
String url; | |
// Variable for holding the retrieved URL | |
int urlColumn = cur.getColumnIndex(BookmarkColumns.URL); | |
// Reference to the the column containing the URL | |
do { | |
url = cur.getString(urlColumn); | |
// Get the field values | |
bookmarksList.add(url); | |
// Do something with the values. | |
} while (managedCursor.moveToNext()); | |
} | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.database.Cursor; | |
Cursor managedCursor = managedQuery( | |
bookmarks, // URI of the resource | |
columns, // Which columns to return | |
null, // Which rows to return (all rows) | |
null, // Selection arguments (none) | |
null); // Order the results (in the order they come) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.View.OnClickListener; | |
getBookmarksButton.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (managedCursor.moveToFirst()) { | |
String url; | |
// Variable for holding the retrieved URL | |
int urlColumn = cur.getColumnIndex(BookmarkColumns.URL); | |
// Reference to the the column containing the URL | |
do { | |
url = cur.getString(urlColumn); | |
// Get the field values | |
bookmarksList.add(url); | |
// Do something with the values. | |
} while (managedCursor.moveToNext()); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.widget.Button; | |
import android.provider.Browser; | |
import android.net.Uri; | |
import java.util.List; | |
Button getBookmarksButton = (Button)this.findViewById(R.id.getBookmarksButton); | |
// I have set up a simple button in my layout, for which we will write a onClick listener. | |
Uri bookmarks = Browser.BOOKMARKS_URI; | |
// The URI to query for bookmarks and history | |
List<String> bookmarksList = new ArrayList<String>(); | |
// Here we'll store the retrieved data. | |
String[] columns = new String[] { | |
BookmarkColumns.URL | |
}; | |
// Array of all the columns you want to get. If you wanted any more like when it was created, | |
// just add another entry in the Array. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment