I have created an array list which gets data from the given URL
,now I want this ArrayList
to be displayed in GridView
.I passed it in the ImageAdapter
but the ArrayList
is showing null.The API
Response List
is getting the data as On item Click it shows the data.
public ArrayList<String> images, urlResponse;
FloatingActionButton fab;
public String Data_Url = "http://gurbaaj.16mb.com/photos.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gallery = (GridView) findViewById(R.id.gridview);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "FAB clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, ImageCheckActivity.class);
intent.putStringArrayListExtra("stock_list", urlResponse);
startActivity(intent);
}
});
//Action On item click
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
if (null != images && !images.isEmpty())
Toast.makeText(
getApplicationContext(),
"position " + position + " " + urlResponse, Toast.LENGTH_SHORT).show();
}
});
gallery.setAdapter(new ImageAdapter(this, urlResponse));
}
@Override
protected void onStart() {
Toast.makeText(this, "On Start" , Toast.LENGTH_SHORT).show();
super.onStart();
final ProgressDialog loading = ProgressDialog.show(MainActivity.this,"Data is Loading", "Please wait...",false,false);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Data_Url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
loading.dismiss();
parseData(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(getApplicationContext(), "Network error", Toast.LENGTH_SHORT).show();
}
});
// Creating RequestQueue
CustomVolleyRequestQueue.getInstance(getApplicationContext()).addToRequestQueue(jsonArrayRequest);
}
private void parseData(JSONArray response) {
urlResponse = new ArrayList<>();
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject;
try {
jsonObject = response.getJSONObject(i);
String Images = jsonObject.getString("Image");
urlResponse.add(Images);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private class ImageAdapter extends BaseAdapter {
/**
* The context.
*/
private Activity context;
ArrayList<String> urlImages;
/**
* Instantiates a new image adapter.
*
* @param localContext the local context
*/
public ImageAdapter(Activity localContext, ArrayList<String> urlImages) {
context = localContext;
images = getAllShownImagesPath(context);
this.urlImages = urlImages;
}
public int getCount() {
return urlImages.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView
.setLayoutParams(new GridView.LayoutParams(270, 270));
} else {
picturesView = (ImageView) convertView;
}
try {
Glide.with(context).load(urlImages.get(position))
.placeholder(R.mipmap.ic_launcher_round).centerCrop()
.into(picturesView);
} catch (Exception e) {
e.printStackTrace();
}
return picturesView;
}
Add this line after parse your data:
gallery.setAdapter(new ImageAdapter(this, urlResponse));
like that:
private void parseData(JSONArray response) {
urlResponse = new ArrayList<>();
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject;
try {
jsonObject = response.getJSONObject(i);
String Images = jsonObject.getString("Image");
urlResponse.add(Images);
} catch (JSONException e) {
e.printStackTrace();
}
}
gallery.setAdapter(new ImageAdapter(this, urlResponse));
}