如何在Android上使用PHP和MySQL基于第一个微调器过滤和填充第二个微调器

时间:2017-05-18 08:12:13

标签: php android mysql spinner android-spinner

请帮助!,如何在Android上使用两个微调器,第二个微调器数据被第一个微调器用PHP& MySQL的。我已经设置了第二个php网址来过滤数据,但我不知道如何用doInBackgroundpopulateSpinneronItemSelected等填充第二个微调器。

public class MainActivity extends Activity implements OnItemSelectedListener {

    private Spinner spinnerFood;
    private Spinner spinnerFood2;
    // array list for spinner adapter
    private ArrayList<Category> categoriesList;
    private ArrayList<Category> categoriesList2;
    ProgressDialog pDialog;
    int kabupaten;
    // API urls
    // Url to get all categories
    private String URL_CATEGORIES = "http://192.168.1.88/android_spinnner/get_categories.php";
    private String URL_CATEGORIES2 = "http://192.168.1.88/android_spinnner/get_kelurahan.php?id_kabupaten=";
    private String URL2 = URL_CATEGORIES2+kabupaten;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinnerFood = (Spinner) findViewById(R.id.spinFood);
        spinnerFood2 = (Spinner) findViewById(R.id.spinner2);
        spinnerFood2.setEnabled(false);
        categoriesList = new ArrayList<Category>();
        categoriesList2 = new ArrayList<Category>();

        // spinner item select listener
        spinnerFood.setOnItemSelectedListener(this);
        spinnerFood2.setOnItemSelectedListener(this);

        new GetCategories().execute();
    }

    /**
     * Adding spinner data
     * */
    private void populateSpinner() {
        List<String> lables = new ArrayList<String>();

        for (int i = 0; i < categoriesList.size(); i++) {
            lables.add(categoriesList.get(i).getName());
        }

        // Creating adapter for spinner
        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);

        // attaching data adapter to spinner
        spinnerFood.setAdapter(spinnerAdapter);
    }

    /**
     * Async task to get all food categories
     * */
    private class GetCategories extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Fetching food categories..");
            pDialog.setCancelable(false);
            pDialog.show();
        }
        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("categories");

                        for (int i = 0; i < categories.length(); i++) {
                            JSONObject catObj = (JSONObject) categories.get(i);
                            Category cat = new Category(catObj.getInt("id"),
                                    catObj.getString("name"));
                            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);
            if (pDialog.isShowing())
                pDialog.dismiss();
            populateSpinner();
        }
    }

        @Override
        public void onItemSelected (AdapterView < ? > parent, View view,int position,
        long id){
        Toast.makeText(
                getApplicationContext(),
                parent.getItemAtPosition(position).toString() + " Selected",
                Toast.LENGTH_LONG).show();
        kabupaten = categoriesList.get(position).getId();
        spinnerFood2.setEnabled(true);

    }

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

1 个答案:

答案 0 :(得分:0)

由于您为同一个类中的两个微调器设置了OnItemSelectedListener,因此我有以下建议。

首先在onItemSelectedListener中,你有一个参数视图。使用它找到Id(view.getId())并找到它是哪个微调器。如果是第一个微调器,则执行另一个AsyncTask来获取第二个微调器的项目。

还有一个建议是, 在异步任务中,不使用void,而是使用该参数发送要为其提取结果的URL,以便您可以重复使用相同的代码来获取两个微调器的详细信息。