Android Hello GridView Tutorial Will Not Display Images

前端 未结 4 1096
心在旅途
心在旅途 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 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

    
    
        
    

提交回复
热议问题