错误:(117,63)错误:不兼容的类型:TabThreeFragment无法转换为Context
错误显示在“新的GridViewAdapter(this,images,names); ”
public class TabThreeFragment扩展了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()); //将我们的请求添加到队列中 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);
}
}
我的适配器是
公共类GridViewAdapter扩展了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;
}
}
答案 0 :(得分:0)
在TabThreeFragment中,替换
GridViewAdapter gridViewAdapter = new GridViewAdapter(this,images,names);
带
GridViewAdapter gridViewAdapter = new GridViewAdapter(getContext(),images,names);
碎片不会从Context扩展。