2017-01-25 161 views
0

我在我的家庭活動中有3個微調。如果我選​​擇1個其他應該根據第一個微調改變。我有國家,城市和位置作爲微調,如果我選擇國家,那麼城市和位置應該改變根據那。現在我設法得到微調(從數據庫中的項目)的國家項目。現在如何獲得微調選項中的項目..迷惑。 這裏是我的家活動(其中我得到微調(國家)項):如何獲得微調項目的選擇先前的微調在Android?

public class Home extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener { 

    private Spinner countrySpinner, locationSpinner, citySpinner; 
    private TextView cityCodeTextView; 
    private Button submitButton; 
    private ArrayList<String> country_list, location_list, city_list; 
    private JSONArray result; 
    ProgressDialog progressDialog; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 

     countrySpinner = (Spinner) findViewById(Country); 



     //citySpinner = (Spinner) findViewById(City); 
     //locationSpinner = (Spinner) findViewById(R.id.Location); 
     countrySpinner .setOnItemSelectedListener(this); 

     country_list = new ArrayList<String>(); 

     //location_list = new ArrayList<String>(); 
     // city_list = new ArrayList<String>(); 

     getData(); 
    } 
    private void getData(){ 
     StringRequest 
       stringRequest = new StringRequest(Config.DATA_URL, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         JSONObject j = null; 
         try { 
          Log.d("Test",response); 
          JSONArray result = new JSONArray(response); 
          //Calling method getCountry to get the Country from the JSON Array 
          getCountry(result); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       },new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
      }}); 
     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     //Adding request to the queue 
     requestQueue.add(stringRequest); 
    } 
    private void getCountry(JSONArray jsonArrayCountry){ 
     //Traversing through all the items in the json array 
     List<Country> countries = new ArrayList<>(); 
     try { 
      String country_name, country_code; 
      JSONObject countries_object; 
      for (int i = 0; i < jsonArrayCountry.length(); i++) { 
       countries_object = jsonArrayCountry.getJSONObject(i); 
       country_code = countries_object.getString("id"); 
       country_name = countries_object.getString("country"); 
       countries.add(new Country(country_code, country_name)); 
      } 
      ArrayAdapter countryAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, countries); 
      countrySpinner.setPrompt("--Select Country--"); 
      countrySpinner.setAdapter(countryAdapter); 
      countrySpinner.setAdapter(new NothingSelectedSpinnerAdapter(countryAdapter, 
          R.layout.contact_spinner_row_nothing_selected,this)); 
      countrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
       @Override 
       public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

       } 
       @Override 
       public void onNothingSelected(AdapterView<?> arg0) { 
       } 
      }); 

     } catch (JSONException e) { 
      Log.e("Home", e.getLocalizedMessage(), e); 
     } 
    } 
    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     //Setting the values to textviews for a selected item 
     //textViewName.setText(getName(position)); 
     //textViewCourse.setText(getCourse(position)); 
     //textViewSession.setText(getSession(position)); 
    } 

    //When no item is selected this method would execute 
    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 
     // textViewName.setText(""); 
     // textViewCourse.setText(""); 
     //textViewSession.setText(""); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    } 
} 

這是Country.java

public class Country { 

     private String name; 
     private String id; 

     public Country(String id, String name) { 
      this.name = name; 
      this.id = id; 
     } 

     public String getName() { 
      return name; 
     } 

     public String getId() { 
      return id; 
     } 
    @Override 
     public String toString() { 
      return name; 
     } 
    } 

這是City.java

public class City { 

    private String name; 



    public String getName() { 
     return name; 
    } 



    @Override 
    public String toString() { 
     return name; 
    } 
} 

這是Location.java

public class Location { 

    private String name; 



    public String getName() { 
     return name; 
    } 



    @Override 
    public String toString() { 
     return name; 
    } 
} 

這是Config.java

public class Config { 
    //JSON URL 
    public static final String DATA_URL = "http://demo5...../get_country.php"; 

    public static final String DATA_URL1 = "http://demo5...../get_jsoncity.php?id="; 

    //Tags used in the JSON String 

public static final String DATA_URL2 = "http://demo5...../get_jsonlocation.php?id="; 

    //Tags used in the JSON String 
    //JSON array name 
    public static final String JSON_ARRAY = "result"; 

} 

這是我第一次微調JSON:

[ 
    { 
    "id": "1", 
    "country": "UAE" 
    }, 
    { 
    "id": "2", 
    "country": "UK" 
    }, 
    { 
    "id": "3", 
    "country": "SAUDI ARABIA" 
    }, 
    { 
    "id": "4", 
    "country": "OMAN" 
    }, 
    { 
    "id": "5", 
    "country": "BAHRAIN" 
    }, 
    { 
    "id": "6", 
    "country": "INDIA" 
    } 
] 

