2016-03-31 9 views
0

私はあまりコードを経てと私が間違っているのかを把握しようとしましたが、私はちょうど私がAndroidアプリを開発しています依存アンドロイドスピナーは

.. time.Iが出て、それを把握することはできません無駄にしていています1つのアクティビティに2つのスピナーを配置する必要があります。 2番目のスピナーは最初のスピナーで選択されたエントリに基づいて作成され、すべてのデータはjsonから取得されます。

私はmysqlテーブルを持っているので、私は2つの列、すなわち国と都市を持っています。私は2つのスピナーでそれぞれ国と都市のデータを取得しました。ユーザーが国名を選択すると、その特定の国の都市は2番目のスピナーに表示されます。

MainActivity.java

ここ
public class LinearLayout extends Activity implements 
    View.OnClickListener{ 
ArrayList<String> listItems=new ArrayList<>(); 
ArrayList<String> listItems1=new ArrayList<>(); 
ArrayAdapter<String> adapter; 
ArrayAdapter<String> adapter1; 
Button submit; 
Spinner s1,s2; 
String text=""; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.linear_layout); 
    submit=(Button)findViewById(R.id.submit); 
    submit.setOnClickListener(this); 
    s1 = (Spinner)findViewById(R.id.spinner1); 
    adapter=new ArrayAdapter<>(this,R.layout.spinner_layout,R.id.txt,listItems); 
    s1.setAdapter(adapter); 
    s2 = (Spinner)findViewById(R.id.spinner2); 
    adapter1=new ArrayAdapter<>(this,R.layout.spinner_layout,R.id.txt,listItems1); 
    s2.setAdapter(adapter1); 
     } 
public void onStart(){ 
    super.onStart(); 
    BackTask bt=new BackTask(); 
    bt.execute(); 
} 
private class BackTask extends AsyncTask<Void,Void,Void> { 
    ArrayList<String> list; 
    ArrayList<String> list1; 
    protected void onPreExecute(){ 
     super.onPreExecute(); 
     list=new ArrayList<>(); 
     list1=new ArrayList<>(); 
    } 
    protected Void doInBackground(Void...params){ 
     InputStream is=null; 
     String result=""; 
     String result1=""; 
     InputStream is1=null; 
     try{ 
      HttpClient httpclient=new DefaultHttpClient(); 
      HttpPost httppost= new HttpPost("http://192.168.3.2/countries.php"); 
      HttpResponse response=httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      // Get our response as a String. 
      is = entity.getContent(); 
      HttpClient httpclient1=new DefaultHttpClient(); 
      HttpPost httppost1= new HttpPost("http://192.168.3.2/cities.php"); 
      HttpResponse response1=httpclient1.execute(httppost1); 
      HttpEntity entity1 = response1.getEntity(); 
      // Get our response as a String. 
      is1 = entity1.getContent(); 
     }catch(IOException e){ 
      e.printStackTrace(); 
     } 
     //convert response to string 
     try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8")); 
      BufferedReader reader1 = new BufferedReader(new InputStreamReader(is1,"utf-8")); 
      String line = null; 
      String line1= null; 
      while ((line = reader.readLine()) != null) { 
       result+=line; 
      } 
      while ((line1 = reader1.readLine()) != null) { 
       result1+=line1; 
      } 
      is.close(); 
      //result=sb.toString(); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     // parse json data 
     try{ 
      JSONArray jArray =new JSONArray(result); 
      //MANUFACTURER = new String[jArray.length()]; 
      for(int i=0;i<jArray.length();i++) { 
       JSONObject jsonObject = jArray.getJSONObject(i); 
       // add interviewee name to arraylist 
       list.add(jsonObject.getString("country")); 
      } 
       JSONArray jArray1 =new JSONArray(result1); 
       //MANUFACTURER = new String[jArray.length()]; 
       for(int j=0;j<jArray.length();j++){ 
        JSONObject jsonObject1=jArray1.getJSONObject(j); 
        // add interviewee name to arraylist 
        list1.add(jsonObject1.getString("city")); 
      } 
      //spinner_fn(); 
     } 
     catch(JSONException e){ 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    protected void onPostExecute(Void result){ 
     listItems.addAll(list); 
     adapter.notifyDataSetChanged(); 
     listItems1.addAll(list1); 
     adapter1.notifyDataSetChanged(); 
     //adapter1.notifyDataSetChanged(); 
    } 
} 

私は国や都市

<?php 
    $DB_USER='root'; 
    $DB_PASS='mysql'; 
    $DB_HOST='localhost'; 
    $DB_NAME='xxxx'; 

    $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME); 
    /* check connection */ 
    if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
    }  

    $mysqli->query("SET NAMES 'utf8'"); 
    $sql1="select city from ssss"; 

    $result1=$mysqli->query($sql1); 

    while($f=mysqli_fetch_assoc($result1)){ 
    $output1[]=$f; 
    } 
    print (json_encode($output1)); 
    $mysqli->close(); 
    ?>  

