Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // AlbumsActivity.java
- package com.example.jsonapp;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import org.apache.http.NameValuePair;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.app.ListActivity;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.TextView;
- import com.example.helper.AlertDialogManager;
- import com.example.helper.ConnectionDetector;
- import com.example.helper.JSONParser;
- public class AlbumsActivity extends ListActivity {
- // Connection detector
- ConnectionDetector cd;
- // Alert dialog manager
- AlertDialogManager alert = new AlertDialogManager();
- // Progress Dialog
- private ProgressDialog pDialog;
- // Creating JSON Parser object
- JSONParser jsonParser = new JSONParser();
- ArrayList<HashMap<String, String>> albumsList;
- // albums JSONArray
- JSONArray albums = null;
- // albums JSON url
- private static final String URL_ALBUMS = "http://api.androidhive.info/songs/albums.php";
- // ALL JSON node names
- private static final String TAG_ID = "id";
- private static final String TAG_NAME = "name";
- private static final String TAG_SONGS_COUNT = "songs_count";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_albums);
- cd = new ConnectionDetector(getApplicationContext());
- // Check for internet connection
- if (!cd.isConnectingToInternet()) {
- // Internet Connection is not present
- alert.showAlertDialog(AlbumsActivity.this, "Internet Connection Error",
- "Please connect to working Internet connection", false);
- // stop executing code by return
- return;
- }
- // Hashmap for ListView
- albumsList = new ArrayList<HashMap<String, String>>();
- // Loading Albums JSON in Background Thread
- new LoadAlbums().execute();
- // get listview
- ListView lv = getListView();
- /**
- * Listview item click listener
- * TrackListActivity will be lauched by passing album id
- * */
- lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> arg0, View view, int arg2,
- long arg3) {
- // on selecting a single album
- // TrackListActivity will be launched to show tracks inside the album
- Intent i = new Intent(getApplicationContext(), TrackListActivity.class);
- // send album id to tracklist activity to get list of songs under that album
- String album_id = ((TextView) view.findViewById(R.id.album_id)).getText().toString();
- i.putExtra("album_id", album_id);
- startActivity(i);
- }
- });
- }
- /**
- * Background Async Task to Load all Albums by making http request
- * */
- class LoadAlbums extends AsyncTask<String, String, String> {
- /**
- * Before starting background thread Show Progress Dialog
- * */
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- pDialog = new ProgressDialog(AlbumsActivity.this);
- pDialog.setMessage("Listing Albums ...");
- pDialog.setIndeterminate(false);
- pDialog.setCancelable(false);
- pDialog.show();
- }
- /**
- * getting Albums JSON
- * */
- protected String doInBackground(String... args) {
- // Building Parameters
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- // getting JSON string from URL
- String json = jsonParser.makeHttpRequest(URL_ALBUMS, "GET",
- params);
- // Check your log cat for JSON reponse
- Log.d("Albums JSON: ", "> " + json);
- try {
- albums = new JSONArray(json);
- if (albums != null) {
- // looping through All albums
- for (int i = 0; i < albums.length(); i++) {
- JSONObject c = albums.getJSONObject(i);
- // Storing each json item values in variable
- String id = c.getString(TAG_ID);
- String name = c.getString(TAG_NAME);
- String songs_count = c.getString(TAG_SONGS_COUNT);
- // creating new HashMap
- HashMap<String, String> map = new HashMap<String, String>();
- // adding each child node to HashMap key => value
- map.put(TAG_ID, id);
- map.put(TAG_NAME, name);
- map.put(TAG_SONGS_COUNT, songs_count);
- // adding HashList to ArrayList
- albumsList.add(map);
- }
- }else{
- Log.d("Albums: ", "null");
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * After completing background task Dismiss the progress dialog
- * **/
- protected void onPostExecute(String file_url) {
- // dismiss the dialog after getting all albums
- pDialog.dismiss();
- // updating UI from Background Thread
- runOnUiThread(new Runnable() {
- public void run() {
- /**
- * Updating parsed JSON data into ListView
- * */
- ListAdapter adapter = new SimpleAdapter(
- AlbumsActivity.this, albumsList,
- R.layout.list_item_albums, new String[] { TAG_ID,
- TAG_NAME, TAG_SONGS_COUNT }, new int[] {
- R.id.album_id, R.id.album_name, R.id.songs_count });
- // updating listview
- setListAdapter(adapter);
- }
- });
- }
- }
- }
- //activity_albums.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:background="#ffffff">
- <ListView
- android:id="@+id/list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:divider="#b5b5b5"
- android:dividerHeight="1dp"
- android:cacheColorHint="#00000000" >
- </ListView>
- </LinearLayout>
- //activity_tracks.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:background="#ffffff">
- <ListView
- android:id="@android:id/list"
- ></ListView>
- </LinearLayout>
- //list_item_albums.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/RelativeLayout1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/album_id"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:visibility="gone" />
- <TextView
- android:id="@+id/album_name"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="16dip"
- android:textColor="#000000"
- android:paddingTop="15dip"
- android:paddingBottom="15dip"
- android:paddingLeft="10dip"
- android:textStyle="bold"/>
- <TextView
- android:id="@+id/songs_count"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:textColor="#ffffff"
- android:textStyle="bold"
- android:background="#9ed321"
- android:paddingRight="3dip"
- android:paddingLeft="3dip" />
- </RelativeLayout>
- //log
- 12-29 11:32:13.611: W/Trace(1507): Unexpected value from nativeGetEnabledTags: 0
- 12-29 11:32:13.649: W/Trace(1507): Unexpected value from nativeGetEnabledTags: 0
- 12-29 11:32:14.309: D/dalvikvm(1507): GC_FOR_ALLOC freed 62K, 7% free 2571K/2752K, paused 47ms, total 50ms
- 12-29 11:32:14.319: I/dalvikvm-heap(1507): Grow heap (frag case) to 3.229MB for 635812-byte allocation
- 12-29 11:32:14.449: D/dalvikvm(1507): GC_FOR_ALLOC freed 1K, 6% free 3190K/3376K, paused 122ms, total 122ms
- 12-29 11:32:14.589: D/dalvikvm(1507): GC_CONCURRENT freed <1K, 6% free 3202K/3376K, paused 5ms+73ms, total 146ms
- 12-29 11:32:14.609: D/AndroidRuntime(1507): Shutting down VM
- 12-29 11:32:14.609: W/dalvikvm(1507): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): FATAL EXCEPTION: main
- 12-29 11:32:14.629: E/AndroidRuntime(1507): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsonapp/com.example.jsonapp.AlbumsActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at android.app.ActivityThread.access$600(ActivityThread.java:141)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at android.os.Handler.dispatchMessage(Handler.java:99)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at android.os.Looper.loop(Looper.java:137)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at android.app.ActivityThread.main(ActivityThread.java:5039)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at java.lang.reflect.Method.invokeNative(Native Method)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at java.lang.reflect.Method.invoke(Method.java:511)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at dalvik.system.NativeStart.main(Native Method)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at android.app.Activity.setContentView(Activity.java:1881)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at com.example.jsonapp.AlbumsActivity.onCreate(AlbumsActivity.java:58)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at android.app.Activity.performCreate(Activity.java:5104)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
- 12-29 11:32:14.629: E/AndroidRuntime(1507): ... 11 more
- 12-29 11:32:28.329: I/Process(1507): Sending signal. PID: 1507 SIG: 9
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement