0

In my app i have a Spinner with list of places(such as airport,mall..).When i click on Airport place from spinner,it should show list of airports auch as(sahara,juhu) below spinner in listview.I have tried this but it mixes list of places of airports and malls. Whts wrong in this.Plz help me.Below is the json which i m using:-

 {
  "PlacesList": {
"PlaceType": [
  {
    "-Name": "Airport",
    "Places": {
      "Place": [
        {
          "name": "Juhu Aerodrome",
          "latitude": "19.09778",
          "longitude": "72.83083",
          "description": "Juhu Aerodrome is an airport that serves the metropolitan" 
        },
        {
          "name": "Chhatrapati Shivaji International Airport",
          "latitude": "19.09353",
          "longitude": "72.85489",
          "description": "Chhatrapati Shivaji International Airport  "
        }
      ]
    }
  },
  {
    "-Name": "Mall",
    "Places": {
      "Place": [
        {
          "name": "Infinity",
          "latitude": "19.14030",
          "longitude": "72.83180",
          "description": "This Mall is one of the best places for all types of brand"
        },
        {
          "name": "Heera Panna",
          "latitude": "18.98283",
          "longitude": "72.80897",
          "description": "The Heera Panna Shopping Center is one of the most popular"
        }
      ]
    }
  }
]
  }
} 

///////////////////////////////////

public class PlaceDemo extends Activity 
{
private String jString;
private Spinner spinner;
private ListView lister2;

private ByteArrayInputStream json;
private ArrayList<HashMap<String, String>> mylist;
private PlaceType pttype;

private List<PlaceType> results;
private Place place;
private List<Place> places;
private ArrayList<HashMap<String, String>> placelist;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try 
    {
        InputStream is = getAssets().open("place_file.json");

        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try 
        {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) 
            {
                writer.write(buffer, 0, n);
            }
        } 
        finally 
        {
            is.close();
        }

        jString = writer.toString();
        System.out.println(jString);


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


    spinner = (Spinner) findViewById(R.id.spinner);
    lister2 = (ListView) findViewById(R.id.ListView2);

        try 
        {
            json = new ByteArrayInputStream(jString.getBytes("UTF-8"));
        } 
        catch (UnsupportedEncodingException e1) 
        {
            e1.printStackTrace();
        }

        try
        {

             Gson gson = new Gson();

                        Reader reader = new InputStreamReader(json);

                        Places response = gson.fromJson(reader,Places.class);

                        results = response.plist;

        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }

        mylist = new ArrayList<HashMap<String, String>>();
        placelist = new ArrayList<HashMap<String, String>>();

        try
        {   
            for (int i = 0; i < results.size(); i++) 
            {
                pttype = results.get(i);

                        HashMap<String, String> map = new HashMap<String, String>();    

                        map.put("name",pttype.name);

                        mylist.add(map);

            }

            for (int i = 0; i < results.size(); i++) 
            {
                pttype = results.get(i);

                        HashMap<String, String> map1 = new HashMap<String, String>();   

                        places = pttype.place;

                        for (int j = 0; j < places.size(); j++) 
                        {
                            place =places.get(j);

                            map1.put("desc",place.desc);
                            map1.put("lat",place.lat);
                            map1.put("lng",place.lng);
                            map1.put("name1",place.name);

                            placelist.add(map1);                
                        }


            }
        }
        catch (Exception e1) 
        {
                    e1.printStackTrace();
        }

        SpinnerAdapter adapter = new SpinnerAdapter(this, mylist , R.layout.row, 
                    new String[] { "name" }, 
                    new int[] { android.R.id.text1});
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

         SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {

                public boolean setViewValue(View view, Object data,
                        String textRepresentation) {

                    TextView textView = (TextView) view;
                    textView.setText(textRepresentation);
                    return true;
                }
            };

        adapter.setViewBinder(viewBinder);
          spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
        {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3)
            {

                String[] lister = null;
                switch (position) 
                {
                case 0:
                    String[] airport = new String[] { "name1" };
                    lister = airport ;

                    break;
                case 1:
                    String[] mall = new String[] { "name1" };
                    lister = mall ;

                    break;

                }


                lister2.setAdapter(new PlaceAdapter(PlaceDemo.this, placelist , R.layout.row, 
                        lister, 
                        new int[] { R.id.tt1}));
                lister2.setVisibility (View.VISIBLE);
            }

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

            }
        });

}


public class PlaceAdapter extends SimpleAdapter 
{

    private ArrayList<HashMap<String, String>> results;

    public PlaceAdapter(Context context, ArrayList<HashMap<String, String>> data, int resource, String[] from, int[] to) 
    {
        super(context, data, resource, from, to);
        this.results = data;
    }

    public View getView(int position, View view, ViewGroup parent)
    {

      View v = super.getView(position, view, parent);

        if (v == null) 
        {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }

        TextView id = (TextView) v.findViewById(R.id.tt1);
        id.setText(results.get(position).get("name1"));

        return v;
    }

}

public class SpinnerAdapter extends SimpleAdapter 
{

    private ArrayList<HashMap<String, String>> results;

    public SpinnerAdapter(Context context, ArrayList<HashMap<String, String>> data, int resource, String[] from, int[] to) 
    {
        super(context, data, resource, from, to);
        this.results = data;
    }

    public View getView(int position, View view, ViewGroup parent)
    {

      View v = super.getView(position, view, parent);

        if (v == null) 
        {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }

        TextView id = (TextView) v.findViewById(R.id.tt1);
        id.setText(results.get(position).get("name"));

        return v;
    }

}
 }
stella
  • 441
  • 4
  • 12

1 Answers1

0

It seems myList contains values - Airport, Mall etc. Placelist contains all places including airports and malls. And you are providing the same placelist in the following code -

    lister2.setAdapter(new PlaceAdapter(PlaceDemo.this, placelist , R.layout.row,
    lister, new int[] { R.id.tt1}));
    lister2.setVisibility (View.VISIBLE); 

Print placelist and confirm if it contains both sets of places. If so, you need to create separate lists for airports and malls.

user994886
  • 444
  • 4
  • 13