如何使用MySQL从一个微调器获取值到另一个微调器?

时间:2017-03-13 08:28:18

标签: php android mysql

我使用微调器和MySQL数据库,我的Android XML文件中有两个微调器。我需要从第一个微调器中选择一个项目,然后第二个微调器将自动从数据库中获取相应的id值。

主要Activity.Java

 public class MainActivity extends Activity implementsOnItemSelectedListener {

private Spinner spinnerFood;
private Spinner spinner;

private ArrayList<Category> categoriesList;
private ArrayList<Category> categoriesList1;

private String URL_CATEGORIES = "http://yyyyy/users.php";
private String URL_CAT = "http://yyy/Category.php";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    spinnerFood = (Spinner) findViewById(R.id.spinFood);
    categoriesList = new ArrayList<Category>();
    spinnerFood.setOnItemSelectedListener(this);
    new GetCategories().execute();

    spinner = (Spinner) findViewById(R.id.spinner);
    categoriesList1 = new ArrayList<Category>();
    spinner.setOnItemSelectedListener(this);
    new GetCategories1().execute();
}

private void populateSpinner() {
    List<String> lables = new ArrayList<String>();
    for (int i = 0; i < categoriesList.size(); i++) {
        lables.add(categoriesList.get(i).getCrop());
    }
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);

    // Drop down layout style - list view with radio button
    spinnerAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinnerFood.setAdapter(spinnerAdapter);
}

private void populateSpinner1() {
    List<String> lables1 = new ArrayList<String>();

    for (int i = 0; i < categoriesList1.size(); i++) {
        lables1.add(categoriesList1.get(i).getCrop());
    }

    ArrayAdapter<String> spinnerAdapter1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables1);

    spinnerAdapter   .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(spinnerAdapter1);
    state = spinner.getSelectedItem().toString();
    Log.e(Tag,"spinner "+ state );

}

private class GetCategories extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET);

        Log.e("Response: ", "> " + json);

        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categories = jsonObj
                            .getJSONArray("agriculture");

                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject catObj = (JSONObject) categories.get(i);
                        Category cat = new Category(catObj.getString("state"));
                        categoriesList.add(cat);
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        populateSpinner();
    }

}


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

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

private class GetCategories1 extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser1 = new ServiceHandler();
        String json1 = jsonParser1.makeServiceCall(URL_CAT, ServiceHandler.GET);

        Log.e("Response: ", "> " + json1);

        if (json1 != null) {
            try {
                JSONObject jsonObj1 = new JSONObject(json1);
                if (jsonObj1 != null) {
                    JSONArray categories1 = jsonObj1
                            .getJSONArray("agriculture");

                    for (int i = 0; i < categories1.length(); i++) {
                        JSONObject catObj1 = (JSONObject) categories1.get(i);
                        Category cat1 = new Category(catObj1.getString("category"));
                        categoriesList1.add(cat1);

                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result1) {
        super.onPostExecute(result1);
        populateSpinner1();
    }

Users.php

 $sql = "select id,state from agriculture where id=id"; 
  $result = mysql_query($sql);
   $json = array();

   if(mysql_num_rows($result)){
  while($row=mysql_fetch_assoc($result)){
  $json['agriculture'][]=$row;
  }
  }

 mysql_close($con);
 echo json_encode($json); 

Category.php

  $sql = "select agriculture.id,category.state_id from        agriculture     JOIN category using(state_id) where agriculture.id='id'"; 
 $result = mysql_query($sql);
 $json = array();

if(mysql_num_rows($result)){
 while($row=mysql_fetch_assoc($result)){
   $json['category'][]=$row;
     }
      }

     mysql_close($con);
     echo json_encode($json);

1 个答案:

答案 0 :(得分:0)

你在on onItemSelected()方法中的

就像这样写

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long l)
{
    if (parent.getId() == R.id.spinner)
    {
        //download spinner list item for second spinner from Internet, **you may need position value**
    }
    if (parent.getId() == R.id.spinFood)
    {
        //do something
    }
}