Both phone and emulator are 2.2.
Trying to create a gridview with a custom SimpleCursorAdapter class. The gridview contains a image and text view.
The code below works perfectly on the emulator but nothing shows up when I try it on the phone.
I have noticed that only the constructor is called from the phone (not newView or bindView)! Any help?
public class GridAdapter extends SimpleCursorAdapter {
private Context context;
private int mLayout;
public GridAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
mLayout = layout;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(mLayout, null);
v.setLayoutParams(new GridView.LayoutParams(150,150));
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
int nameCol = c.getColumnIndex("show_title");
String name = c.getString(nameCol);
TextView tv = (TextView) v.findViewById(R.id.textView1);
if (name != null) {
tv.setText(name);
}
ImageView iv = (ImageView) v.findViewById(R.id.album_image);
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setImageResource(R.drawable.icon);
}
}
Here is my main xml file:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
Here is my view for each grid position:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/single_item_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ImageView
android:id = "@+id/album_image"
android:adjustViewBounds="true"
android:layout_width = "fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:text="TextView"
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
And finally here is my onCreate(well some of it):
gridview = (GridView) findViewById(R.id.gridview);
Cursor c = mDbHelper.fetchAllShows();
int[] to = new int[] {R.id.name};
GridAdapter ga = new GridAdapter(this,R.layout.icon,c,new String[] {"show_title"},
to);
gridview.setAdapter(ga);
(and yes, I know I am doign nothing with the imageview inside of each position yet).
I ended up taking Herrmann’s advice here. I removed the reference to the context and to the cursor. Also renamed the gridview in my layout and changed the appropriate references in my code.