0
votes

Error:(117, 63) error: incompatible types: TabThreeFragment cannot be converted to Context

error is showing in "new GridViewAdapter(this,images,names);"

public class TabThreeFragment extends Fragment {

//url for grid images
public static final String DATA_URL = "https://gist.githubusercontent.com/theBoyMo/40b97e688f90a68bfc02/raw/c8463217c22e597c316edb059db410fa38ec26dc/gallery.json";

//Tag values to read from json
public static final String TAG_IMAGE_URL = "image";
public static final String TAG_NAME = "caption";

//GridView Object
private GridView gridView;

//ArrayList for Storing image urls and titles
private ArrayList<String> images;
private ArrayList<String> names;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab_three_fragment, container, false);

    // Inflate the layout for this fragment
    gridView = (GridView) rootView.findViewById(R.id.gridView);

    images = new ArrayList<>();
    names = new ArrayList<>();

    getData();
   return rootView;

// return inflater.inflate(R.layout.tab_three_fragment, container, false); }

private void getData(){
    //Showing a progress dialog while our app fetches the data from url
   // final ProgressDialog loading = ProgressDialog.show(this, "Please wait...","Fetching data...",false,false);

    //Creating a json array request to get the json from our api
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    //Dismissing the progressdialog on response
                  //  loading.dismiss();

                    //Displaying our grid
                    showGrid(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }
    );

    //Creating a request queue

// RequestQueue requestQueue = Volley.newRequestQueue(this); RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext()); //Adding our request to the queue requestQueue.add(jsonArrayRequest); }

private void showGrid(JSONArray jsonArray){
    //Looping through all the elements of json array
    for(int i = 0; i<jsonArray.length(); i++){
        //Creating a json object of the current index
        JSONObject obj = null;
        try {
            //getting json object from current index
            obj = jsonArray.getJSONObject(i);

            //getting image url and title from json object
            images.add(obj.getString(TAG_IMAGE_URL));
            names.add(obj.getString(TAG_NAME));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    //Creating GridViewAdapter Object
    GridViewAdapter gridViewAdapter = new GridViewAdapter(this,images,names);

    //Adding adapter to gridview
    gridView.setAdapter(gridViewAdapter);
}

}

and my adapter is

public class GridViewAdapter extends BaseAdapter {

//Imageloader to load images
private ImageLoader imageLoader;

//Context
private Context context;

//Array List that would contain the urls and the titles for the images
private ArrayList<String> images;
private ArrayList<String> names;

public GridViewAdapter (Context context, ArrayList<String> images, ArrayList<String> names){
    //Getting all the values
    this.context = context;
    this.images = images;
    this.names = names;
}

@Override
public int getCount() {
    return images.size();
}

@Override
public Object getItem(int position) {
    return images.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //Creating a linear layout
    LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    //NetworkImageView
    NetworkImageView networkImageView = new NetworkImageView(context);

    //Initializing ImageLoader
    imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
   // imageLoader.get(images.get(position), ImageLoader.getImageListener(networkImageView, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

    //Setting the image url to load
    networkImageView.setImageUrl(images.get(position),imageLoader);

    //Creating a textview to show the title
    TextView textView = new TextView(context);
    textView.setText(names.get(position));

    //Scaling the imageview
    networkImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    //networkImageView.setLayoutParams(new GridView.LayoutParams(200,200));

    //Adding views to the layout
    //linearLayout.addView(textView);
    linearLayout.addView(networkImageView);

    //Returnint the layout
    return linearLayout;
}

}

1
paste your code in GridViewAdapter class. Replace new GridViewAdapter(this,images, names) to new GridViewAdapter(getActivity, images, names)Dharma Sai Seerapu

1 Answers

0
votes

In TabThreeFragment, replace

GridViewAdapter gridViewAdapter = new GridViewAdapter(this,images,names);

with

GridViewAdapter gridViewAdapter = new GridViewAdapter(getContext(),images,names);

Fragments don't extend from Context.