是Android新手。我试图通过数组适配器将我的res / Drawable文件夹中的一堆图像加载到Gridview中。不幸的是我的应用程序每次尝试使用gridView查看活动时都会崩溃。我想知道如何设置我在res文件夹中的图像的imageResource以显示在Gridview中。
这是我的代码:
Smile.class
public class Smiley extends Activity {
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.smile);
gridView = (GridView) findViewById(R.id.gridView1);
String planets[] = this.getResources().getStringArray(R.array.imageme);
ArrayAdapter<String>adapter = new ArrayAdapter<String> (this,R.layout.grid_view_row,R.id.imageGrid , planets);
gridView.setAdapter(adapter);
}
}
Smile.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blurred"
>
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
>
</GridView>
</RelativeLayout>
grid_view_row.xml(Gridview的自定义布局)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imageGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:src="@drawable/a"
/>
</FrameLayout>
My String Array(xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="imageme">
<item>@drawable/a</item>
<item>@drawable/b</item>
<item>@drawable/c</item>
<item>@drawable/d</item>
</string-array>
</resources>
logcat的
09-04 16:48:40.561: E/ArrayAdapter(25938): You must supply a resource ID for a TextView
09-04 16:48:40.568: E/AndroidRuntime(25938): FATAL EXCEPTION: main
09-04 16:48:40.568: E/AndroidRuntime(25938): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.widget.AbsListView.obtainView(AbsListView.java:2207)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.widget.GridView.onMeasure(GridView.java:1040)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.View.measure(View.java:15609)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:645)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:425)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.View.measure(View.java:15609)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.View.measure(View.java:15609)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.View.measure(View.java:15609)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-04 16:48:40.568: E/AndroidRuntime(25938): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2203)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.View.measure(View.java:15609)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.Choreographer.doCallbacks(Choreographer.java:579)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.Choreographer.doFrame(Choreographer.java:548)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.os.Handler.handleCallback(Handler.java:725)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.os.Handler.dispatchMessage(Handler.java:92)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.os.Looper.loop(Looper.java:153)
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.app.ActivityThread.main(ActivityThread.java:5297)
09-04 16:48:40.568: E/AndroidRuntime(25938): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:48:40.568: E/AndroidRuntime(25938): at java.lang.reflect.Method.invoke(Method.java:511)
09-04 16:48:40.568: E/AndroidRuntime(25938): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
09-04 16:48:40.568: E/AndroidRuntime(25938): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-04 16:48:40.568: E/AndroidRuntime(25938): at dalvik.system.NativeStart.main(Native Method)
09-04 16:48:40.568: E/AndroidRuntime(25938): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView
09-04 16:48:40.568: E/AndroidRuntime(25938): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:382)
09-04 16:48:40.568: E/AndroidRuntime(25938): ... 35 more
请帮忙。谢谢
答案 0 :(得分:2)
问题是您尝试将字符串类型数据分配给ImageView
。
解决问题的最简单方法是:
不使用简单的ArrayAdapter
字符串数据,而是使用自定义适配器,如:
class ImgAdapter extends BaseAdapter {
@Override
public int getCount() {
return planets.length;
}
@Override
public Integer getItem(final int position) {
return planets[position];
}
@Override
public long getItemId(final int position) {
return position;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_view_row, null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageGrid);
imageView.setImageResource(getItem(position));
return view;
}
}
这很简单。最后一件事是将新适配器设置为gridView
答案 1 :(得分:1)
您的问题的具体答案是您的适配器的构造函数是:
(this,R.layout.grid_view_row,R.id.imageGrid , planets);
R.layout.grid_view_row
必须是只有TextView
的xml文件。它不能包含在LinearLayout
或RelativeLayout
之类的任何其他内容中。所以你需要:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
//attributes
/>
但是,如果要将图像加载到GridView
,我建议使用扩展BaseAdapter
的“图像适配器”,然后从那里获取图像。与此示例非常相似:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to your images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
其中getView
设置网格的外观,mThumbIds
是您想要的每张图片的Array
。从Activity
开始,只需添加代码即可显示GridView
:
gridview.setAdapter(new ImageAdapter(this));