Issue 838: Fixed the bookmark picker crashing on Honeycomb.

git-svn-id: https://zxing.googlecode.com/svn/trunk@1785 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
dswitkin@google.com 2011-05-19 19:23:42 +00:00
parent d7e9842e47
commit 9f519ac146
2 changed files with 66 additions and 16 deletions

View file

@ -0,0 +1,62 @@
// Copyright 2011 Google Inc. All Rights Reserved.
package com.google.zxing.client.android.share;
import com.google.zxing.client.android.R;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* A custom adapter designed to fetch bookmarks from a cursor. Before Honeycomb we used
* SimpleCursorAdapter, but it assumes the existence of an _id column, and the bookmark schema was
* rewritten for HC without one. This caused the app to crash, hence this new class, which is
* forwards and backwards compatible.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
public final class BookmarkAdapter extends BaseAdapter {
private Context context;
private Cursor cursor;
public BookmarkAdapter(Context context, Cursor cursor) {
this.context = context;
this.cursor = cursor;
}
public int getCount() {
return cursor.getCount();
}
public Object getItem(int index) {
// Not used, so no point in retrieving it.
return null;
}
public long getItemId(int index) {
return index;
}
public View getView(int index, View view, ViewGroup viewGroup) {
LinearLayout layout;
if (view == null || !(view instanceof LinearLayout)) {
LayoutInflater factory = LayoutInflater.from(context);
layout = (LinearLayout) factory.inflate(R.layout.bookmark_picker_list_item, viewGroup, false);
} else {
layout = (LinearLayout) view;
}
cursor.moveToPosition(index);
String title = cursor.getString(BookmarkPickerActivity.TITLE_COLUMN);
((TextView) layout.findViewById(R.id.bookmark_title)).setText(title);
String url = cursor.getString(BookmarkPickerActivity.URL_COLUMN);
((TextView) layout.findViewById(R.id.bookmark_url)).setText(url);
return layout;
}
}

View file

@ -16,17 +16,13 @@
package com.google.zxing.client.android.share; package com.google.zxing.client.android.share;
import com.google.zxing.client.android.R;
import android.app.ListActivity; import android.app.ListActivity;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Browser; import android.provider.Browser;
import android.view.View; import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
/** /**
* This class is only needed because I can't successfully send an ACTION_PICK intent to * This class is only needed because I can't successfully send an ACTION_PICK intent to
@ -40,18 +36,13 @@ public final class BookmarkPickerActivity extends ListActivity {
Browser.BookmarkColumns.URL Browser.BookmarkColumns.URL
}; };
private static final int[] TWO_LINE_VIEW_IDS = { static final int TITLE_COLUMN = 0;
R.id.bookmark_title, static final int URL_COLUMN = 1;
R.id.bookmark_url
};
private static final int TITLE_COLUMN = 0;
private static final int URL_COLUMN = 1;
// Without this selection, we'd get all the history entries too // Without this selection, we'd get all the history entries too
private static final String BOOKMARK_SELECTION = "bookmark = 1"; private static final String BOOKMARK_SELECTION = "bookmark = 1";
private Cursor cursor; private Cursor cursor = null;
@Override @Override
protected void onCreate(Bundle icicle) { protected void onCreate(Bundle icicle) {
@ -60,10 +51,7 @@ public final class BookmarkPickerActivity extends ListActivity {
cursor = getContentResolver().query(Browser.BOOKMARKS_URI, BOOKMARK_PROJECTION, cursor = getContentResolver().query(Browser.BOOKMARKS_URI, BOOKMARK_PROJECTION,
BOOKMARK_SELECTION, null, null); BOOKMARK_SELECTION, null, null);
startManagingCursor(cursor); startManagingCursor(cursor);
setListAdapter(new BookmarkAdapter(this, cursor));
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.bookmark_picker_list_item,
cursor, BOOKMARK_PROJECTION, TWO_LINE_VIEW_IDS);
setListAdapter(adapter);
} }
@Override @Override