【问题标题】:How to filter and populate second spinner based on first spinner with PHP and MySQL on Android如何在 Android 上使用 PHP 和 MySQL 基于第一个微调器过滤和填充第二个微调器
【发布时间】:2017-05-18 17:25:35
【问题描述】:

请帮助!,如何在 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){
    }
}

【问题讨论】:

    标签: php android mysql spinner android-spinner


    【解决方案1】:

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

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

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

    【讨论】:

    • 你能给我看看代码吗?我认为这不会是一个很长的代码...我对 android 编程真的很陌生..
    • 你能把你的代码写在codepen或github中并分享链接吗?
    • 我在这里上传了源代码:drive.google.com/open?id=0B9SYHG43eWD7SlZjbTRQNWdXbDg 我在那里包含了 php 和数据库文件。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2014-10-27
    相关资源
    最近更新 更多