2011-11-25 2 views
0

내 앱에는 스피너 (공항, 쇼핑몰 등) 목록이있는 스피너가 있습니다. 스피너에서 공항 위치를 클릭하면 목록보기의 스피너 아래에 (공항, 주후) 공항 목록이 표시됩니다. 나는 이것을 시도했지만 공항과 쇼핑몰의 장소 목록을 혼합합니다. this.Plz 도움말 me.Below 잘못 Whts 사용하여 메신저 json으로 수 있습니다 : -Spinner 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을하고 장소의 두 세트가 포함 된 경우 확인 - 그리고 당신은 다음 코드에서 같은 placelist를 제공하고 있습니다. 그렇다면 공항과 쇼핑몰을위한 별도의 목록을 만들어야합니다.

관련 문제