麦子学院Android开发教程如何添加书签

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

经常有网友问如何添加书签,其实很简单就是利用ContentResolver向浏览器的provider中添加相应书签项。

Java代码
1.ContentValues inputValue = new ContentValues();
2.//Bookmark值为1
3.inputValue.put(android.provider.Browser.BookmarkColumns.BOOKMARK, 1);
4.//添加书签Title
5.inputValue.put(android.provider.Browser.BookmarkColumns.TITLE, title);
6.//添加书签URL
7.inputValue.put(android.provider.Browser.BookmarkColumns.URL, url);
8.ContentResolver cr = getContentResolver();
9.//向浏览器添加该书签
10.Uri uri = cr.insert(android.provider.Browser.BOOKMARKS_URI, inputValue);
11.
复制代码
加入权限
Java代码
1.<uses-permission
android:name="com.android.browser.permission.READ_HISTORY_BOOKMAR KS" />
2.<uses-permission
android:name="com.android.browser.permission.WRITE_HISTORY_BOOKMAR KS"/>
3.
复制代码
运行结果:
Bookmarker.java:
Java代码
1.package com.iaiai;
2.
3.import android.app.Activity;
4.import android.app.AlertDialog;
5.import android.app.Dialog;
6.import android.content.ContentResolver;
7.import android.content.ContentValues;
8.import android.content.DialogInterface;
9.import android.content.Intent;
10.import android.content.SharedPreferences;
11.import .Uri;
12.import android.os.Bundle;
13.import android.provider.Browser;
14.import android.view.Menu;
15.import android.view.MenuItem;
16.import android.view.View;
17.import android.view.View.OnClickListener;
18.import android.widget.AdapterView;
19.import android.widget.AdapterView.OnItemClickListener;
20.import android.widget.ImageButton;
21.import android.widget.ListView;
22.
23./**
24.*
25.* <p>
26.* Title: Bookmarker.java
27.* </p>
28.* <p>
29.* E-Mail: 176291935@
30.* </p>
31.* <p>
32.* QQ: 176291935
33.* </p>
34.* <p>
35.* Http:
36.* </p>
37.* <p>
38.* Create time: 2011-8-29
39.* </p>
40.*
41.* @author 丸子
42.* @version 0.0.1
43.*/
44.public class Bookmarker extends Activity implements OnItemClickListener,
45. OnClickListener {
46.
47. /** Called when the activity is first created. */
48. private ListView mListView;
49. private BookmarkAdapter bookmarkAdapter;
50. private ImageButton upButton;
51. private ImageButton downButton;
52. private ImageButton deleteButton;
53. private ImageButton launchButton;
54. private ImageButton topButton;
55. private ImageButton bottomButton;
56. private static final int DIALOG_YES_NO_MESSAGE = 1;
57. private static final int DIALOG_WELCOME = 2;
58. private int currentPos = -1;
59.
60. private static final String PREFS_NAME = "BookmarkerPrefs";
61. private static final String LAUNCHED_KEY = "LAUNCHED";
62. private static final int MENU_ABOUT = 0;
63. private static final int MENU_ADD = 1;
64.
65. @Override
66. public void onCreate(Bundle icicle) {
67. super.onCreate(icicle);
68. setContentView(yout.main);
69.
70. ContentValues inputValue = new ContentValues();
71. inputValue.put(android.provider.Browser.BookmarkColumns.BOOKMARK,
1);
72. inputValue.put(android.provider.Browser.BookmarkColumns.TITLE, "丸子
");
73. inputValue.put(android.provider.Browser.BookmarkColumns.URL,
"");
74. ContentResolver cr = getContentResolver();
75. Uri uri = cr.insert(android.provider.Browser.BOOKMARKS_URI,
inputValue);
76.
77. bookmarkAdapter = new BookmarkAdapter(this, "");// new
78. //
ArrayAdapter<Bookmark>(this,
79. //
yout.simple_list_item_single_choice,bookmarks);
80. mListView = ((ListView) findViewById(R.id.mylistview));
81. mListView.setAdapter(bookmarkAdapter);
82. mListView.setOnItemClickListener(this);
83.
84. upButton = (ImageButton) findViewById(R.id.upbutton);
85. downButton = (ImageButton) findViewById(R.id.downbutton);
86. deleteButton = (ImageButton) findViewById(R.id.deletebutton);
87. launchButton = (ImageButton) findViewById(unchbutton);
88. topButton = (ImageButton) findViewById(R.id.topbutton);
89. bottomButton = (ImageButton) findViewById(R.id.bottombutton);
90.
91. upButton.setOnClickListener(this);
92. downButton.setOnClickListener(this);
93. deleteButton.setOnClickListener(this);
94. launchButton.setOnClickListener(this);
95. topButton.setOnClickListener(this);
96. bottomButton.setOnClickListener(this);
97.
98. // if we have some bookmarks select the first and start everything
99. if (bookmarkAdapter.getCount() > 0) {
100. bookmarkAdapter.initialiseAllRows();
101. currentPos = 0;
102. bookmarkAdapter.setSelected(currentPos);
103. }
104.
105. // Make sure the welcome message only appears on first launch 106. SharedPreferences settings =
getSharedPreferences(PREFS_NAME, 0);
107. if (settings != null) {
108. boolean launchedPreviously =
settings.getBoolean(LAUNCHED_KEY,
109. false);
110. if (!launchedPreviously) {
111. showDialog(DIALOG_WELCOME);
112. SharedPreferences.Editor editor = settings.edit(); 113. editor.putBoolean(LAUNCHED_KEY, true);
114. mit();
115. }
116. }
117. }
118.
119. /**
120. * Invoked during init to give the Activity a chance to set up its Menu. 121. *
122. * @param menu
123. * the Menu to which entries may be added
124. * @return true
125. */
126. @Override
127. public boolean onCreateOptionsMenu(Menu menu) {
128. super.onCreateOptionsMenu(menu);
129. menu.add(0, MENU_ADD, 0, "Add New Bookmark").setIcon( 130. android.R.drawable.ic_menu_info_details);
131. menu.add(0, MENU_ABOUT, 1, R.string.menu_about).setIcon( 132. android.R.drawable.ic_menu_info_details);
133.
134. return true;
135. }
136.
137. /**
138. * Invoked when the user selects an item from the Menu.
139. *
140. * @param item
141. * the Menu entry which was selected
142. * @return true if the Menu item was legit (and we consumed it), false 143. * otherwise
144. */
145. @Override
146. public boolean onOptionsItemSelected(MenuItem item) {
147. if (item.getItemId() == MENU_ABOUT) {
148. showDialog(DIALOG_WELCOME);
149. return true;
150. } else if (item.getItemId() == MENU_ADD) {
151. Browser.saveBookmark(this, "New Bookmark", "http://"); 152. bookmarkAdapter.notifyDataSetChanged();
153. return true;
154. } else
155. return false;
156. }
157.
158. public void onItemClick(AdapterView<?> arg0, View view, int position, 159. long id) {
160. currentPos = position;
161. bookmarkAdapter.setSelected(position);
162. }
163.
164. public void onClick(View v) {
165.
166. if (v == upButton && currentPos > 0) {
167. bookmarkAdapter.moveItemUp(currentPos);
168. currentPos--;
169. } else if (v == downButton && currentPos >= 0
170. && currentPos < bookmarkAdapter.getCount() - 1) { 171. bookmarkAdapter.moveItemDown(currentPos);
172. currentPos++;
173. } else if (v == deleteButton) {
174. showDialog(DIALOG_YES_NO_MESSAGE);
175. } else if (v == topButton) {
176. currentPos = bookmarkAdapter.moveToTop(currentPos); 177. } else if (v == bottomButton) {
178. currentPos = bookmarkAdapter.moveToBottom(currentPos); 179. } else if (v == launchButton) {
180. String url = bookmarkAdapter.getUrl(currentPos);
181. if (url != null) {
182. Intent i = new Intent("android.intent.action.VIEW", 183. Uri.parse(url));
184. startActivity(i);
185. }
186. }
187.
188. }
189.
190. protected Dialog onCreateDialog(int id) {
191. if (id == DIALOG_YES_NO_MESSAGE) {
192. return new AlertDialog.Builder(Bookmarker.this)
193. .setIcon(android.R.drawable.ic_dialog_alert) 194. .setTitle("Confirm Delete")
195. .setMessage(
196. "Are you sure you want to delete this bookmark?")
197. .setPositiveButton("OK",
198. new DialogInterface.OnClickListener() { 199. public void onClick(DialogInterface dialog,
200. int whichButton) { 201. bookmarkAdapter.deleteRow( currentPos);
202. }
203. })
204. .setNegativeButton("Cancel",
205. new DialogInterface.OnClickListener() { 206. public void onClick(DialogInterface dialog,
207. int whichButton) { 208. // do nothing
209. }
210. }).create();
211. } else if (id == DIALOG_WELCOME) {
212. String message = getString(R.string.welcome);
213. if (bookmarkAdapter.getCount() < 1) {
214. message += getString(R.string.nomarks);
215. }
216.
217. AlertDialog dialog = new
AlertDialog.Builder(Bookmarker.this)
218. .create();// new AlertDialog(Bookmarker.this); 219. dialog.setTitle("Welcome");
220. dialog.setMessage(message);
221. dialog.setButton("OK", new DialogInterface.OnClickListener() {
222. public void onClick(DialogInterface dialog, int whichButton) {
223. if (bookmarkAdapter.getCount() < 1) {
224. finish();
225. }
226. }
227. });
228.
229. dialog.setCancelable(true); 230. return dialog;
231. } else
232. return null;
233. }
234.}
235.
复制代码
BookmarkItem.java:
Java代码
1.package com.iaiai;
2.
3.import android.content.Context;
4.import android.graphics.Bitmap;
5.import youtInflater;
6.import android.widget.ImageView;
7.import android.widget.RelativeLayout;
8.import android.widget.TextView;
9.
10./**
11.*
12.* <p>
13.* Title: BookmarkItem.java
14.* </p>
15.* <p>
16.* E-Mail: 176291935@
17.* </p>
18.* <p>
19.* QQ: 176291935
20.* </p>
21.* <p>
22.* Http:
23.* </p>
24.* <p>
25.* Create time: 2011-8-29
26.* </p>
27.*
28.* @author 丸子
29.* @version 0.0.1
30.*/
31.class BookmarkItem extends RelativeLayout {
32.
33. private TextView mTextView; // title
34. private TextView mUrlText; // url
35. private ImageView mImageView; // favicon
36. private LayoutInflater mFactory;
37. private RelativeLayout mBookmarkItem;
38.
39. /**
40. * Instantiate a bookmark item, including a default favicon.
41. *
42. * @param context
43. * The application context for the item.
44. */
45. BookmarkItem(Context context) {
46. super(context);
47.
48. mFactory = LayoutInflater.from(context);
49. mFactory.inflate(yout.bookmark_item, this);
50. mTextView = (TextView) findViewById(R.id.title);
51. mUrlText = (TextView) findViewById(R.id.url);
52. mImageView = (ImageView) findViewById(R.id.favicon);
53. mBookmarkItem = (RelativeLayout)
findViewById(R.id.bookmarkitem);
54. }
55.
56. public void setSelected() {
57. mBookmarkItem.setBackgroundColor(0xFFFF6633);
58. mUrlText.setTextColor(0xFF000000);
59. }
60.
61. public void setUnselected() {
62. mBookmarkItem.setBackgroundColor(0xFF000000);
63. mUrlText.setTextColor(0xFFAAAAAA);
64. }
65.
66. /**
67. * Copy this BookmarkItem to item.
68. *
69. * @param item
70. * BookmarkItem to receive the info from this BookmarkItem.
71. */
72. /* package */void copyTo(BookmarkItem item) {
73. item.mTextView.setText(mTextView.getText());
74. item.mUrlText.setText(mUrlText.getText());
75. item.mImageView.setImageDrawable(mImageView.getDrawable());
76. }
77.
78. /**
79. * Return the name assigned to this bookmark item.
80. */
81. /* package */CharSequence getName() {
82. return mTextView.getText();
83. }
84.
85. /**
86. * Return the TextView which holds the name of this bookmark item.
87. */
88. /* package */TextView getNameTextView() {
89. return mTextView;
90. }
91.
92. /**
93. * Set the favicon for this item.
94. *
95. * @param b
96. * The new bitmap for this item. If it is null, will use the
97. * default.
98. */
99. /* package */void setFavicon(Bitmap b) {
100. if (b != null) {
101. mImageView.setImageBitmap(b);
102. } else {
103. mImageView.setImageResource(R.drawable.app_web_brows er_sm);
104. }
105. }
106.
107. /**
108. * Set the new name for the bookmark item.
109. *
110. * @param name
111. * The new name for the bookmark item.
112. */
113. /* package */void setName(String name) {
114. mTextView.setText(name);
115. }
116.
117. /**
118. * Set the new url for the bookmark item.
119. *
120. * @param url
121. * The new url for the bookmark item.
122. */
123. /* package */void setUrl(String url) {
124. mUrlText.setText(url);
125. }
126.}
127.
复制代码
BookmarkAdapter.java:
Java代码
1.package com.iaiai;
2.
3.import java.io.ByteArrayOutputStream;
4.import java.util.Date;
5.
6.import android.content.ContentResolver;
7.import android.content.ContentUris;
8.import android.content.ContentValues;
9.import android.database.ContentObserver;
10.import android.database.Cursor;
11.import android.database.DataSetObserver;
12.import android.graphics.Bitmap;
13.import android.graphics.BitmapFactory;
14.import .Uri;
15.import android.os.Bundle;
16.import android.os.Handler;
17.import android.provider.Browser;
18.import android.view.View;
19.import android.view.ViewGroup;
20.import android.webkit.WebIconDatabase;
21.import android.webkit.WebIconDatabase.IconListener;
22.import android.widget.BaseAdapter;
23.
24./**
25.*
26.* <p>
27.* Title: BookmarkAdapter.java
28.* </p>
29.* <p>
30.* E-Mail: 176291935@
31.* </p>
32.* <p>
33.* QQ: 176291935
34.* </p>
35.* <p>
36.* Http:
37.* </p>
38.* <p>
39.* Create time: 2011-8-29
40.* </p>
41.*
42.* @author 丸子
43.* @version 0.0.1
44.*/
45.class BookmarkAdapter extends BaseAdapter {
46.
47. private Cursor mCursor;
48. private int mCount;
49. private ContentResolver mContentResolver;
50. private ChangeObserver mChangeObserver;
51. private DataSetObserver mDataSetObserver;
52. private boolean mDataValid;
53. private Bookmarker mBookmarker;
54.
55. // Implementation of WebIconDatabase.IconListener
56. private class IconReceiver implements IconListener {
57. public void onReceivedIcon(String url, Bitmap icon) {
58. updateBookmarkFavicon(mContentResolver, url, icon);
59. }
60. }
61.
62. // Instance of IconReceiver
63. private final IconReceiver mIconReceiver = new IconReceiver();
64.
65. /**
66. * Create a new BrowserBookmarksAdapter.
67. *
68. * @param b
69. * BrowserBookmarksPage that instantiated this. Necessary so it
70. * will adjust its focus appropriately after a search.
71. */
72. public BookmarkAdapter(Bookmarker b, String curPage) {
73. this(b, curPage, false);
74. }
75.
76. /**
77. * Create a new BrowserBookmarksAdapter.
78. *
79. * @param b
80. * BrowserBookmarksPage that instantiated this. Necessary so it
81. * will adjust its focus appropriately after a search.
82. */
83. public BookmarkAdapter(Bookmarker b, String curPage, boolean
createShortcut) {
84. mDataValid = false;
85. mBookmarker = b;
86. mContentResolver = b.getContentResolver();
87. mChangeObserver = new ChangeObserver();
88. mDataSetObserver = new MyDataSetObserver();
89. // FIXME: Should have a default sort order that the user selects.
90. search();
91. // FIXME: This requires another query of the database after the
92. // initial search(null). Can we optimize this?
93. Browser.requestAllIcons(mContentResolver,
94. Browser.BookmarkColumns.FAVICON + " is NULL AND "
95. + Browser.BookmarkColumns.BOOKMARK + "
== 1",
96. mIconReceiver);
97. }
98.
99. public void initialiseAllRows() {
100. int processed = 0;
101.
102. // ensures each row has a created time
103. while (processed < mCount) {
104. for (int i = 0; i < mCount; i++) {
105. Bundle thisRow = getRow(i, true);
106. Long thisCreateTime = thisRow
107. .getLong(Browser.BookmarkColumns.CREATE
D);
108. if (thisCreateTime == null || thisCreateTime < 900) { 109. // get createTime of row above and subtract 1000; 110. long createTime = (mCount - i) * 1000;
111. if (i > 0) {
112. Bundle prevRow = getRow(i, true);
113. Long prevCreateTime = prevRow
114. .getLong(Browser.BookmarkColum ns.CREATED);
115. if (prevCreateTime != null && prevCreateTime > 1001) {
116. createTime = prevCreateTime - 1000; 117. }
118. }
119.
120. thisRow.putLong(Browser.BookmarkColumns.CRE ATED, createTime);
121. updateRow(thisRow, true);
122. break;// underlying dataset has updated so quit the loop and
123. // start again
124. }
125. processed = i + 1;
126. }
127. }
128. }
129.
130. /**
131. * Return a hashmap with one row's Title, Url, and favicon.
132. *
133. * @param position
134. * Position in the list.
135. * @return Bundle Stores title, url of row position, favicon, and id for the
136. * url. Return a blank map if position is out of range.
137. */
138. public Bundle getRow(int position, boolean includeCreateTime) { 139. Bundle map = new Bundle();
140. if (position < 0 || position >= mCount) {
141. return map;
142. }
143. mCursor.moveToPosition(position);
144. String url =
mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
145. map.putString(Browser.BookmarkColumns.TITLE,
146. mCursor.getString(Browser.HISTORY_PROJECTION_TITL E_INDEX));
147. map.putString(Browser.BookmarkColumns.URL, url);
148. byte[] data =
mCursor.getBlob(Browser.HISTORY_PROJECTION_FAVICON_INDEX);
149. if (data != null) {
150. map.putByteArray("FAV_BYTES", data);
151. map.putParcelable(Browser.BookmarkColumns.FAVICON, 152. BitmapFactory.decodeByteArray(data, 0, data.length));
153. }
154.
155. if (includeCreateTime && createdColumnIndex != -1) {
156. Long createTime = mCursor.getLong(createdColumnIndex); 157. if (createTime != null && createTime != 0) {
158. map.putLong(Browser.BookmarkColumns.CREATED, createTime);
159. }
160. }
161.
162. map.putInt("id",
mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX));
163.
164. return map;
165. }
166.
167. /**
168. * Update a row in the database with new information. Requeries the database
169. * if the information has changed.
170. *
171. * @param map
172. * Bundle storing id, title and url of new information
173. */
174. public void updateRow(Bundle map, boolean includeCreateTime) { 175.
176. // Find the record
177. int id = map.getInt("id");
178. int position = -1;
179. for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor 180. .moveToNext()) {
181. if (mCursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX) == id) {
182. position = mCursor.getPosition();
183. break;
184. }
185. }
186. if (position < 0) {
187. return;
188. }
189.
190. mCursor.moveToPosition(position);
191. ContentValues values = new ContentValues();
192. String title = map.getString(Browser.BookmarkColumns.TITLE); 193. if (!title.equals(mCursor
194. .getString(Browser.HISTORY_PROJECTION_TITLE_INDEX ))) {
195. values.put(Browser.BookmarkColumns.TITLE, title);
196. }
197. String url = map.getString(Browser.BookmarkColumns.URL); 198. if
(!url.equals(mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX))) { 199. values.put(Browser.BookmarkColumns.URL, url);
200. }
201.
202. byte[] favicon = map.getByteArray("FAV_BYTES");
203. if (favicon != null) {
204. values.put(Browser.BookmarkColumns.FAVICON, favicon); 205. }
206.
207. if (includeCreateTime) {
208. Long createTime =
map.getLong(Browser.BookmarkColumns.CREATED);
209. if (createTime != null)
210. values.put(Browser.BookmarkColumns.CREATED, createTime);
211. }
212.
213. if (values.size() > 0
214. &&
mContentResolver.update(Browser.BOOKMARKS_URI, values,
215. "_id = " + id, null) != -1) {
216. refreshList();
217. }
218. }
219.
220. /**
221. * Delete a row from the database. Requeries the database. Does nothing if
222. * the provided position is out of range.
223. *
224. * @param position
225. * Position in the list.
226. */
227. public void deleteRow(int position) {
228. if (position < 0 || position >= getCount()) {
229. return;
230. }
231. mCursor.moveToPosition(position);
232. String url =
mCursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
233. WebIconDatabase.getInstance().releaseIconForPageUrl(url); 234. Uri uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI, 235. mCursor.getInt(Browser.HISTORY_PROJECTION_ID_IND EX));
236. int numVisits =
mCursor.getInt(Browser.HISTORY_PROJECTION_VISITS_INDEX);
237. if (0 == numVisits) {
238. mContentResolver.delete(uri, null, null);
239. } else {
240. // It is no longer a bookmark, but it is still a visited site. 241. ContentValues values = new ContentValues();
242. values.put(Browser.BookmarkColumns.BOOKMARK, 0); 243. mContentResolver.update(uri, values, null, null);
244. }
245. refreshList();
246. }
247.
248. /**
249. * Refresh list to recognize a change in the database.
250. */
251. public void refreshList() {
252. searchInternal();
253. }
254.
255. /**
256. * Search the database for bookmarks that match the input string. 257. *
258. * @param like
259. * String to use to search the database. Strings with spaces are 260. * treated as having multiple search terms using the OR operator.
261. * Search both the title and url.
262. */
263. public void search() {
264. searchInternal();
265. }
266.
267. /**
268. * Update the bookmark's favicon.
269. *
270. * @param cr
271. * The ContentResolver to use.
272. * @param url
273. * The url of the bookmark to update.
274. * @param favicon
275. * The favicon bitmap to write to the db.
276. */
277. /* package */static void updateBookmarkFavicon(ContentResolver cr, 278. String url, Bitmap favicon) {
279. if (url == null || favicon == null) {
280. return;
281. }
282. // Strip the query.
283. int query = url.indexOf('?');
284. String noQuery = url;
285. if (query != -1) {
286. noQuery = url.substring(0, query);
287. }
288. url = noQuery + '?';
289. // Use noQuery to search for the base url (i.e. if the url is
290. // /?rs=1, search for
)
291. // Use url to match the base url with other queries (i.e. if the url is 292. // /m, search for
293. // /m?some_query)
294. final String[] selArgs = new String[] { noQuery, url };
295. final String where = "(" + Browser.BookmarkColumns.URL + "
== ? OR "
296. + Browser.BookmarkColumns.URL + " GLOB ? || '*') AND "
297. + Browser.BookmarkColumns.BOOKMARK + " == 1"; 298. final String[] projection = new String[]
{ Browser.BookmarkColumns._ID };
299. final Cursor c = cr.query(Browser.BOOKMARKS_URI, projection, where,
300. selArgs, null);
301. boolean succeed = c.moveToFirst();
302. ContentValues values = null;
303. while (succeed) {
304. if (values == null) {
305. final ByteArrayOutputStream os = new ByteArrayOutputStream();
306. press(pressFormat.PNG, 100, os);
307. values = new ContentValues();
308. values.put(Browser.BookmarkColumns.FAVICON, os.toByteArray());
309. }
310. cr.update(
311. ContentUris.withAppendedId(Browser.BOOKMARK S_URI,
312. c.getInt(0)), values, null, null);
313. succeed = c.moveToNext();
314. }
315. c.close();
316. }
317.
318. /**
319. * Internal function used in search, sort, and refreshList.
320. */
321. private int createdColumnIndex = -1;
322.
323. private void searchInternal() {
324. if (mCursor != null) {
325. mCursor.unregisterContentObserver(mChangeObserver); 326. mCursor.unregisterDataSetObserver(mDataSetObserver); 327. mCursor.deactivate();
328. }
329.
330. // need to add the created date column to the query
331. String[] columns = new
String[Browser.HISTORY_PROJECTION.length + 1];
332. System.arraycopy(Browser.HISTORY_PROJECTION, 0, columns, 0, 333. Browser.HISTORY_PROJECTION.length);
334. createdColumnIndex = columns.length - 1;
335. columns[createdColumnIndex] =
Browser.BookmarkColumns.CREATED;
336.
337. String whereClause = Browser.BookmarkColumns.BOOKMARK + " == 1";
338. String orderBy = Browser.BookmarkColumns.CREATED + " DESC"; 339. String[] selectionArgs = null;
340. mCursor = mContentResolver.query(Browser.BOOKMARKS_URI, columns,
341. whereClause, selectionArgs, orderBy);
342. mCursor.registerContentObserver(mChangeObserver);
343. mCursor.registerDataSetObserver(mDataSetObserver);
344.
345. mDataValid = true;
346. notifyDataSetChanged();
347.
348. mCount = mCursor.getCount();
349. }
350.
351. /**
352. * How many items should be displayed in the list.
353. *
354. * @return Count of items.
355. */
356. public int getCount() {
357. if (mDataValid) {
358. return mCount;
359. } else {
360. return 0;
361. }
362. }
363.
364. public boolean areAllItemsEnabled() {
365. return true;
366. }
367.
368. public boolean isEnabled(int position) {
369. return true;
370. }
371.
372. /**
373. * Get the data associated with the specified position in the list. 374. *
375. * @param position
376. * Index of the item whose data we want.
377. * @return The data at the specified position.
378. */
379. public Object getItem(int position) {
380. return null;
381. }
382.
383. /**
384. * Get the row id associated with the specified position in the list.
385. *
386. * @param position
387. * Index of the item whose row id we want.
388. * @return The id of the item at the specified position.
389. */
390. public long getItemId(int position) {
391. return position;
392. }
393.
394. int mCurrentSelection = -1;
395.
396. public void setSelected(int position) {
397. mCurrentSelection = position;
398. notifyDataSetInvalidated();
399. }
400.
401. /**
402. * Get a View that displays the data at the specified position in the list. 403. *
404. * @param position
405. * Index of the item whose view we want.
406. * @return A View corresponding to the data at the specified position. 407. */
408. public View getView(int position, View convertView, ViewGroup parent) {
409. if (!mDataValid) {
410. throw new IllegalStateException(
411. "this should only be called when the cursor is valid");
412. }
413. if (position < 0 || position > mCount) {
414. throw new AssertionError(
415. "BrowserBookmarksAdapter tried to get a view out of range");
416. }
417.
418. if (convertView == null) {
419. convertView = new BookmarkItem(mBookmarker);
420. }
421. bind((BookmarkItem) convertView, position);
422. if (position == mCurrentSelection) {
423. ((BookmarkItem) convertView).setSelected();
424. } else {
425. ((BookmarkItem) convertView).setUnselected();
426. }
427.
428. return convertView;
429. }
430.
431. /**
432. * Return the title for this item in the list.
433. */
434. public String getTitle(int position) {
435. return getString(Browser.HISTORY_PROJECTION_TITLE_INDEX, position);
436. }
437.
438. /**
439. * Return the Url for this item in the list.
440. */
441. public String getUrl(int position) {
442. return getString(Browser.HISTORY_PROJECTION_URL_INDEX, position);
443. }
444.
445. /**
446. * Private helper function to return the title or url.
447. */
448. private String getString(int cursorIndex, int position) {
449. if (position < 0 || position > mCount) {
450. return "";
451. }
452. mCursor.moveToPosition(position);
453. return mCursor.getString(cursorIndex);
454. }
455.。

相关文档
最新文档