How to populate GridView if Internet not available but images already cached to SD Card
- by Sophie
Hello I am writing an Application in which i am parsing JSON Images and then caching into SD Card.
What I want to do ?
I want to load images into GridView from JSON (by caching images into SD Card), and wanna populate GridView (no matter Internet available or not) once images already downloaded into SD Card.
What I am getting ?
I am able to cache images into SD Card, also to populate GridView, but not able to show images into GridView (if Internet not available) but images cached into SD Card
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myGridView = inflater.inflate(R.layout.fragment_church_grid, container, false);
if (isNetworkAvailable()) {
new DownloadJSON().execute();
} else {
Toast.makeText(getActivity(), "Internet not available !", Toast.LENGTH_LONG).show();
}
return myGridView ;
}
private boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
return (info != null);
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(getActivity());
// Set progressdialog title
mProgressDialog.setTitle("Church Application");
// Set progressdialog message
mProgressDialog.setMessage("Loading Images...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://snapoodle.com/APIS/android/feed.php");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("print");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("saved_location", jsonobject.getString("saved_location"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (GridView) shriRamView.findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ChurchImagesAdapter(getActivity(), arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}