Android Hello GridView Tutorial Will Not Display Images

前端 未结 4 1100
心在旅途
心在旅途 2021-02-13 12:25

First time poster!

I am new to Android development and have been following Google\'s HelloView tutorials without problems... until the HelloGridView tutorial. For some r

相关标签:
4条回答
  • 2021-02-13 12:53

    replace your adapter class by given code

    package com.marnbeast.android; 
    
    import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
    
    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 mThumbIds[position];
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        // 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 our 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
        };
    }
    
    0 讨论(0)
  • 2021-02-13 13:02

    we can use this code,

    public class MyGridView extends Activity { 
    
    @Override 
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.animalsgridview);
    GridView gridview = (GridView) findViewById(R.id.gridviewid);
    gridview.setAdapter(new ImageAdapter(this));
    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            int imageId = ((ImageAdapter)parent.getAdapter()).mygetItemId(position);
    
            Intent fullScreenIntent = new Intent(v.getContext(),Full.class);
            fullScreenIntent.putExtra(Full.class.getName(),imageId);
            AnimalsGridView.this.startActivity(fullScreenIntent);         
            }
        });
    }
    private class ImageAdapter extends BaseAdapter {
        private Context mContext;
        public ImageAdapter(Context c) {
        mContext = c;
        }
        public int getCount() {
            return imagearray.length;
        }
        public Object getItem(int position) {
            return null;
        }
        public long getItemId(int position) {
            return imagearray[position];
        }
    
        // 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(150, 150));
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setPadding(1, 1, 1, 1);
    
            } else {
                imageView = (ImageView) convertView;
            }
    
            imageView.setImageResource(imagearray[position]);
            return imageView;
        }
        // references to our images                    
        private Integer[] imagearray = {                 
    
            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
    
            };
        public int mygetItemId(int position) {
                    return imagearray[position];
        }                                
      }
    }   
    

    this is the xml code

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_weight="1"
      android:layout_height="fill_parent">
    <GridView
        android:id="@+id/gridviewid"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:columnWidth="60dp"
        android:stretchMode="columnWidth"
        android:padding="5dp"
        android:verticalSpacing="5dp"
        android:horizontalSpacing="20dp"        
        android:gravity="center_horizontal"
        android:background="#ffffff"/>    
    

    0 讨论(0)
  • 2021-02-13 13:06

    Usually, in these cases, a clean and rebuild of the project will resolve the problem. It is possible that replacing the images simply forced your R file to refresh.

    If not, my only other thought is that android only supports baseline and progressive jpegs. If your jpeg had some sort of weird optimisation on it, that could have made for an invalid resource.

    0 讨论(0)
  • 2021-02-13 13:09

    Try making a Clean/rebuild of your project please

    0 讨论(0)
提交回复
热议问题