请帮助!,如何在Android上使用两个微调器,第二个微调器数据由PHP和MySQL的第一个微调器过滤。我已经设置了第二个PHP URL来过滤数据,但是我不知道如何用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){
    }
}

最佳答案

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

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

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

关于php - 如何在Android上使用PHP和MySQL筛选和填充基于第一个微调器的第二个微调器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44042123/

10-12 01:51