手机和模拟器都是2.2。Android Gridview在模拟器上工作,但没有电话!
尝试使用自定义SimpleCursorAdapter类创建gridview。 gridview包含一个图像和文本视图。
下面的代码在模拟器上完美工作,但在手机上尝试时没有显示出来。
我注意到,只有构造函数是从手机中调用的(不是newView或bindView)!任何帮助?
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);
}
}
这里是我的主XML文件:
<?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"
/>
这里是我的每个网格位置视野:
<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>
最后,这里是我的onCreate(以及它的一些):
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);
(是的,我知道我是doign没有任何与每个位置内的图像视图)。
这是Android开发人员网站上的示例的公然副本!在这种情况下,他们对我没有帮助:( – IamAlexAlright 2011-06-17 17:02:51
嗨亚历克斯请尝试上面给出的代码。我认为这将解决您的问题:) – Basil 2011-06-18 04:57:23