cities.php

countries.php

<?php 
    $DB_USER='root'; 
    $DB_PASS='mysql'; 
    $DB_HOST='localhost'; 
    $DB_NAME='xxxx'; 

    $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME); 
    /* check connection */ 
    if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
    }  

     $mysqli->query("SET NAMES 'utf8'"); 
    $sql="SELECT DISTINCT country from ssss"; 
    $result=$mysqli->query($sql); 
    while($e=mysqli_fetch_assoc($result)){ 
    $output[]=$e; 
    } 
    print(json_encode($output)); 
    $mysqli->close(); 
    ?>  

の値を取得するための2 PHPを持っていますここにはm yのJSON出力

[{"country":"india"},{"country":"england"},{"country":"australia"}] 

[{"city":"bangalore"},{"city":"kolkata"},{"city":"mumbai"},{"city":"london"},{"city":"manchester"},{"city":"southampton"},{"city":"canberra"},{"city":"sydney"},{"city":"melbourne"}] 

今私はonitemclicklistenerを追加する必要がありますが、私はいけないことを知っている依存の都市を取得する方法を知っている第二スピナーで

ここに私のデータベース

ID国の都市

です

1インドのバンガロール

インドコルカタ0

1インドムンバイ

2イングランドロンドン

2イングランドマンチェスター

2イングランドサウサンプトン

3オーストラリア、キャンベラ

3オーストラリアのシドニー

3オーストラリアメルボルン

+0

を支援を期待してみあなたの都市には、次のJSON配列であると仮定すると、その後のバンガロール、コルカタとムンバイは2番目のスピナーなどに表示されるべきです。 –

+0

ムンバイがインドにあることはどうやってわかりますか?国のキーを都市jsonに追加して、都市をその国に応じてフィルタリングする必要があります。 – SaNtoRiaN

+0

'canberre' ==' canberra'? –

答えて

0

[{"country":"india", "city":"city1"},{"country":"england", "city":"city2"},{"country":"australia", "city":"city3"}] 

は、以下の

// map to keep each country and its cities 
final HashMap<String, ArrayList<String>> data = new HashMap<>(); 
// add your json array 
JSONArray array; 
try { 
    array = new JSONArray(""); 
} catch (JSONException e) { 
    e.printStackTrace(); 
    return; 
} 
try { 
    for (int i = 0; i < array.length(); i++) { 
     JSONObject item = array.getJSONObject(i); 

     String country = item.getString("country"); 

     // get the country from the map according to the country in json 
     ArrayList<String> dataList = data.get(country); 
     // if the list is null "the map doesn't have the list", create new one and add it to the map 
     // else add the city to the country list 
     if (dataList == null) { 
      dataList = new ArrayList<>(); 
      data.put(country, dataList); 
     } 
     dataList.add(item.getString("city")); 
    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

final ArrayList<String> citiesList = new ArrayList<>(); 
final ArrayAdapter<String> citiesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, citiesList); 

Spinner countriesSpinner = (Spinner) findViewById(R.id.spCountries); 
// create the countries list to add it to the countries spinner 
final ArrayList<String> countriesList = new ArrayList<>(); 
countriesList.add("india"); 
countriesList.add("england"); 
countriesList.add("australia"); 
countriesSpinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, countriesList)); 
countriesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     // get the selected country from spinner 
     String selectedCountry = countriesList.get(position); 
     // get the cities in the selected country 
     ArrayList<String> cities = data.get(selectedCountry); 

     // clear the list then add new cities 
     citiesList.clear(); 
     for (String city : cities) 
      citiesList.add(city); 
     citiesAdapter.notifyDataSetChanged(); 
    } 

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

    } 
}); 

Spinner citiesSpinner = (Spinner) findViewById(R.id.spCities); 
citiesSpinner.setAdapter(citiesAdapter); 

が、それは私が第一スピナーでインドをクリックすると、JSON出力に示すように