2016-03-31 15 views
0

o kadar çok kod geçirdi ve ben yanlış yapıyorum anlamaya çalıştı ama ben sadece ben bir android uygulaması geliştiriyorumbağımlı android iplikçiler

.. time.I dışarı bilemiyorum harcıyorum var ve bir etkinliğe 2 spinner koymamız gerekiyor. ikinci çevirici, ilk çeviricide seçilen girişe göre doldurulacak ve tüm veriler jsondan alınacaktır.

Bir mysql tablosum var ve içinde 2 tane sütun var, yani ülke ve şehir. Ülke ve şehir verilerini sırasıyla 2 iplikçide almayı başardım, şimdi kullanıcı ülke adını seçtiğinde, o ülkenin belirli şehirleri 2. spinnerde listelenmelidir.

MainActivity.java

burada
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(); 
    } 
} 

i cities.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'"); 
    $sql1="select city from ssss"; 

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

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

ülkenin değerlerini ve şehirler

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(); 
    ?>  

almak için 2 php var burada m y json çıktısı şimdi ben bir onitemclicklistener eklemek zorunda ama 2 spinner burada

bağımlı şehirler nasıl bilmiyorum biliyorum

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

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

benim veritabanıdır

Kimlik ülke şehir

1 India Bangalore

1 Hindistan kolkata

1 Hindistan Mumbai

2 İngiltere Londra

2 ingiltere Manchester

2 ingiltere Southampton

3 Australia Canberra

3 Avustralya Sidney

3 Australia Melbourne

+0

yardımcı olur umarım deneyin sizin şehirler aşağıdaki json dizi olduğunu varsayarsak o zaman Bangalore, Kalküta ve mumbai, 2. spinner'da ve benzeri şekilde gösterilmelidir. –

+0

Örneğin mumbai'nın Hindistan'da olduğunu nasıl bilebilirdik? Şehirleri jsonlarına bir ülke anahtarı eklemelisiniz, böylece şehirleri ülkelerine göre filtreleyebilirsiniz. – SaNtoRiaN

+0

'canberre' ==' canberra'? –

cevap

0

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

aşağıdaki

// 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); 

i 1 bükücüsünde india tıklarsanız json çıktıda görüldüğü gibi

İlgili konular