0

Getting only first Event's Angel Names in a List, either i do tap on second or third event or any other Event..

i want whenever user do tap on Event Honda Motors need to show Angel A & B, and whenever user do tap on Event Tata Motors need to show Angel C & D....i have around 200 events in my json and over 1000 angels, some events having 50 or some having 200 angels, but i need to show Angels for tapped Event only.....

JSON :-

[
    {
     "eventName" : "Honda Motors",
     "angelList" : [
    {
    "angelID": "1",
        "angelName": "Angel A"
    },
    {
    "angelID": "2",
        "angelName": "Angel B"
    }
    ]
   },


    {
     "eventName" : "Tata Motors",
     "angelList" : [
    {
    "angelID": "1",
        "angelName": "Angel C"
    },
    {
    "angelID": "2",
        "angelName": "Angel D"
    }
    ]
   }
 ]

for an example, first EventName is Honda Motors, having two Angels Angel A, B and second EventName is Tata Motors, this event has two more Angels namely Angel C, Angel D .... and my issue is whenever i do tap on Tata Motors getting Angel A, Angel B in a List, instead of showing Angel C, Angel D...

Code :-

// Creating JSON Parser object
        JSONParser jsonParser = new JSONParser();

        ArrayList<HashMap<String, String>> angelsList;

        // angels JSONArray
        JSONArray arrayAngels = null;

protected String doInBackground(String... args) {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                // getting JSON string from URL
                String json = jsonParser.makeHttpRequest(EventsActivity.URL_JSON, "GET", params);
                JSONObject jsonData;
                try {
                    jsonData = new JSONArray(json).getJSONObject(0);
                    arrayAngels = jsonData.optJSONArray("angelList");

                    if (arrayAngels != null) {

                        // looping through all angels
                        for (int i = 0; i < arrayAngels.length(); i++) {
                            JSONObject c = arrayAngels.getJSONObject(i);
                            // Storing each json item values in variable
                            String name = c.getString(ANGEL_NAME);                            
                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();
                            // adding each child node to HashMap key => value
                            map.put(ANGEL_NAME, name);                          
                            // adding HashList to ArrayList
                            angelsList.add(map);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
               }
                return null;
            }

Complete code:-

public class AngelsActivity 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>> angelsList;

        // angels JSONArray
        JSONArray arrayAngels = null;

        String intentEvents = null;

        public static final String LOG_TAG = "AngelsActivity";

        // JSON node names
        private static final String ANGEL_NAME = "angelName";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list);

            cd = new ConnectionDetector(getApplicationContext());

            // Check for internet connection
            if (!cd.isConnectingToInternet()) {
                // Internet Connection is not present
                alert.showAlertDialog(AngelsActivity.this, "Internet Connection Error",
                        "Please connect to working Internet connection", false);
                // stop executing code by return
                return;
            }

            // Get EventName using Intent (from EventsActivity)
            Intent i = getIntent();
            intentEvents = (i.getStringExtra("event_name"));

            // Hashmap for ListView
            angelsList = new ArrayList<HashMap<String, String>>();

            // Loading Angels JSON in Background Thread
            new LoadAngels().execute();

            // get listview
            ListView lv = getListView();

            lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                        long arg3) {

                }
            });     
        }

        /**
         * Background Async Task to Load all Angels by making http request
         * */
        class LoadAngels extends AsyncTask<String, String, String> {

            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(AngelsActivity.this);
                pDialog.setMessage("Loading Data ...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }

            protected String doInBackground(String... args) {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                // getting JSON string from URL
                String json = jsonParser.makeHttpRequest(EventsActivity.URL_JSON, "GET", params);
                JSONArray jsonData;
                try {
                    jsonData = new JSONArray(json);
                    for (int j = 0; j < jsonData.length(); j++) {
                        arrayAngels = jsonData.getJSONObject(j).optJSONArray("angelList");

                    if (arrayAngels != null) {

                        // looping through all angels
                        for (int i = 0; i < arrayAngels.length(); i++) {
                            Log.d(AngelsActivity.LOG_TAG, "arrayAngels.length " + arrayAngels.length());
                            JSONObject c = arrayAngels.getJSONObject(i);
                            // Storing each json item values in variable
                            String name = c.getString(ANGEL_NAME);                            
                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();
                            // adding each child node to HashMap key => value
                            map.put(ANGEL_NAME, name);                          
                            // adding HashList to ArrayList
                            angelsList.add(map);
                        }
                    }
                    }
                } 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 angels
                pDialog.dismiss();
                // updating UI from Background Thread
                runOnUiThread(new Runnable() {
                    public void run() {
                        /**
                         * Updating parsed JSON data into ListView
                         * */
                        ListAdapter adapter = new SimpleAdapter(
                                AngelsActivity.this, angelsList,
                                R.layout.list_item, 
                                new String[] {ANGEL_NAME}, 
                                new int[] {R.id.name });

                        // updating listview
                        setListAdapter(adapter);

                        // Change activity Title with Event Name
                        setTitle(intentEvents);
                    }
                });
            }
        }
    }
