Sajad Asadi Sajad Asadi - 1 month ago 5
Android Question

Custom Adapter for fragment context cant resolve activity

i have a custom Gridview and used the custom adapter for this but afterwards i converted the list view activity to fragment and now i have incompatible types problem in custom adapter
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));
}
}


this is Custom adapter

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

}


im getting incompatible error in this :
context=mainActivity;

what can i do in this case?

Answer

Your problem is when you instantiate your CustomAdapter -

new CustomAdapter(this, prgmNameList, prgmImages));

this is referencing the Fragment instance, and that is what you are passing in, which is obviously not and instance of an Actvitiy.

I would suggest you construct and instantiate your Custom Adapter like this:

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

or/and change the signature to be :

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