1

这是有问题的代码:

public class Level1 extends Activity {

int[] logos = {
        R.drawable.arutz8,
        R.drawable.channel1,
        R.drawable.doctor_gav,
        R.drawable.foxgroup3,
        R.drawable.careline,
        R.drawable.golfnew,
        R.drawable.haaretz,
        R.drawable.hafenix,
        /*R.drawable.hando,
        R.drawable.bankleumi,
        R.drawable.jerusalempostred,
        R.drawable.laisha,
        R.drawable.logo,
        R.drawable.logodelta,
        R.drawable.maariv,
        R.drawable.pelephone,
        R.drawable.ravbariah,
        R.drawable.renuar,
        R.drawable.reshet_tv,
        R.drawable.sano,
        R.drawable.shilav,
        R.drawable.sport5,
        R.drawable.srigamish,
        R.drawable.steimatzky,
        R.drawable.superpharm,
        R.drawable.supersal,
        R.drawable.tambur,
        R.drawable.tzometsfarim,
        R.drawable.walla,
        R.drawable.yediot,*/
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.level1);
    ListAdapter adapter = (new ArrayAdapter<Integer>(this, R.layout.level1));

    GridView grid = (GridView) findViewById(R.id.gridview1);
    grid.setAdapter(new ImageAdapter(this));
}


private class ImageAdapter extends BaseAdapter 
{
    private Context context;

    public ImageAdapter(Context c) 
    {
        context = c;
    }

    //---returns the number of images---
    public int getCount() {
        return logos.length;
    }

    //---returns the ID of an item--- 
    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(90, 90));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setPadding(5, 5, 5, 5);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(logos[position]);
        return imageView;
    }
} 

}

只要所有图像的大小约为每张图像 60 像素,该程序就可以完美运行。这种尺寸的图像的问题是每个图像都被压缩并且它的丑陋!我尝试在更大的图像中使用,由于某种原因,只有前五个图像出现在屏幕上,当我尝试加载其余图像时,应用程序崩溃。我认为这是因为图像的大小而发生的,但后来我意识到所有图像的大小都相同,其中仍有 5 个被绘制在屏幕上。有任何想法吗?

这些是来自 logCat 的日志:

日志

这是我按下 1 级按钮时的结果:

结果

4

1 回答 1

0

ImageAdapter 中的公共 View getView(...) 方法存在问题。当 convertView == null 时,您永远不会将 convertView 链接到您的 imageView。所以else语句会有问题。您可以通过两种方式做到这一点:

  • 通过像您一样在代码中定义您的 ImageView,在您的情况下,这将是最佳选择:

    ImageView imageView = (ImageView) convertView;

    if (convertView == null) { convertView = new ImageView(context); imageView = (ImageView) convertView; // Set other parameters }

    // Set resource

    return convertView;

  • By defining your layout in a xml-layout file an using a layout inflater:

Handler handler;

 if (convertView == null) {
        LayoutInflater li = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(yourLayoutResourceId, parent, false);
        handler = new Handler();
        handler.itemIV = (ImageView) convertView.findViewById(imageViewLayoutId);

        convertView.setTag(handler);
    } else {
        handler = (Handler) convertView.getTag();
    }

     handler.imageView.setImageResource(...);
     return convertView;

Where yourLayoutResourceId is the id of the created xml-layout file (R.layout.exmaple), and imageViewLayoutId is the id of your imaeView in the layout (R.id.exmapleIV). As last step define an inner class Handler in your ImageAdapter:

class Handler { ImageView imageView; }

祝你好运!有好的发展。

于 2012-06-12T08:26:55.773 回答