]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - BarCodeScanner/mobile/src/main/java/cz/cvut/fel/dce/barcodescanner/share/BookmarkAdapter.java
Initial commit
[hornmich/skoda-qr-demo.git] / BarCodeScanner / mobile / src / main / java / cz / cvut / fel / dce / barcodescanner / share / BookmarkAdapter.java
1 /*
2  * Copyright (C) 2011 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package cz.cvut.fel.dce.barcodescanner.share;
18
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.BaseAdapter;
25 import android.widget.LinearLayout;
26 import android.widget.TextView;
27
28 import cz.cvut.fel.dce.barcodescanner.R;
29
30 /**
31  * A custom adapter designed to fetch bookmarks from a cursor. Before Honeycomb we used
32  * SimpleCursorAdapter, but it assumes the existence of an _id column, and the bookmark schema was
33  * rewritten for HC without one. This caused the app to crash, hence this new class, which is
34  * forwards and backwards compatible.
35  *
36  * @author dswitkin@google.com (Daniel Switkin)
37  */
38 final class BookmarkAdapter extends BaseAdapter {
39   private final Context context;
40   private final Cursor cursor;
41
42   BookmarkAdapter(Context context, Cursor cursor) {
43     this.context = context;
44     this.cursor = cursor;
45   }
46
47   @Override
48   public int getCount() {
49     return cursor.getCount();
50   }
51
52   @Override
53   public Object getItem(int index) {
54     // Not used, so no point in retrieving it.
55     return null;
56   }
57
58   @Override
59   public long getItemId(int index) {
60     return index;
61   }
62
63   @Override
64   public View getView(int index, View view, ViewGroup viewGroup) {
65     View layout;
66     if (view instanceof LinearLayout) {
67       layout = view;
68     } else {
69       LayoutInflater factory = LayoutInflater.from(context);
70       layout = factory.inflate(R.layout.bookmark_picker_list_item, viewGroup, false);
71     }
72
73     if (!cursor.isClosed()) {
74       cursor.moveToPosition(index);
75       CharSequence title = cursor.getString(BookmarkPickerActivity.TITLE_COLUMN);
76       ((TextView) layout.findViewById(R.id.bookmark_title)).setText(title);
77       CharSequence url = cursor.getString(BookmarkPickerActivity.URL_COLUMN);
78       ((TextView) layout.findViewById(R.id.bookmark_url)).setText(url);
79     } // Otherwise... just don't update as the object is shutting down
80     return layout;
81   }
82 }