ImageAdapter不能应用于Fragment类 [英] ImageAdapter cannot be applied to a Fragment Class

查看:79
本文介绍了ImageAdapter不能应用于Fragment类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循ImageView和GalleryView教程,并在fragment类中添加galleryview,但是存在一个错误,即ImageAdapter无法应用于Tab1Fragment类,并且我不知道如何解决它.

I am trying to follow ImageView and GalleryView tutorial and add a galleryview in a fragment class however there is an error that the ImageAdapter cannot be applied to the Tab1Fragment class, and I cannot figure out how to solve it.

片段类

package eu.lucazanini;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import eu.lucazanini.DialogHelper;


public class Tab2Fragment extends Fragment{


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

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

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

ImageAdapter类-此类中仅在Fragment类中未显示错误

ImageAdapter Class - No errors shown within this class, only within the Fragment Class

package eu.lucazanini;

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

推荐答案

首先,您应该移动返回行 return inflater.inflate(R.layout.tab2,container,false); 到onCreateView的末尾,因为所有其他行都将无法访问.

First of all, you should move the return line return inflater.inflate(R.layout.tab2, container, false); to end of your onCreateView, because all other lines will be unreachable.

此外,您还应通过以下方式更正onCreateView:

View view = inflater.inflate(R.layout.tab2, container, false);

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

    .
    .
    .

    return view ;

您应该向其传递上下文.您可以使用 getActivity().另外,您缺少右括号.因此,将错误的行替换为

You should pass a context to it. You can use the getActivity(). Also you are missing a closing parenthesis. So, replace the wrong line with

new ImageAdapter(getActivity()) ;

这篇关于ImageAdapter不能应用于Fragment类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