這是對城市,如果我選擇的ID = 1

[ 
    { 
    "id": "1", 
    "city_name": "Abu Dhabi" 
    }, 
    { 
    "id": "2", 
    "city_name": "Dubai" 
    }, 
    { 
    "id": "3", 
    "city_name": "Sharjah" 
    }, 
    { 
    "id": "4", 
    "city_name": "Ajman" 
    }, 
    { 
    "id": "5", 
    "city_name": "Ummal Qwain" 
    } 
] 

我已經得到第一個微調項目即國家,我需要根據第一個微調選項獲得城市(第二微調)和位置(第三微調)的項目。

+0

見本教程; http://www.androidhive.info/2013/12/android-populating-spinner-data-from-mysql-database/ – rafsanahmad007

+0

是的,我看到了,但我需要3微調..thts 1 –

+0

,如果我選擇其他1 2 shud從服務器n得到所選項目不是所有項目 –

回答

0

你的country.class應該有getter setter的城市和位置。

public class Country { 
     private String name; 
      private String id; 
private Location loc; 
     ArrayList<City> Cityarr = new ArrayList<City>(); 

     public ArrayList<City> getCityarr() { 
      return Cityarr; 
     } 

     public void setCityarr(ArrayList<City> Citya) { 
      this.Cityarr = Citya; 
     } 

     public Country(String id, String name) { 
       this.name = name; 
       this.id = id; 
      } 

      public String getName() { 
       return name; 
      } 

      public String getId() { 
       return id; 
      } 
public Location getLocation() { 
     return loc; 
    } 
     @Override 
      public String toString() { 
       return name; 
      } 
     } 

和spinner.OnItemSelectedLisenter應該嵌套

country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
        @Override 
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
         vbopr.get(i).getCityarr().size(); 

         cityname.clear(); 
         cityname.add("All"); 
         for (int j = 0; j < vbopr.get(i).getCityarr().size(); j++) { 
          cityname.add(vbopr.get(i).getCityarr().get(j).getCityname()); 
         } 
         try { 
          adpcity.notifyDataSetChanged(); 
         } catch (NullPointerException ne) { 

         } 
         adpcity = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_items, cityname); 
         city.setAdapter(adpcity); 

        } 

        @Override 
        public void onNothingSelected(AdapterView<?> adapterView) { 

        } 
       }); 

我的網址包含XML形式 例如

<world> 
<country> 
<countryid>2</countryid> 
<countryname>India</countryname> 
<city> 
<city_id>1462</city_id> 
<city_name>abc</city_name> 
</city> 

<city> 
<city_id>1460</city_id> 
<city_name>def</city_name> 
</city> 

<city> 
<city_id>1461</city_id> 
<city_name>jkl PLANT</city_name> 
</city> 

</country> 

<country> 
<countryid>3</countryid> 
<countryname>Australia</countryname> 
<city> 
<city_id>1462</city_id> 
<city_name>gdfg PLANT</city_name> 
</city> 

<city> 
<city_id>1460</city_id> 
<city_name>xdfPLANT</city_name> 
</city> 

<city> 
<city_id>1461</city_id> 
<city_name>dfgPLANT</city_name> 
</city> 

<city> 
<city_id>617</city_id> 
<city_name>dgvdfgg</city_name> 
</city> 
</country> 
</world> 
+0

你的意思是說一個項目選擇我們可以得到3旋轉的所有項目.. –

+0

可以解釋我們如何可以顯示來自url的數據 –

+0

因爲我有3個url在config.java ..我只需要使php文件...作爲即時通訊的小你可以告訴我.. –

0

數據試試這個:

spinner_1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

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

        if (spinner_1.getSelectedItem().equals("UAE")) { 

         //cal api url here to get data 
         // set adapter to spinner_2 here for "UAE" selected 

        } else if (spinner_1.getSelectedItem().equals("UK")) { 
         //same.. 
        } 
       } 


       @Override 
       public void onNothingSelected(AdapterView<?> arg0) { 
       } 
      } 
    ); 
+0

不,我不能這樣做tht方式..國家的名稱可以改變..我無法採取硬編碼的名稱 –

+0

你做了任何例子,如果你點擊國家的微調,你會得到在其他項目2動態微調 –

+0

@ z.al是的...但在你的情況下,它是不同的..我想如果你想你的選擇動態...你可以傳遞第一個微調選擇器的值到服務器..並在服務器端,你可以返回適當的JSON respnse其他微調... – rafsanahmad007

相關問題