Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Parsing Issue with Spinners

All my spinners are connected with each other, they are not independent, I would like to show options in spinner2 and spinner3 based on the selection in spinner1 (I mean based on category chosen by user in spinner1)

In onCreate I am populating data into Spinners, but in spinner2 and spinner3 I am getting data that belongs to CategoryB whereas they must populate with CategoryA data only.

So where is my mistake? Here is my JSON Parsing code:

               categoryArrayList = new ArrayList<Category>();
               cArrayList = new ArrayList<String>();

                ...................................

                // Array Level 1 --- START
            JSONArray jarray = jsono.getJSONArray("categories");

            for (int i = 0; i < jarray.length(); i++) {
                JSONObject object = jarray.getJSONObject(i);

                Category language = new Category();                     
                language.setName(object.getString("category_name"));
                Log.d("category_name::-", object.getString("category_name"));

                language.setTypeArrayList(typeArrayList);
                categoryArrayList.add(language);
                cArrayList.add(categoryArrayList.get(i).getName());

                // Array Level 1 --- END

                // Array Level 2 --- START

                JSONArray jsarray = object.getJSONArray("types");
                typeArrayList = new ArrayList<Type>();
                tArrayList = new ArrayList<String>();
                for (int j = 0; j < jsarray.length(); j++) {
                    JSONObject jjobject = jsarray.getJSONObject(j);

                    Type genre = new Type();

                    genre.setName(jjobject.getString("type_name"));
                    Log.d("type_name::-", jjobject.getString("type_name"));     

                    genre.setServiceArrayList(serviceArrayList);
                    typeArrayList.add(genre);
                    tArrayList.add(typeArrayList.get(j).getName());     

                    // Array Level 2 --- END

                    // Array Level 3 --- START

                    JSONArray jsonarray = jjobject.getJSONArray("services");
                    serviceArrayList = new ArrayList<Service>();
                    sArrayList = new ArrayList<String>();
                    for (int k = 0; k < jsonarray.length(); k++) {
                        JSONObject jjjobject = jsonarray.getJSONObject(k);

                        Service movie = new Service();

                        movie.setName(jjjobject.getString("service_name"));
                        Log.d("service_name::-", jjjobject.getString("service_name"));

                        serviceArrayList.add(movie);    
                        sArrayList.add(serviceArrayList.get(k).getName());

                        // Array Level 3 --- END
                    }

                }       

            }

            return true;
        }

And here is how i am populating Spinners:

            spinner1.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
                     android.R.layout.simple_spinner_dropdown_item,
                     cArrayList));

            spinner2.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
                    android.R.layout.simple_spinner_dropdown_item,
                    tArrayList));

            spinner3.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
                    android.R.layout.simple_spinner_dropdown_item,
                    sArrayList));
like image 286
Oreo Avatar asked Sep 11 '15 05:09

Oreo


2 Answers

Oreo. Here is the full code.

Category class

public class Category {

    String name;
    ArrayList<ArrayList<Type>> type;
    Category()
    {
        name = "";
        type = new ArrayList<ArrayList<Type>>(); 
    }

    void setName(String s)
    {
        name =s;
    }

    public String getName()
    {
        return name;
    }

    void setTypeArrayList(ArrayList<Type> serviceArrayList)
    {
        type.add(serviceArrayList);
    }

    ArrayList<Type> getTypeArrayList(int i)
    {
        return type.get(i);
    }
}

Type class

import java.util.ArrayList;

public class Type {
    String type;
    ArrayList<ArrayList<Service>> service;

    public Type() {
        // TODO Auto-generated constructor stub
        type = "";
        service = new ArrayList<ArrayList<Service>>();
    }
    void setName(String s)
    {
        type =s;
    }

    public String getName()
    {
        return type;
    }

    void setServiceArrayList(ArrayList<Service> serviceArrayList)
    {
        service.add(serviceArrayList);
    }

    ArrayList<Service> getServiceArrayList(int i)
    {
        return service.get(i);
    }
}

Service class

public class Service {

    String service;

    public Service() {
        // TODO Auto-generated constructor stub
        service ="";
    }

    void setName(String s)
    {
        service =s;
    }

    public String getName()
    {
        return service;
    }
}

initialize global

 int catpos =0;
 ArrayList<Category> categoryArrayList = new ArrayList<Category>();
 ArrayList<String> cArrayList = new ArrayList<String>();
 ArrayList<Type> typeArrayList;
 ArrayList<Service> serviceArrayList;

