无法从GridView显示图像

时间:2016-04-26 03:00:52

标签: android gridview

我试图实现一个显示与此类似的几个图像的GridView:http://developer.android.com/guide/topics/ui/layout/gridview.html

除了上面的链接,我还会关注这个简短的教程:http://www.101apps.co.za/index.php/articles/gridview-tutorial-using-the-picasso-library.html

我能够在主要活动中的GridView中显示图像,但是,我的麻烦在于我的第二个活动,我试图将图像从GridView显示到ImageView上第二项活动。

更具体地说,麻烦在于这行代码(使用mThumbIds):

Picasso.with(MovieDetails.this).load(MainActivity.mThumbIds[position]).into(imageView);

我尝试过几种不同的尝试,但无法弄清楚如何纠正" 'cannot resolve symbol 'mThumbIds'"错误。任何帮助将非常感激。

MainActivity -

public class MainActivity extends AppCompatActivity {

    public ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, MovieDetails.class);
                intent.putExtra("Position", position);
                startActivity(intent);
            }
        });
    }

    public class ImageAdapter extends BaseAdapter {

        public Context mContext;

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

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return mThumbIds[position];
        }

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

        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {

                imageView = new ImageView(mContext);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(0, 0, 0, 0);
            } else {

                imageView = (ImageView) convertView;
            }

            Picasso.with(MainActivity.this).load(mThumbIds[position]).noFade().into(imageView);
            return imageView;
        }

        // references to our images
        public Integer[] mThumbIds = {
                .... ref to my images 
        };

    }

}

第二项活动 -

public class MovieDetails extends MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_details);

        int position = getIntent().getIntExtra("position", -1);

        if (position != -1) {
            Picasso.with(MovieDetails.this).load(MainActivity.mThumbIds[position]).into(imageView);
        } else {
            Picasso.with(MovieDetails.this).load(R.drawable.sample_7).into(imageView);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我们只能访问其他类的静态变量,而你的变量mThumbIds是非静态变量。

在Adapter类中声明变量并尝试从MainActivity.java

进行访问还有一件事

尝试将变量移动到MainActivity.java并将变量设置为静态并从其他类访问

public class MainActivity extends AppCompatActivity {

    public ImageView imageView;

    // references to our images
    public static Integer[] mThumbIds = {};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, MovieDetails.class);
                intent.putExtra("Position", position);
                startActivity(intent);
            }
        });
    }

    public class ImageAdapter extends BaseAdapter {

        public Context mContext;

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

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return mThumbIds[position];
        }

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

        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {

                imageView = new ImageView(mContext);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(0, 0, 0, 0);
            } else {

                imageView = (ImageView) convertView;
            }

            Picasso.with(MainActivity.this).load(mThumbIds[position]).noFade().into(imageView);
            return imageView;
        }


    }

}

答案 1 :(得分:1)

嗯,基本的Java在这里

你宣布:

public Integer[] mThumbIds = { .... ref to my images };

这是MainActivity

中的实例变量

在SecondActivity中将此变量作为static变量访问。

应该声明mThumbIds是静态的(即使我不建议将此作为Android开发的最佳做法)。