ShadowCursorAdapter.java revision 6d2daa795cd59a43fbebc4e10e0a314a5440bb59
1package com.xtremelabs.robolectric.shadows;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import android.content.Context;
7import android.database.Cursor;
8import android.view.View;
9import android.view.ViewGroup;
10import android.widget.CursorAdapter;
11
12import com.xtremelabs.robolectric.internal.Implementation;
13import com.xtremelabs.robolectric.internal.Implements;
14
15
16@Implements(CursorAdapter.class)
17public class ShadowCursorAdapter {
18
19	private Cursor cursor;
20	private List<View> views = new ArrayList<View>();
21
22    public void __constructor__( Context ctx, Cursor curs, boolean autoRequery ) {
23    	cursor = curs;
24    }
25
26    @Implementation
27	public void changeCursor( Cursor curs ) {
28    	if( cursor != null && !cursor.isClosed() ) {
29    		cursor.close();
30    	}
31    	cursor = curs;
32    }
33
34    @Implementation
35    public Cursor getCursor() {
36    	return cursor;
37    }
38
39    @Implementation
40    public int getCount() {
41    	if ( cursor == null ) {
42    		return 0;
43    	}
44    	return cursor.getCount();
45    }
46
47    @Implementation
48    public Object getItem(int position) {
49    	if ( cursor == null ) {
50    		return null;
51    	}
52
53    	cursor.moveToPosition(position);
54    	return new Integer(position);
55    }
56
57    @Implementation
58    public long getItemId(int position) {
59    	if ( cursor == null ) {
60    		return 0;
61    	}
62
63    	cursor.moveToPosition(position);
64    	int rowIdColumn = cursor.getColumnIndexOrThrow("_id");
65    	return cursor.getLong(rowIdColumn);
66    }
67
68    @Implementation
69    public View getView(int position, View convertView, ViewGroup parent) {
70    	if ( cursor == null ) {
71    		return null;
72    	}
73
74    	if ( convertView != null ) {
75    		return convertView;
76    	}
77
78    	return views.get(position);
79    }
80
81    /**
82     * Non-Android API.  Set a list of views to be returned for successive
83     * calls to getView().
84     *
85     * @param views
86     */
87    public void setViews( List<View> views ) {
88    	this.views = views;
89    }
90}
91