doInbackground

    //your http request and response process


    JSONObject jsono;
    try {
        jsono = new JSONObject(data);

        JSONArray jarray = jsono.getJSONArray("categories");

        for (int i = 0; i < jarray.length(); i++) {
            JSONObject object = jarray.getJSONObject(i);

            Category language = new Category();
            language.setName(object.getString("category_name"));
            Log.d("category_name::-", object.getString("category_name"));

            JSONArray jsarray = object.getJSONArray("types");
            typeArrayList = new ArrayList<Type>();
            tArrayList = new ArrayList<String>();
            for (int j = 0; j < jsarray.length(); j++) {
                JSONObject jjobject = jsarray.getJSONObject(j);

                Type genre = new Type();

                genre.setName(jjobject.getString("type_name"));
                Log.d("type_name::-", jjobject.getString("type_name"));

                JSONArray jsonarray = jjobject.getJSONArray("services");
                serviceArrayList = new ArrayList<Service>();
                sArrayList = new ArrayList<String>();
                for (int k = 0; k < jsonarray.length(); k++) {
                    JSONObject jjjobject = jsonarray.getJSONObject(k);

                    Service movie = new Service();

                    movie.setName(jjjobject.getString("service_name"));
                    Log.d("service_name::-",
                            jjjobject.getString("service_name"));

                    serviceArrayList.add(movie);
                    sArrayList.add(serviceArrayList.get(k).getName());
                }
                genre.setServiceArrayList(serviceArrayList);
                typeArrayList.add(genre);
                tArrayList.add(typeArrayList.get(j).getName());
            }

            language.setTypeArrayList(typeArrayList);
            categoryArrayList.add(language);
            cArrayList.add(categoryArrayList.get(i).getName());

        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

onPostExecute Method

    tArrayList = new ArrayList<String>();
    sArrayList = new ArrayList<String>();

    Category c = categoryArrayList.get(0);      
    typeArrayList = c.getTypeArrayList(0);
    for(int i=0;i<typeArrayList.size();i++)
        tArrayList.add(typeArrayList.get(i).getName());
    Type t = typeArrayList.get(0);
    serviceArrayList = t.getServiceArrayList(0);
    for(int i=0;i<serviceArrayList.size();i++)
        sArrayList.add(serviceArrayList.get(i).getName());

    spinner1.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
            android.R.layout.simple_spinner_dropdown_item, cArrayList));

    spinner2.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
            android.R.layout.simple_spinner_dropdown_item, tArrayList));


    spinner3.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
            android.R.layout.simple_spinner_dropdown_item, sArrayList));

    spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {

            catpos = pos;
            tArrayList = new ArrayList<String>();
            sArrayList = new ArrayList<String>();
            Category c = categoryArrayList.get(pos);        
            typeArrayList = c.getTypeArrayList(0);
            for(int i=0;i<typeArrayList.size();i++)
                tArrayList.add(typeArrayList.get(i).getName());
            Type t = typeArrayList.get(0);
            serviceArrayList = t.getServiceArrayList(0);
            for(int i=0;i<serviceArrayList.size();i++)
                sArrayList.add(serviceArrayList.get(i).getName());

            spinner2.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
                    android.R.layout.simple_spinner_dropdown_item, tArrayList));


            spinner3.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
                    android.R.layout.simple_spinner_dropdown_item, sArrayList));


        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

    spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {

            sArrayList = new ArrayList<String>();
            Category c = categoryArrayList.get(catpos);     
            typeArrayList = c.getTypeArrayList(0);              
            Type t = typeArrayList.get(pos);
            serviceArrayList = t.getServiceArrayList(0);
            for(int i=0;i<serviceArrayList.size();i++)
                sArrayList.add(serviceArrayList.get(i).getName());

            spinner3.setAdapter(new ArrayAdapter<String>(CategoryActivity.this,
                    android.R.layout.simple_spinner_dropdown_item, sArrayList));                
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });
like image 150
arun Avatar answered Nov 09 '22 04:11

arun


This should do what you want, however, I am not sure why you are reading a json, but you seems to know the content already. i.e. you want something so specific in spinner 3

           categoryArrayList = new ArrayList<Category>();
           cArrayList = new ArrayList<String>();

            ...................................

            JSONArray jarray = jsono.getJSONArray("categories");

            for (int i = 0; i < jarray.length(); i++) {
                JSONObject object = jarray.getJSONObject(i);

                Category language = new Category();                     
                language.setName(object.getString("category_name"));
                Log.d("category_name::-", object.getString("category_name"));

                language.setTypeArrayList(typeArrayList);
                categoryArrayList.add(language);
                cArrayList.add(categoryArrayList.get(i).getName());

            }

            JSONArray jsarray = jarray.getJSONObject(0).getJSONArray("types");
            typeArrayList = new ArrayList<Type>();
            tArrayList = new ArrayList<String>();
            for (int j = 0; j < jsarray.length(); j++) {
                JSONObject jjobject = jsarray.getJSONObject(j);

                Type genre = new Type();

                genre.setName(jjobject.getString("type_name"));
                Log.d("type_name::-", jjobject.getString("type_name"));                     

                genre.setServiceArrayList(serviceArrayList);
                typeArrayList.add(genre);
                tArrayList.add(typeArrayList.get(j).getName());         
            }       

            JSONArray jsonarray = jsarray.getJSONObject(1).getJSONArray("services");
            serviceArrayList = new ArrayList<Service>();
            sArrayList = new ArrayList<String>();
            for (int k = 0; k < jsonarray.length(); k++) {
                JSONObject jjjobject = jsonarray.getJSONObject(k);

                Service movie = new Service();

                movie.setName(jjjobject.getString("service_name"));
                Log.d("service_name::-", jjjobject.getString("service_name"));

                serviceArrayList.add(movie);    
                sArrayList.add(serviceArrayList.get(k).getName());
            }
            return true;
like image 40
Derek Fung Avatar answered Nov 09 '22 04:11

Derek Fung