如何在android中显示动态微调器值

时间:2015-09-03 10:31:39

标签: android spinner

在下面的代码中,我拿了两个微调器。一个是品牌,另一个是模特。但是数据并没有在微调中显示出来。它没有显示任何错误,但也没有显示下拉列表。

任何人都可以帮助我吗? 代码中的错误是什么?

爪哇

    public class HomeFragment extends Fragment {

    public HomeFragment(){}
    Fragment fragment = null;

    String userId,companyId;
    private String brandid = "3";
    public static List<LeadResult.Users> list;
    public static List<BrandResult.Brands> listBrands;
    public static List<ModelResult.Models> listModels;
    public static ArrayList<String> listBrands_String;
   // public static List<BrandResult.Brands> list1;
    String[] brand_name;
    Spinner spinner1;
    private  RelativeLayout mRel_Ownview,mRel_publicview;
    private  LinearLayout mLin_Stock,mLin_Contact;
    private TextView mTxt_OwnView,mTxt_PublicView;
    private Map<String, String> BrandMap = new HashMap<String, String>();
    private  RangeSeekBar<Integer> seekBar;
    private  RangeSeekBar<Integer> seekBar1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        ActionBar actionBar=getActivity().getActionBar();

        actionBar.setTitle("DEVINE MECHINES");

        SharedPreferences userPreference = getActivity().getSharedPreferences("UserDate", Context.MODE_PRIVATE);

        userId=userPreference.getString("MYID", null);
        companyId=userPreference.getString("companyId",null);


        final View rootView = inflater.inflate(R.layout.layout_ownview, container, false);
        spinner1=(Spinner)rootView.findViewById(R.id.brand1);
        mTxt_OwnView=(TextView) rootView.findViewById(R.id.txt_OwnView);
        mTxt_PublicView =(TextView) rootView.findViewById(R.id.txt_PublicView);
        mRel_Ownview=(RelativeLayout)rootView.findViewById(R.id.ownview);
        mRel_publicview =(RelativeLayout)rootView.findViewById(R.id.publicview);

        listBrands = new ArrayList<BrandResult.Brands>();

        listBrands_String = new ArrayList<String>();
        listModels = new ArrayList<ModelResult.Models>();

        seekBar = new RangeSeekBar<Integer>(5000, 50000,getActivity());
        seekBar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
            @Override
            public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
                // handle changed range values
                //Log.i(TAG, "User selected new range values: MIN=" + minValue + ", MAX=" + maxValue);
                TextView seekMin = (TextView) getView().findViewById(R.id.textSeekMin);
                TextView seekMax = (TextView) getView().findViewById(R.id.textSeekMax);
                seekMin.setText(minValue.toString());
                seekMax.setText(maxValue.toString());
            }
        });

        seekBar1 = new RangeSeekBar<Integer>(5000, 50000,getActivity());
        seekBar1.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
            @Override
            public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
                // handle changed range values
                //Log.i(TAG, "User selected new range values: MIN=" + minValue + ", MAX=" + maxValue);
                TextView seekMin = (TextView) getView().findViewById(R.id.textSeekMin1);
                TextView seekMax = (TextView) getView().findViewById(R.id.textSeekMax1);
                seekMin.setText(minValue.toString());
                seekMax.setText(maxValue.toString());
            }
        });


        mTxt_OwnView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mRel_publicview.setVisibility(View.GONE);
                mTxt_OwnView.setBackgroundColor(getResources().getColor(R.color.light_blue));
                mTxt_PublicView.setBackgroundColor(getResources().getColor(R.color.dark_blue));
                mTxt_OwnView.setTextColor(getResources().getColor(R.color.text_white));
                mTxt_PublicView.setTextColor(getResources().getColor(R.color.light_blue));
                mRel_Ownview.setVisibility(View.VISIBLE);


            }
        });

        mTxt_PublicView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mRel_Ownview.setVisibility(View.GONE);
                mTxt_PublicView.setBackgroundColor(getResources().getColor(R.color.light_blue));
                mTxt_OwnView.setBackgroundColor(getResources().getColor(R.color.dark_blue));
                mTxt_OwnView.setTextColor(getResources().getColor(R.color.light_blue));
                mTxt_PublicView.setTextColor(getResources().getColor(R.color.text_white));
                mRel_publicview.setVisibility(View.VISIBLE);

            }
        });


        String selectedBrandId = BrandMap.get(String.valueOf(spinner1.getSelectedItem()));


      // System.out.print(url);



        mLin_Stock=(LinearLayout)rootView.findViewById(R.id.stock);

        mLin_Stock.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                fragment =new StockFragment();

                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment).commit();


            }
        });
        mLin_Contact =(LinearLayout)rootView.findViewById(R.id.contacts);
        mLin_Contact.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                // getLead();
                fragment = new ContactFragment();

                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment).commit();

                getLead();

            }
        });

