如何在另一个类的GridView中解析ImageAdapter?

时间:2020-11-24 21:23:06

I have a little problem, which I'm sure is easily resolvable, but I'm struggling. I need to parse resources from ImageAdapter class to my Gallery class. ImageAdapter:

我有一点问题,我肯定很容易解决,但我很挣扎。我需要将ImageAdapter类中的资源解析为Gallery类。 ImageAdapter:

package rs.srecnaplaneta.www.srecnaplaneta;
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.slika21,
            R.drawable.slika22,
            R.drawable.slika23,
            R.drawable.slika24,
            R.drawable.slika25,
            R.drawable.slika26,
            R.drawable.slika27,
            R.drawable.slika28,
            R.drawable.slika29
    };
}

Gallery:

画廊:

package rs.srecnaplaneta.www.srecnaplaneta;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

public class klasaGalerija extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab3galerija, container, false);
    }

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            }
        });
    }

}

So, the problem is that, in Gallery class, in code line:

所以,问题是,在Gallery类中,在代码行中:

gridView.setAdapter(new ImageAdapter(this));

gridView.setAdapter(new ImageAdapter(this));

"this" doesn't work. Auto solutions from Android Studio were to change 1st parameter of method "ImageAdapter" from "Context" to "klasaGalerija", but I must not do that. It must stay as "Context" I assume in order to work, but I cannot point to it.

“这个”不起作用。 Android Studio的自动解决方案是将方法“ImageAdapter”的第一个参数从“Context”更改为“klasaGalerija”,但我不能这样做。它必须保持为“上下文”我假设为了工作,但我不能指出它。

If anyone can help, I would really appreciate it.

如果有人可以提供帮助,我会非常感激。

1 个解决方案

#1


0  

Since it's a fragment you'd have to use getContext() instead of this, I think.

因为它是一个片段,你必须使用getContext()而不是这个,我想。

#1


0  

Since it's a fragment you'd have to use getContext() instead of this, I think.

因为它是一个片段,你必须使用getContext()而不是这个,我想。