片段上下文的自定义适配器无法解析活动

时间:2016-10-31 17:39:42

标签: android gridview fragment custom-adapter

我有一个自定义Gridview并使用自定义适配器,但后来我将列表视图活动转换为片段,现在我在自定义适配器中有不兼容的类型问题 Gridview:

package com.appp.web.a95;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import java.util.ArrayList;
public class Main_grid extends Fragment {
    GridView gv;
    Context context;
    ArrayList prgmName;
    public static String [] prgmNameList={   //some items};
    public static int [] prgmImages={//some items};
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        return inflater.inflate(R.layout.main_grid2, container, false);







    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        gv = (GridView) getView().findViewById(R.id.gridView1);
        gv.setAdapter(new CustomAdapter(this, prgmNameList, prgmImages));
    }
}

这是自定义适配器

package com.appp.web.a95;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter{

    String [] result;
    Context context;
    int [] imageId;
    private static LayoutInflater inflater=null;
    public CustomAdapter(Main_grid mainActivity, String[] prgmNameList, int[] prgmImages) {
        // TODO Auto-generated constructor stub
        result=prgmNameList;
        //here is incompatible types error
        context=mainActivity;
        imageId=prgmImages;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class Holder
    {
        TextView tv;
        ImageView img;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        View rowView;

        if ((position % 2) == 0 ){
            rowView = inflater.inflate(R.layout.grid_list4, null);}
        else {rowView = inflater.inflate(R.layout.grid_list5, null);}
        holder.tv=(TextView) rowView.findViewById(R.id.textView1);
        holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

        holder.tv.setText(result[position]);
        holder.img.setImageResource(imageId[position]);

        rowView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // custom dialog
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.customdialog);
                dialog.setTitle(result[position]);

                // set the custom dialog components - text, image and button
                TextView text = (TextView) dialog.findViewById(R.id.text);
                ImageView image = (ImageView) dialog.findViewById(R.id.image);
                image.setImageResource(imageId[position]);

                Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                // if button is clicked, close the custom dialog
                dialogButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
            }
        });

        return rowView;
    }

}

我在这里遇到了不兼容的错误:         上下文= mainActivity;

在这种情况下我该怎么办?

2 个答案:

答案 0 :(得分:2)

您的问题是当您实例化CustomAdapter时 -

new CustomAdapter(this, prgmNameList, prgmImages));

this正在引用Fragment实例,这就是你传入的内容,这显然不是Actvitiy的实例。

我建议您构建并实例化自定义适配器,如下所示:

new CustomAdapter(getActivity(), prgmNameList, prgmImages)); 

或/并将签名更改为:

public CustomAdapter(Context context, String[] prgmNameList, int[] prgmImages)

答案 1 :(得分:1)

不要在Fragment.Use this上使用getActivity(),因为您的Main_grid类是一个片段,它使用getActivity()从活动类呈现活动 因此,请按照以下代码

替换您的onViewCreated()方法
@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        gv = (GridView) getView().findViewById(R.id.gridView1);
        gv.setAdapter(new CustomAdapter(getActivity(), prgmNameList, prgmImages));
    }