GridView的教程问题教程、问题、GridView

由网友(浪够了我带你回家)分享简介:我试图进入Android的发展,我经历的教程,但是我真的很粘在的 HelloGridView 教程中,我通过苹果开发者包围,所以有没有人有任何的Andr​​oid体验问!I'm trying to get into android development and I'm going through the tutor...

我试图进入Android的发展,我经历的教程,但是我真的很粘在的 HelloGridView 教程中,我通过苹果开发者包围,所以有没有人有任何的Andr​​oid体验问!

I'm trying to get into android development and I'm going through the tutorials, however I'm really stuck on the HelloGridView tutorial and I'm surrounded by apple developers, so have nobody with any android experience to ask!

我一直试图做的HelloGridView教程中,我得到17错误,但无法看到我在哪里的问题呢?!?我有一种感觉,我缺少一个库还是什么?!?

I've attempted to do the HelloGridView tutorial and I get 17 Errors but cant see where am I going wrong?!? I have a feeling I'm missing a library or something?!?

由于我有这么多的错误,我并不能解决我采取的截图: https://p.xsw88.cn/allimgs/daicuo/20230905/990.png.jpeg

As I've got so many errors I cant solve I have taken a screenshot: https://p.xsw88.cn/allimgs/daicuo/20230905/991.png.jpeg

问题出在HelloGridView和ImageAdapter类:

The problems lie in the HelloGridView and ImageAdapter classes:

HelloGridView类:

HelloGridView class:

    package com.example.HelloGridView;

    import android.app.Activity;
    import android.os.Bundle;

    public class HelloGridView extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            GridView gridview = (GridView) findViewById(R.id.gridview);
            gridview.setAdapter(new ImageAdapter(this));

            gridview.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                    Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
                }
            });
        }

ImageAdapter类:

ImageAdapter class:

    package com.example.HelloGridView;

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

如果有人能告诉我,我要去哪里错了,或者至少指明了正确的方向,这将是非常美联社preciated!

If anyone could tell me where I'm going wrong or at least point me in the right direction, it would be much appreciated!

推荐答案

下面是固定设置,下次你需要做的进口为每个对象:

Here is the fixed setup, next time you need to do the imports for each object:

您HelloGridView

Your HelloGridView

package com.example.HelloGridView;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class HelloGridView extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(getApplicationContext()));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                Toast.makeText(HelloGridView.this, "" + position,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

您底座适配器

package com.example.HelloGridView;

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 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 };
}
阅读全文

相关推荐

最新文章