Am an Android newbiew. Am trying to load a bunch of images in my res/Drawable folder into a Gridview via an array adapter. unfortunately my the app crashes each time i try to view the activity with the gridView. i would like to know how to set the imageResource of image i have in the res folder to display in the Gridview.
here is my Code:
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 (Custom layout for the 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
please help.Thanks