Sun
  • 6,768
  • 25
  • 76
  • 131
  • I want to scream when I see people parsing json manually, Why you are not using gSon? Its will take one line to parse or generate json using this library. https://code.google.com/p/google-gson/ – M-Wajeeh Nov 26 '13 at 08:57
  • arrayAngels.length() what the size of it? – shreyansh jogi Nov 26 '13 at 08:58
  • Agreed with top comment, use GSON. It maps nicely to POJO's – Ben Dale Nov 26 '13 at 09:01
  • don't know how to use GSON – Sun Nov 26 '13 at 09:01
  • did you check this http://pastie.org/8507358 – SathishKumar Nov 26 '13 at 09:43
  • yeah but not resolved @SathishKumar – Sun Nov 26 '13 at 09:44
  • you said that, you used tab, depends on that tab you need to pass your tab position, and set adapter. But u used listview only i confused. in that list view what you trying to show – SathishKumar Nov 26 '13 at 09:47
  • no i want whenever user do tap on Event Honda Motors need to show Angel A & B, and whenever user do tap on Event Tata Motors need to show Angel C & D....i have around 200 events in my json and over 1000 angels, some events having 50 or some having 200 angels, but i need to show Angels for tapped Event only..... @SathishKumar – Sun Nov 26 '13 at 09:49
  • yeah that is fine, but are using 200 tabs? – SathishKumar Nov 26 '13 at 09:49
  • i am not using tabs, i am fetching data into listview using JSON, Let think EventName as Category Name and AngelName as SubCategory Name... now clear @SathishKumar – Sun Nov 26 '13 at 09:50
  • so u need to show eventname as well as category name in same list view right? – SathishKumar Nov 26 '13 at 09:56
  • first need to show list of Events like: Honda Motors, Tata Motors etc... once user do tap on Honda Motors then just need to show their Angels @SathishKumar – Sun Nov 26 '13 at 10:01
  • @SathishKumar any help bro much needed – Sun Nov 26 '13 at 10:31
  • mskumar51@gmail.com my mail id, i have some query – SathishKumar Nov 26 '13 at 10:38

1 Answers1

0

In your adapter, change jsonData = new JSONArray(json).getJSONObject(0); by jsonData = new JSONArray(json).getJSONObject(yourPosition);

Damien R.
  • 3,383
  • 1
  • 21
  • 32
  • now getting all the angel names, earlier was getting Angel A & Angel B... but now getting Angel A, Angel B, Angel C, Angel D.... not matter which event you tapped.... – Sun Nov 26 '13 at 09:07
  • What do you want to get ? Only C and D ? – Damien R. Nov 26 '13 at 09:08
  • i want whenever user do tap on Event Honda Motors need to show Angel A & B, and whenever user do tap on Event Tata Motors need to show Angel C & D....i have around 200 events in my json and over 1000 angels, some events having 50 or some having 200 angels, but i need to show Angels of tapped Event – Sun Nov 26 '13 at 09:11
  • You need to create a custom adapter to do this. Check this answer: http://stackoverflow.com/a/8166802/2065418 – Damien R. Nov 26 '13 at 09:17
  • already using, this is not that kind of issue, it is very clear not able to get angel names for particular tapped event – Sun Nov 26 '13 at 09:19
  • OK. In your adapter, you get the position the the clicked row. Now in your code you posted, just changed `jsonData = new JSONArray(json).getJSONObject(0);` by `jsonData = new JSONArray(json).getJSONObject(yourPosition);` And that should be good. Sorry, didn't really get the issue at first. – Damien R. Nov 26 '13 at 09:21
  • Is it working now? i've edited my answer so you can accept it if it's ok. – Damien R. Nov 26 '13 at 10:04
  • sorry not getting i posted my whole code, please make changes in my code with yours – Sun Nov 26 '13 at 10:11