2011-11-25 42 views
0

在我的應用程序中,我有一個微調列表的地方(如機場,商場..)。當我點擊機場的地方從微調,它應該顯示列表中的機場auch如(撒哈拉沙漠,朱胡)下方微調在列表視圖。我試過這個,但是它混合了機場和商場的地方列表。 Whts錯this.Plz幫助me.Below是即時通訊使用JSON: -微調選擇一個顯示android中的listview?

{ 
    "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; 
    } 

} 
} 

回答

0

看來myList包含值 - 機場,購物中心等。Placelist包含所有地方,包括機場和商場。並且您在以下代碼中提供了相同的地點列表 -

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

打印placelist並確認它是否包含兩組地點。如果是這樣,您需要爲機場和商場創建單獨的列表。

相關問題