简体   繁体   English

网格视图/适配器问题,将图像添加到网格视图

[英]gridview/adapter issue, adding images to gridview

first do I have to make my own adapter class to set images in a gridview?首先我必须制作我自己的适配器类来在 gridview 中设置图像吗? its seams ineligent, requires I make a new class each time I make a gridview.它的接缝不灵,要求我每次制作 gridview 时都创建一个新类。

the problem I have having is that I can't make my image appear in my gridview我遇到的问题是我无法让我的图像出现在我的网格视图中

package joshpike.hsh.hsh_game;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;


public class DisplayActivity extends MainActivity
{
    public class ImageAdapter extends BaseAdapter 
    {
        private Context mContext;

        // 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(imageArray[position]);
            return imageView;
        }


        private Integer[] imageArray = 
        {
            //currentImageView(0, "xh","bottom",0,0), currentImageView(0, "xh","bottom",1,0)    
            R.drawable.level_1_bottom, R.drawable.level_1_top
        };


        @Override
        public int getCount() 
        {
            return 0;
        }

        @Override
        public Object getItem(int position) 
        {
            return null;
        }

        @Override
        public long getItemId(int position) 
        {
            return 0;
        }
    }


    // Inflate the menu; this adds items to the action bar if it is present.
    //makes the options menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.display, menu);
        return true;
    }

    //what happens if you select items from the options menu
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId() )
        {
            case R.id.miniMap:

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    //called when activity is started for first time either for first time or after destroyed
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display);

        GridView bottomMapGrid = (GridView)findViewById(R.id.bottomMapGrid);

        bottomMapGrid.setAdapter(new ImageAdapter());

        System.out.println("DisplayActivity onCreate done");        
    }

currently you are passing Null mContext to ImageView Constructor for Creating it.目前您正在将 Null mContext传递给ImageView构造函数以创建它。 you need to initialize mContext before using it as :在将mContext用作以下mContext之前,您需要对其进行初始化:

public class ImageAdapter extends BaseAdapter 
    {
        private Context mContext;

        public ImageAdapter(Context mContext){
           this.mContext=mContext;  //<<< initialize here
        }
         //....your code here
    }

and from Activity Create ImageAdapter instance by passing Current Activity Context as :并从 Activity Create ImageAdapter实例通过将 Current Activity Context 传递为:

bottomMapGrid.setAdapter(new ImageAdapter(DisplayActivity.this));
 @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display);
        GridView bottomMapGrid = (GridView)findViewById(R.id.bottomMapGrid);
        bottomMapGrid.setAdapter(new ImageAdapter(this));
        System.out.println("DisplayActivity onCreate done");        
    }

And Adapter class Use like this和 Adapter 类像这样使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM