Array Adapter Load Images from Res Folder (Android

2019-03-02 16:32发布

问题:

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

回答1:

The specific answer to your issue is your constructor for your adapter is:

(this,R.layout.grid_view_row,R.id.imageGrid , planets);

R.layout.grid_view_row needs to be an xml file with only a TextView. It can't be wrapped in anything else like a LinearLayout or RelativeLayout. So you would need:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    //attributes
/>

However, If you want to load images into a GridView I would recommend using an "image adapter" that extends BaseAdapter and then get the images from there. Very similar to this example:

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
};

}

where getView sets how the grid should look and mThumbIds is an Array of every picture that you want. From here in the Activity that you want to display the GridView in just add the code:

gridview.setAdapter(new ImageAdapter(this));


回答2:

Problem is that you try to assign String-type data to ImageView. The easiest way to fix it would be:

  1. Change resource array type to integer-array
  2. Instead of using simple ArrayAdapter with string data, use custom adapter like:

    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;
    }
    }
    

That is simple it. Last thing is to set new adapter to gridView