传递参数在Android的微调器 [英] Passing a parameter to spinner in android

查看:140
本文介绍了传递参数在Android的微调器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个纺纱和第一装载值,从JSON响应第一微调。然后选择从1微调1微调,然后选择值的值将被发送到一个方法调用另一个方法 getFilteredDescriptions(),它返回一个ArrayList的()称为 resultDescription 。我想将其设置为输入到第2微调。

我分配到第二微调值 resultDescription ,但因为我已经初始化或声明我没有在正确的方式来完成。谁能告诉我该怎么办呢?任何帮助将AP preciated。

  @覆盖
公共无效onTaskCompleted(JSONArray responseJson){    尝试{
        清单<串GT;地壳=新的ArrayList<串GT;();
        最终名单<串GT;说明=新的ArrayList<串GT;();        的for(int i = 0; I< responseJson.length(); ++ I){
            的JSONObject对象= responseJson.getJSONObject(I)            如果((object.getString(MainCategoryID))。等于(1)){
                Log.i(描述,object.getString(描述));
                descriptionHalf.add(object.getString(描述));
                JSONArray subMenuArray =对象
                        .getJSONArray(SubMenuEntity);
                为(中间体J = 0; J&下; subMenuArray.length(); ++ j)条{
                    JSONObject的subMenuObject = subMenuArray
                            .getJSONObject(J);
                    Log.i(壳,subMenuObject.getString(壳));
                    crust.add(subMenuObject.getString(壳));                    Log.i(描述,
                            subMenuObject.getString(描述));
                    description.add(subMenuObject.getString(描述));
                    Log.i(描述,
                            subMenuObject.getString(描述));
                }            }            crustSP =(微调)findViewById(R.id.sp_crust);
            sizeSP =(微调)findViewById(R.id.sp_pizza_size);            地壳= Utils.removeDuplicatesFromList(外壳);
            ArrayAdapter<串GT; dataAdapterCru =新ArrayAdapter<串GT;(
                    对此,android.R.layout.simple_spinner_item,外壳);
            dataAdapterCru
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            crustSP.setAdapter(dataAdapterCru);
            crustSP.setOnItemSelectedListener(新OnItemSelectedListener(){                公共无效onNothingSelected(适配器视图<>为arg0){
                }                @覆盖
                公共无效onItemSelected(适配器视图<>母公司,
                        查看查看,INT位置,长的id){                    字符串crustSelectedItem = crustSP.getSelectedItem()
                            的ToString();                    getFilteredDescriptions(crustSelectedItem,说明);                }
            });            ArrayAdapter<串GT; dataAdapterDes =新ArrayAdapter<串GT;(
                    对此,android.R.layout.simple_spinner_item,resultDescription); // resultDescription不能被解析为一个变量
            dataAdapterDes
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            sizeSP.setAdapter(dataAdapterDes);
            sizeSP.setAdapter(新NothingSelectedSpinnerAdapter(
                    dataAdapterDes,
                    R.layout.contact_spinner_row_nothing_selected,此));
            sizeSP.setOnItemSelectedListener(新OnItemSelectedListener(){                公共无效onNothingSelected(适配器视图<>为arg0){
                }                @覆盖
                公共无效onItemSelected(适配器视图<>母公司,
                        查看查看,INT位置,长的id){                }
            });        }
    }赶上(JSONException E){
        e.printStackTrace();
    }}清单<串GT; getFilteredDescriptions(字符串crustSelectedItem,
        清单<串GT;说明){
    清单<串GT; resultDescription =新的ArrayList<串GT;();
    。crustSelectedItem = crustSP.getSelectedItem()的toString();    如果(说明== NULL || description.isEmpty())
        返回resultDescription;    的for(int i = 0; I< description.size();我++){
        说明= Utils.removeDuplicatesFromList(介绍);
        如果(!description.get(I)。载有(crustSelectedItem))
            继续;        resultDescription.add(description.get(I));    }    返回resultDescription; //发送这个上面dataAdapterDes适配器}


解决方案

您code看起来像下面。

  @覆盖
公共无效onTaskCompleted(JSONArray responseJson){    尝试{
        清单<串GT;地壳=新的ArrayList<串GT;();
        最终名单<串GT;说明=新的ArrayList<串GT;();        的for(int i = 0; I< responseJson.length(); ++ I){
            的JSONObject对象= responseJson.getJSONObject(I)            如果((object.getString(MainCategoryID))。等于(1)){
                Log.i(描述,object.getString(描述));
                descriptionHalf.add(object.getString(描述));
                JSONArray subMenuArray =对象
                        .getJSONArray(SubMenuEntity);
                为(中间体J = 0; J&下; subMenuArray.length(); ++ j)条{
                    JSONObject的subMenuObject = subMenuArray
                            .getJSONObject(J);
                    Log.i(壳,subMenuObject.getString(壳));
                    crust.add(subMenuObject.getString(壳));                    Log.i(描述,
                            subMenuObject.getString(描述));
                    description.add(subMenuObject.getString(描述));
                    Log.i(描述,
                            subMenuObject.getString(描述));
                }            }            crustSP =(微调)findViewById(R.id.sp_crust);
            sizeSP =(微调)findViewById(R.id.sp_pizza_size);            地壳= Utils.removeDuplicatesFromList(外壳);
            ArrayAdapter<串GT; dataAdapterCru =新ArrayAdapter<串GT;(
                    对此,android.R.layout.simple_spinner_item,外壳);
            dataAdapterCru
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            crustSP.setAdapter(dataAdapterCru);
            crustSP.setOnItemSelectedListener(新OnItemSelectedListener(){                公共无效onNothingSelected(适配器视图<>为arg0){
                }                @覆盖
                公共无效onItemSelected(适配器视图<>母公司,
                        查看查看,INT位置,长的id){                    字符串crustSelectedItem = crustSP.getSelectedItem()
                            的ToString();                   清单<串GT; resultDescription = getFilteredDescriptions(crustSelectedItem,说明);
                    ArrayAdapter<串GT; dataAdapterDes =新ArrayAdapter<串GT;(
                            对此,android.R.layout.simple_spinner_item,resultDescription); // resultDescription不能被解析为一个变量
                    dataAdapterDes
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    sizeSP.setAdapter(dataAdapterDes);
                    sizeSP.setAdapter(新NothingSelectedSpinnerAdapter(
                            dataAdapterDes,
                            R.layout.contact_spinner_row_nothing_selected,此));
                }
            });
            sizeSP.setOnItemSelectedListener(新OnItemSelectedListener(){                公共无效onNothingSelected(适配器视图<>为arg0){
                }                @覆盖
                公共无效onItemSelected(适配器视图<>母公司,
                        查看查看,INT位置,长的id){                }
            });        }
    }赶上(JSONException E){
        e.printStackTrace();
    }}

学习一些基本的编码为好。取而代之的这个使用活动/ Class_Name.this

I have 2 spinners and first loading the values to the 1st spinner from the json response. Then select a value from 1st spinner then selected value from 1st spinner will be send to a method another method called getFilteredDescriptions() and it returns a ArrayList() called resultDescription. I want to set it as the input to the 2nd spinner.

I have assign the value resultDescription to the 2nd spinner but I haven't done in correct way because I have initialized or declare. Can anyone tell me how to do it? Any help will be appreciated.

@Override
public void onTaskCompleted(JSONArray responseJson) {

    try {
        List<String> crust = new ArrayList<String>();
        final List<String> description = new ArrayList<String>();

        for (int i = 0; i < responseJson.length(); ++i) {
            JSONObject object = responseJson.getJSONObject(i);

            if ((object.getString("MainCategoryID")).equals("1")) {
                Log.i("Description ", object.getString("Description"));
                descriptionHalf.add(object.getString("Description"));
                JSONArray subMenuArray = object
                        .getJSONArray("SubMenuEntity");
                for (int j = 0; j < subMenuArray.length(); ++j) {
                    JSONObject subMenuObject = subMenuArray
                            .getJSONObject(j);
                    Log.i("Crust", subMenuObject.getString("Crust"));
                    crust.add(subMenuObject.getString("Crust"));

                    Log.i("Description",
                            subMenuObject.getString("Description"));
                    description.add(subMenuObject.getString("Description"));


                    Log.i("Description",
                            subMenuObject.getString("Description"));
                }

            }

            crustSP = (Spinner) findViewById(R.id.sp_crust);
            sizeSP = (Spinner) findViewById(R.id.sp_pizza_size);

            crust = Utils.removeDuplicatesFromList(crust);
            ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
                    this, android.R.layout.simple_spinner_item, crust);
            dataAdapterCru
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            crustSP.setAdapter(dataAdapterCru);
            crustSP.setOnItemSelectedListener(new OnItemSelectedListener() {

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

                @Override
                public void onItemSelected(AdapterView<?> parent,
                        View view, int position, long id) {

                    String crustSelectedItem = crustSP.getSelectedItem()
                            .toString();

                    getFilteredDescriptions(crustSelectedItem, description);

                }
            });

            ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
                    this, android.R.layout.simple_spinner_item, resultDescription); //resultDescription cannot be resolved to a variable
            dataAdapterDes
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            sizeSP.setAdapter(dataAdapterDes);
            sizeSP.setAdapter(new NothingSelectedSpinnerAdapter(
                    dataAdapterDes,
                    R.layout.contact_spinner_row_nothing_selected, this));
            sizeSP.setOnItemSelectedListener(new OnItemSelectedListener() {

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

                @Override
                public void onItemSelected(AdapterView<?> parent,
                        View view, int position, long id) {

                }
            });

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

}

List<String> getFilteredDescriptions(String crustSelectedItem,
        List<String> description) {
    List<String> resultDescription = new ArrayList<String>();
    crustSelectedItem = crustSP.getSelectedItem().toString();

    if (description == null || description.isEmpty())
        return resultDescription;

    for (int i = 0; i < description.size(); i++) {
        description = Utils.removeDuplicatesFromList(description);
        if (!description.get(i).contains(crustSelectedItem))
            continue;

        resultDescription.add(description.get(i));

    }

    return resultDescription; //send this above dataAdapterDes Adapter

}

解决方案

your code will look like below.

 @Override
public void onTaskCompleted(JSONArray responseJson) {

    try {
        List<String> crust = new ArrayList<String>();
        final List<String> description = new ArrayList<String>();

        for (int i = 0; i < responseJson.length(); ++i) {
            JSONObject object = responseJson.getJSONObject(i);

            if ((object.getString("MainCategoryID")).equals("1")) {
                Log.i("Description ", object.getString("Description"));
                descriptionHalf.add(object.getString("Description"));
                JSONArray subMenuArray = object
                        .getJSONArray("SubMenuEntity");
                for (int j = 0; j < subMenuArray.length(); ++j) {
                    JSONObject subMenuObject = subMenuArray
                            .getJSONObject(j);
                    Log.i("Crust", subMenuObject.getString("Crust"));
                    crust.add(subMenuObject.getString("Crust"));

                    Log.i("Description",
                            subMenuObject.getString("Description"));
                    description.add(subMenuObject.getString("Description"));


                    Log.i("Description",
                            subMenuObject.getString("Description"));
                }

            }

            crustSP = (Spinner) findViewById(R.id.sp_crust);
            sizeSP = (Spinner) findViewById(R.id.sp_pizza_size);

            crust = Utils.removeDuplicatesFromList(crust);
            ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
                    this, android.R.layout.simple_spinner_item, crust);
            dataAdapterCru
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            crustSP.setAdapter(dataAdapterCru);
            crustSP.setOnItemSelectedListener(new OnItemSelectedListener() {

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

                @Override
                public void onItemSelected(AdapterView<?> parent,
                        View view, int position, long id) {

                    String crustSelectedItem = crustSP.getSelectedItem()
                            .toString();

                   List<String> resultDescription =  getFilteredDescriptions(crustSelectedItem, description);
                    ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
                            this, android.R.layout.simple_spinner_item, resultDescription); //resultDescription cannot be resolved to a variable
                    dataAdapterDes
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    sizeSP.setAdapter(dataAdapterDes);
                    sizeSP.setAdapter(new NothingSelectedSpinnerAdapter(
                            dataAdapterDes,
                            R.layout.contact_spinner_row_nothing_selected, this));
                }
            });


            sizeSP.setOnItemSelectedListener(new OnItemSelectedListener() {

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

                @Override
                public void onItemSelected(AdapterView<?> parent,
                        View view, int position, long id) {

                }
            });

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

}

learn some basic coding as well. Instead of this use Activity/Class_Name.this

这篇关于传递参数在Android的微调器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