Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. public class AlbumsActivity extends ListActivity {
  2. // Connection detector
  3. ConnectionDetector cd;
  4.  
  5. // Alert dialog manager
  6. AlertDialogManager alert = new AlertDialogManager();
  7.  
  8. // Progress Dialog
  9. private ProgressDialog pDialog;
  10.  
  11. // Creating JSON Parser object
  12. JSONParser jsonParser = new JSONParser();
  13.  
  14. ArrayList<HashMap<String, String>> albumsList;
  15.  
  16. // albums JSONArray
  17. JSONArray albums = null;
  18.  
  19. // albums JSON url
  20. private static final String URL_ALBUMS = "http://api.androidhive.info/songs/albums.php";
  21.  
  22. // ALL JSON node names
  23. private static final String TAG_ID = "id";
  24. private static final String TAG_NAME = "name";
  25. private static final String TAG_SONGS_COUNT = "songs_count";
  26.  
  27. @Override
  28. public void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_albums);
  31.  
  32. cd = new ConnectionDetector(getApplicationContext());
  33.  
  34. // Check for internet connection
  35. if (!cd.isConnectingToInternet()) {
  36. // Internet Connection is not present
  37. alert.showAlertDialog(AlbumsActivity.this, "Internet Connection Error",
  38. "Please connect to working Internet connection", false);
  39. // stop executing code by return
  40. return;
  41. }
  42.  
  43. // Hashmap for ListView
  44. albumsList = new ArrayList<HashMap<String, String>>();
  45.  
  46. // Loading Albums JSON in Background Thread
  47. new LoadAlbums().execute();
  48.  
  49. // get listview
  50. ListView lv = getListView();
  51.  
  52. /**
  53. * Listview item click listener
  54. * TrackListActivity will be lauched by passing album id
  55. * */
  56. lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
  57. @Override
  58. public void onItemClick(AdapterView<?> arg0, View view, int arg2,
  59. long arg3) {
  60. // on selecting a single album
  61. // TrackListActivity will be launched to show tracks inside the album
  62. Intent i = new Intent(getApplicationContext(), TrackListActivity.class);
  63.  
  64. // send album id to tracklist activity to get list of songs under that album
  65. String album_id = ((TextView) view.findViewById(R.id.album_id)).getText().toString();
  66. i.putExtra("album_id", album_id);
  67.  
  68. startActivity(i);
  69. }
  70. });
  71. }
  72.  
  73. /**
  74. * Background Async Task to Load all Albums by making http request
  75. * */
  76. class LoadAlbums extends AsyncTask<String, String, String> {
  77.  
  78. /**
  79. * Before starting background thread Show Progress Dialog
  80. * */
  81. @Override
  82. protected void onPreExecute() {
  83. super.onPreExecute();
  84. pDialog = new ProgressDialog(AlbumsActivity.this);
  85. pDialog.setMessage("Listing Albums ...");
  86. pDialog.setIndeterminate(false);
  87. pDialog.setCancelable(false);
  88. pDialog.show();
  89. }
  90.  
  91. /**
  92. * getting Albums JSON
  93. * */
  94. protected String doInBackground(String... args) {
  95. // Building Parameters
  96. List<NameValuePair> params = new ArrayList<NameValuePair>();
  97.  
  98. // getting JSON string from URL
  99. String json = jsonParser.makeHttpRequest(URL_ALBUMS, "GET",
  100. params);
  101.  
  102. // Check your log cat for JSON reponse
  103. Log.d("Albums JSON: ", "> " + json);
  104.  
  105. try {
  106. albums = new JSONArray(json);
  107.  
  108. if (albums != null) {
  109. // looping through All albums
  110. for (int i = 0; i < albums.length(); i++) {
  111. JSONObject c = albums.getJSONObject(i);
  112.  
  113. // Storing each json item values in variable
  114. String id = c.getString(TAG_ID);
  115. String name = c.getString(TAG_NAME);
  116. String songs_count = c.getString(TAG_SONGS_COUNT);
  117.  
  118. // creating new HashMap
  119. HashMap<String, String> map = new HashMap<String, String>();
  120.  
  121. // adding each child node to HashMap key => value
  122. map.put(TAG_ID, id);
  123. map.put(TAG_NAME, name);
  124. map.put(TAG_SONGS_COUNT, songs_count);
  125.  
  126. // adding HashList to ArrayList
  127. albumsList.add(map);
  128. }
  129. }else{
  130. Log.d("Albums: ", "null");
  131. }
  132.  
  133. } catch (JSONException e) {
  134. e.printStackTrace();
  135. }
  136.  
  137. return null;
  138. }
  139.  
  140. /**
  141. * After completing background task Dismiss the progress dialog
  142. * **/
  143. protected void onPostExecute(String file_url) {
  144. // dismiss the dialog after getting all albums
  145. pDialog.dismiss();
  146. // updating UI from Background Thread
  147. runOnUiThread(new Runnable() {
  148. public void run() {
  149. /**
  150. * Updating parsed JSON data into ListView
  151. * */
  152. ListAdapter adapter = new SimpleAdapter(
  153. AlbumsActivity.this, albumsList,
  154. R.layout.list_item_albums, new String[] { TAG_ID,
  155. TAG_NAME, TAG_SONGS_COUNT }, new int[] {
  156. R.id.album_id, R.id.album_name, R.id.songs_count });
  157.  
  158. // updating listview
  159. setListAdapter(adapter);
  160. }
  161. });
  162.  
  163. }
  164.  
  165. }
  166. }
  167.  
  168. <?xml version="1.0" encoding="utf-8"?>
  169. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  170. android:layout_width="fill_parent"
  171. android:layout_height="fill_parent"
  172. android:orientation="vertical"
  173. android:background="#ffffff">
  174.  
  175. <ListView
  176. android:id="@android:id/list"
  177. android:layout_width="fill_parent"
  178. android:layout_height="wrap_content"
  179. android:divider="#b5b5b5"
  180. android:dividerHeight="1dp"
  181. android:cacheColorHint="#00000000"/>
  182.  
  183. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement