动态查看和隐藏活动中的Spinners

时间:2015-03-16 10:27:53

标签: android json android-spinner

我想动态生成Spinners。我想要生成的最大旋转器数量是7.但我想根据我从json响应得到的数据集生成Spinners。

目前,我在下面显示了代码,用于确定要生成多少个微调器。根据以下代码MealDealCatCode包含L1,L2。所以我希望将值设为2并且仅显示7个中的2个Spinners(隐藏剩余的5个Spinners)。

如何才能完成我的任务。任何帮助将不胜感激。

@Override
    public void onTaskCompleted(JSONArray responseJson) {

        try {
            List<String> MainMenuDescription1 = new ArrayList<String>();
            List<String> MainMenuDescription2 = new ArrayList<String>();
            List<String> MainMenuDescription3 = new ArrayList<String>();
            List<String> MainMenuDescription4 = new ArrayList<String>();
            List<String> MainMenuDescription5 = new ArrayList<String>();
            List<String> MainMenuDescription6 = new ArrayList<String>();
            List<String> MainMenuDescription7 = new ArrayList<String>();
            List<String> MealDealCatCode = new ArrayList<String>();
            int uniqueItemIdCount = 0;
            for (int i = 0; i < responseJson.length(); ++i) {
                JSONObject object = responseJson.getJSONObject(i);

                JSONArray subMenuArray = object
                        .getJSONArray("MealDealItemEntity");

                for (int j = 0; j < subMenuArray.length(); ++j) {
                    JSONObject subMenuObject = subMenuArray.getJSONObject(j);

                    Log.i("MealDealCatCode ",
                            subMenuObject.getString("MealDealCatCode"));
                    MealDealCatCode.add(subMenuObject
                            .getString("MealDealCatCode"));
                    MealDealCatCode = Utils
                            .removeDuplicatesFromList(MealDealCatCode);

                    uniqueItemIdCount = MealDealCatCode.size();

                    if (subMenuObject.getString("MealDealCatCode").trim()
                            .equals("L1")) {
                        Log.i("MainMenuDescription",
                                subMenuObject.getString("MainMenuDescription"));
                        MainMenuDescription1.add(subMenuObject
                                .getString("MainMenuDescription"));

                    }
                    if (subMenuObject.getString("MealDealCatCode").trim()
                            .equals("L2")) {

                        MainMenuDescription2.add(subMenuObject
                                .getString("MainMenuDescription"));

                    }
                    if (subMenuObject.getString("MealDealCatCode").trim()
                            .equals("L3")) {

                        MainMenuDescription3.add(subMenuObject
                                .getString("MainMenuDescription"));

                    }
                    if (subMenuObject.getString("MealDealCatCode").trim()
                            .equals("L4")) {

                        MainMenuDescription4.add(subMenuObject
                                .getString("MainMenuDescription"));

                    }
                    if (subMenuObject.getString("MealDealCatCode").trim()
                            .equals("L5")) {

                        MainMenuDescription5.add(subMenuObject
                                .getString("MainMenuDescription"));

                    }
                    if (subMenuObject.getString("MealDealCatCode").trim()
                            .equals("L6")) {

                        MainMenuDescription6.add(subMenuObject
                                .getString("MainMenuDescription"));

                    }
                    if (subMenuObject.getString("MealDealCatCode").trim()
                            .equals("L7")) {

                        MainMenuDescription7.add(subMenuObject
                                .getString("MainMenuDescription"));
                    }
                }

                item1SP = (Spinner) findViewById(R.id.sp_item1);
                item2SP = (Spinner) findViewById(R.id.sp_item2);
                item3SP = (Spinner) findViewById(R.id.sp_item3);
                item4SP = (Spinner) findViewById(R.id.sp_item4);
                item5SP = (Spinner) findViewById(R.id.sp_item5);
                item6SP = (Spinner) findViewById(R.id.sp_item6);
                item7SP = (Spinner) findViewById(R.id.sp_item7);

                item1SP.setVisibility(View.GONE);
                item2SP.setVisibility(View.GONE);
                item3SP.setVisibility(View.GONE);
                item4SP.setVisibility(View.GONE);
                item5SP.setVisibility(View.GONE);
                item6SP.setVisibility(View.GONE);
                item7SP.setVisibility(View.GONE);




                item1SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
                MainMenuDescription1 = Utils
                        .removeDuplicatesFromList(MainMenuDescription1);
                ArrayAdapter<String> dataAdapterOne = new ArrayAdapter<String>(
                        this, android.R.layout.simple_spinner_item,
                        MainMenuDescription1);
                dataAdapterOne
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                item1SP.setAdapter(dataAdapterOne);
                item1SP.setAdapter(new NothingSelectedSpinnerAdapter(
                        dataAdapterOne,
                        R.layout.contact_spinner_row_nothing_selected, this));

                item2SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
                MainMenuDescription2 = Utils
                        .removeDuplicatesFromList(MainMenuDescription2);
                ArrayAdapter<String> dataAdapterTwo = new ArrayAdapter<String>(
                        this, android.R.layout.simple_spinner_item,
                        MainMenuDescription2);
                dataAdapterTwo
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                item2SP.setAdapter(dataAdapterTwo);
                item2SP.setAdapter(new NothingSelectedSpinnerAdapter(
                        dataAdapterTwo,
                        R.layout.contact_spinner_row_nothing_selected, this));

                item3SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
                MainMenuDescription3 = Utils
                        .removeDuplicatesFromList(MainMenuDescription3);
                ArrayAdapter<String> dataAdapterThree = new ArrayAdapter<String>(
                        this, android.R.layout.simple_spinner_item,
                        MainMenuDescription3);
                dataAdapterThree
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                item3SP.setAdapter(dataAdapterThree);
                item3SP.setAdapter(new NothingSelectedSpinnerAdapter(
                        dataAdapterThree,
                        R.layout.contact_spinner_row_nothing_selected, this));

                item4SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
                MainMenuDescription4 = Utils
                        .removeDuplicatesFromList(MainMenuDescription4);
                ArrayAdapter<String> dataAdapterFour = new ArrayAdapter<String>(
                        this, android.R.layout.simple_spinner_item,
                        MainMenuDescription4);
                dataAdapterFour
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                item4SP.setAdapter(dataAdapterFour);
                item4SP.setAdapter(new NothingSelectedSpinnerAdapter(
                        dataAdapterFour,
                        R.layout.contact_spinner_row_nothing_selected, this));

                item5SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
                MainMenuDescription5 = Utils
                        .removeDuplicatesFromList(MainMenuDescription5);
                ArrayAdapter<String> dataAdapterFive = new ArrayAdapter<String>(
                        this, android.R.layout.simple_spinner_item,
                        MainMenuDescription5);
                dataAdapterFive
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                item5SP.setAdapter(dataAdapterFive);
                item5SP.setAdapter(new NothingSelectedSpinnerAdapter(
                        dataAdapterFive,
                        R.layout.contact_spinner_row_nothing_selected, this));

                item6SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
                MainMenuDescription6 = Utils
                        .removeDuplicatesFromList(MainMenuDescription6);
                ArrayAdapter<String> dataAdapterSix = new ArrayAdapter<String>(
                        this, android.R.layout.simple_spinner_item,
                        MainMenuDescription6);
                dataAdapterSix
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                item6SP.setAdapter(dataAdapterSix);
                item6SP.setAdapter(new NothingSelectedSpinnerAdapter(
                        dataAdapterSix,
                        R.layout.contact_spinner_row_nothing_selected, this));

                item7SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
                MainMenuDescription7 = Utils
                        .removeDuplicatesFromList(MainMenuDescription7);
                ArrayAdapter<String> dataAdapterSeven = new ArrayAdapter<String>(
                        this, android.R.layout.simple_spinner_item,
                        MainMenuDescription7);
                dataAdapterSeven
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                item7SP.setAdapter(dataAdapterSeven);
                item7SP.setAdapter(new NothingSelectedSpinnerAdapter(
                        dataAdapterSeven,
                        R.layout.contact_spinner_row_nothing_selected, this));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

1 个答案:

答案 0 :(得分:1)

我能够通过以下答案实现我想要的目标,

                for (int a = 0; a < uniqueItemIdCount; a++) {
                    if (uniqueItemIdCount == 1) {
                        item1SP.setVisibility(View.VISIBLE);
                    }

                    if (uniqueItemIdCount == 2) {
                        item1SP.setVisibility(View.VISIBLE);
                        item2SP.setVisibility(View.VISIBLE);
                    }

                    if (uniqueItemIdCount == 3) {
                        item1SP.setVisibility(View.VISIBLE);
                        item2SP.setVisibility(View.VISIBLE);
                        item3SP.setVisibility(View.VISIBLE);
                    }

                    if (uniqueItemIdCount == 4) {
                        item1SP.setVisibility(View.VISIBLE);
                        item2SP.setVisibility(View.VISIBLE);
                        item3SP.setVisibility(View.VISIBLE);
                        item4SP.setVisibility(View.VISIBLE);
                    }

                    if (uniqueItemIdCount == 5) {
                        item1SP.setVisibility(View.VISIBLE);
                        item2SP.setVisibility(View.VISIBLE);
                        item3SP.setVisibility(View.VISIBLE);
                        item4SP.setVisibility(View.VISIBLE);
                        item5SP.setVisibility(View.VISIBLE);
                    }

                    if (uniqueItemIdCount == 6) {
                        item1SP.setVisibility(View.VISIBLE);
                        item2SP.setVisibility(View.VISIBLE);
                        item3SP.setVisibility(View.VISIBLE);
                        item4SP.setVisibility(View.VISIBLE);
                        item5SP.setVisibility(View.VISIBLE);
                        item6SP.setVisibility(View.VISIBLE);
                    }

                    if (uniqueItemIdCount == 7) {
                        item1SP.setVisibility(View.VISIBLE);
                        item2SP.setVisibility(View.VISIBLE);
                        item3SP.setVisibility(View.VISIBLE);
                        item4SP.setVisibility(View.VISIBLE);
                        item5SP.setVisibility(View.VISIBLE);
                        item6SP.setVisibility(View.VISIBLE);
                        item7SP.setVisibility(View.VISIBLE);
                    }
                }