简体   繁体   中英

How to add items in spinners with a parent - child relationship?

I have two spinners in my android app. One for selecting a chain and other for selecting stores . I want to add items to both spinners. The items of second spinner is dependent to first spinner, ie items of stores should be dependent on the selected chain item. I have tried for same but items of store are empty. Please help me - here's my code I've tried:

private class ChainDownloadTask extends AsyncTask<Void, Void, ArrayList<Chain>> {
        @Override
        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(SelectStoreActivity.this, null, "Loading Chains...");
        }

        @Override
        protected ArrayList<Chain> doInBackground(Void... arg0) {
            // call web method to get all chains
            CallSoap cs = new CallSoap();
            ArrayList<Chain> chainList = cs.getAllChains();
            return chainList;
        }

        @Override
        protected void onPostExecute(ArrayList<Chain> result) {
            mArrayListChains = result;
            loadChainSpinner();
            new StoresDownloadTask().execute();
        }
    }

    private class StoresDownloadTask extends AsyncTask<Void, Void, ArrayList<Store>> {
        @Override
        protected void onPreExecute() {
            mProgressDialog.setMessage("Loading Stores...");
        }

        @Override
        protected ArrayList<Store> doInBackground(Void... arg0) {
            // call web method to get all chains
            CallSoap cs = new CallSoap();
            ArrayList<Store> chainList = cs.getAllStores();
            return chainList;
        }

        @Override
        protected void onPostExecute(ArrayList<Store> result) {
            mArrayListStores = result;
            loadStoreSpinner(mArrayListChains.get(0).getId());
            mProgressDialog.dismiss();
        }
    }

    private void loadChainSpinner() {
        ArrayList<String> chainNameList = new ArrayList<String>();
        for (Chain temp : mArrayListChains) {
            chainNameList.add(temp.getName());
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(SelectStoreActivity.this, android.R.layout.simple_spinner_item, chainNameList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinnerChain.setAdapter(adapter);
    }

    private void loadStoreSpinner(int chainId) {
        ArrayList<Store> storeList = new ArrayList<Store>();
        ArrayList<String> storeNameList = new ArrayList<String>();
        for (Store store : mArrayListStores) {
            if((""+chainId).equals(store.getChainID())){
                storeList.add(store);
                storeNameList.add(store.getFld_str_Name());
            }
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(SelectStoreActivity.this, android.R.layout.simple_spinner_item, storeNameList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinnerStores.setAdapter(adapter);
    }

在此处输入图片说明 `

Check following code

for (int i = 0; i < Counter; i++) {
        strPromoterNames[i] = promoterDetailsResponse.Sub_promoters.get(i).SubpromoterName;
}


       ArrayAdapter<String> adapterPromoterDetails = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, strPromoterNames);
       adapterPromoterDetails.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       spinnerPromoterDetails.setAdapter(adapterPromoterDetails);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM