java-阵列适配器从Res文件夹加载图像(Android App)
作者:互联网
是Android新手.我试图通过数组适配器将res / Drawable文件夹中的一堆图像加载到Gridview中.不幸的是,每次我尝试使用gridView查看活动时,我的应用程序都会崩溃.我想知道如何设置res文件夹中要显示在Gridview中的图像的imageResource.
这是我的代码:
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>
我的字符串数组(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
请帮助.谢谢
解决方法:
您问题的具体答案是适配器的构造函数是:
(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是所需的每张图片的数组.在此处要显示GridView的Activity中,只需添加代码即可:
gridview.setAdapter(new ImageAdapter(this));
标签:android-gridview,android,java,android-imageview,android-arrayadapter 来源: https://codeday.me/bug/20191013/1906864.html