// add RangeSeekBar to pre-defined layout

        ViewGroup layout = (ViewGroup) rootView.findViewById(R.id.layout_seek);
        layout.addView(seekBar);

        ViewGroup layout1 = (ViewGroup) rootView.findViewById(R.id.layout_seek1);
        layout1.addView(seekBar1);

        getBrands();
        getModels();

        return rootView;
    }

    private void getBrands() {

       String brandjson = JSONBuilder.getJSONBrand();
        String brandurl = URLBuilder.getBrandUrl();

        Log.d("url", "" + brandurl);
        SendToServerTaskBrand taskBrand = new SendToServerTaskBrand(getActivity());
        taskBrand.execute(brandurl, brandjson);
        //Log.d("brandjson", "" + brandjson);


    }

    private void setBrand(String brandjson)
    {

        ObjectMapper objectMapper_brand = new ObjectMapper();

        try

         {
            BrandResult brandresult_object = objectMapper_brand.readValue(brandjson, BrandResult.class);

            String Brand_result = brandresult_object.getRESULT();
            Log.i("Brand_result","Now" + Brand_result);


            if(Brand_result.equals("SUCCESS"))
            {
                listBrands =brandresult_object.getBRANDS();
                Log.i("listbrands", "List Brands" + listBrands);

               /* for(int i = 0; i < listBrands.size(); i++){

                    listBrands_String.add(listBrands.get(i).toString());
                    Log.d("string is",""+ listBrands_String);
                }*/

                spinner_fn();



                // startActivity(new Intent(getActivity().getApplicationContext(), Contact_Activity.class));

            }

            else
            {
                Toast.makeText(getActivity().getApplicationContext(), "Unable to load data please try again", Toast.LENGTH_SHORT).show();
            }

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

        // return eNames;

    }

    public class SendToServerTaskBrand extends AsyncTask<String, String, String>
    {

        private Context mContext = null;
        private ProgressDialog mProgressDialog;

        public SendToServerTaskBrand(Context context)
        {
            mContext = context;
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            mProgressDialog = ProgressDialog.show(mContext, "", "Loading...");
        }

        @Override
        protected String doInBackground(String... params)
        {
            String Burl = params[0];
            String Bjson = params[1];
            String Bresult = UrlRequester.post(mContext, Burl, Bjson);

            return Bresult;
        }

        @Override
        protected void onPostExecute(String result)
        {
            super.onPostExecute(result);
            setBrand(result);
            Log.i("Result","Brand Result"+result);
            if (mProgressDialog.isShowing())
                mProgressDialog.dismiss();

        }
    }


    private void getModels() {

        String model_url = URLBuilder.getModelUrl();
        String model_json = JSONBuilder.getJSONModel(brandid);

        Log.d("model_json", "" + model_json);
        SendToServerTaskModel taskModel = new SendToServerTaskModel(getActivity());
        taskModel.execute(model_url, model_json);


    }

    private void setModel(String json)
    {

        ObjectMapper objectMapperModel = new ObjectMapper();

        try
        {
            ModelResult modelresult_object = objectMapperModel.readValue(json, ModelResult.class);
            String model_result = modelresult_object.getRESULT();
            Log.d("model_result","" + model_result);


            if (model_result.equals("SUCCESS"))
            {
                listModels =modelresult_object.getMODELS();

                Log.i("listmodels", " " + listModels);


                // startActivity(new Intent(getActivity().getApplicationContext(), Contact_Activity.class));

            }

            else
            {
                Toast.makeText(getActivity().getApplicationContext(), "Unable to load data please try again", Toast.LENGTH_SHORT).show();
            }

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

        // return eNames;

    }

    public class SendToServerTaskModel extends AsyncTask<String, String, String>
    {

        private Context mContext = null;
        private ProgressDialog mProgressDialog;

        public SendToServerTaskModel(Context context)
        {
            mContext = context;
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            mProgressDialog = ProgressDialog.show(mContext, "", "Loading...");
        }

        @Override
        protected String doInBackground(String... params)
        {
            String url = params[0];
            String json = params[1];
            String result = UrlRequester.post(mContext, url, json);

            return result;
        }

        @Override
        protected void onPostExecute(String result)
        {
            super.onPostExecute(result);
            setModel(result);
            if (mProgressDialog.isShowing())
                mProgressDialog.dismiss();

        }
    }


    private void spinner_fn() {


        /*ArrayAdapter<String> dataAdapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(),
                listBrands_String, android.R.layout.simple_spinner_item);*/

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext()
                ,android.R.layout.simple_spinner_item, listBrands_String);

       // ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.category_array, android.R.layout.simple_spinner_item);


        //dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(dataAdapter);

        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int position, long id) {

                Log.e("Position new",""+ listBrands_String.get(position));

            }

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

            }

        });

    }

    private void getLead()
    {

        String url = URLBuilder.getLeadUrl();
        String json = JSONBuilder.getJSONLead(userId, companyId);
        SendToServerTask task = new SendToServerTask(getActivity());
        task.execute(url, json);

    }


    private void setLead(String json)
    {

        ObjectMapper objectMapper = new ObjectMapper();

        try
        {
            LeadResult result_object = objectMapper.readValue(json, LeadResult.class);

            String lead_result = result_object.getRESULT();
            Log.d("lead_result","" + lead_result);


            if (lead_result.equals("SUCCESS"))
            {
                list=result_object.getUSERS();
               // startActivity(new Intent(getActivity().getApplicationContext(), Contact_Activity.class));
            }

            else
            {
                Toast.makeText(getActivity().getApplicationContext(), "Unable to load data please try again", Toast.LENGTH_SHORT).show();
            }

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

        // return eNames;

    }
    public class SendToServerTask extends AsyncTask<String, String, String>
    {

        private Context mContext = null;
        private ProgressDialog mProgressDialog;

        public SendToServerTask(Context context)
        {
            mContext = context;
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            mProgressDialog = ProgressDialog.show(mContext, "", "Loading...");
        }

        @Override
        protected String doInBackground(String... params)
        {
            String url = params[0];
            String json = params[1];
            String result = UrlRequester.post(mContext, url, json);

            return result;
        }

        @Override
        protected void onPostExecute(String result)
        {
            super.onPostExecute(result);
            setLead(result);
            if (mProgressDialog.isShowing())
                mProgressDialog.dismiss();

        }
    }

0 个答案:

没有答案