【问题标题】:android spinner for loop for json response [closed]用于json响应的循环的android微调器[关闭]
【发布时间】:2016-03-26 01:28:07
【问题描述】:

回复:

   {
id: "155",
name: "Dummy Deal For Shirt",
subtitle: "Dummy Deal For Shirt",
type: "virtual",
description: "",
imageurl: "http:media/catalog/product/cache/1/image/960x600/17f82f742ffe127f42dca9de82fb58b1/0/2/02337_WD9388.jpg",
specialprice_with_symbol: "50.00MMK",
specialprice: "50.0000",
price_with_symbol: "100.00MMK",
price: "100.0000",
discount: 50,
discountprice_with_symbol: "50.00MMK",
discountprice: 50,
symbol: "MMK",
weight: 0,
validto: null,
city: "don't subscribe",
address: "",
site: null,
currenttime: "12/19/15 09:11:56",
todate: "01/04/16 23:59:00",
targetnumber: 5,
Qty: 10,
stockType: 1,
stringcurrenttime: 1450516316,
stringtodate: 1451951940,
producturl: "http://www.hai.com/dummy-deal-for-shirt.html",
purchasedquantity: 0,
has_options: "2",
fashion: "Yes",
options: [
{
option_id: "1214",
product_id: "155",
type: "drop_down",
is_require: "0",
sku: null,
max_characters: null,
file_extension: null,
image_size_x: null,
image_size_y: null,
sort_order: "0",
default_title: "color",
store_title: null,
title: "color",
default_price: null,
default_price_type: null,
store_price: null,
store_price_type: null,
price: null,
price_type: null,
values: [
{
option_type_id: "3290",
option_id: "1214",
sku: "SDW",
sort_order: "0",
default_title: "BLUE",
store_title: null,
title: "BLUE",
default_price: "30.0000",
default_price_type: "fixed",
store_price: null,
store_price_type: null,
price: 80,
price_type: "fixed",
bought: 0,
priceWithSymbol: "80.00MMK"
},
{
option_type_id: "3291",
option_id: "1214",
sku: "ASDX",
sort_order: "0",
default_title: "GREEN",
store_title: null,
title: "GREEN",
default_price: "40.0000",
default_price_type: "fixed",
store_price: null,
store_price_type: null,
price: 90,
price_type: "fixed",
bought: 0,
priceWithSymbol: "90.00MMK"
}
]
},
{
option_id: "1215",
product_id: "155",
type: "drop_down",
is_require: "0",
sku: null,
max_characters: null,
file_extension: null,
image_size_x: null,
image_size_y: null,
sort_order: "0",
default_title: "size",
store_title: null,
title: "size",
default_price: null,
default_price_type: null,
store_price: null,
store_price_type: null,
price: null,
price_type: null,
values1: [
{
option_type_id: "3292",
option_id: "1215",
sku: "tgre",
sort_order: "0",
default_title: "XL",
store_title: null,
title: "XL",
default_price: "456.0000",
default_price_type: "fixed",
store_price: null,
store_price_type: null,
price: 506,
price_type: "fixed",
bought: 0,
priceWithSymbol: "506.00MMK"
},
{
option_type_id: "3293",
option_id: "1215",
sku: "twre",
sort_order: "0",
default_title: "XXL",
store_title: null,
title: "XXL",
default_price: "466.0000",
default_price_type: "fixed",
store_price: null,
store_price_type: null,
price: 516,
price_type: "fixed",
bought: 0,
priceWithSymbol: "516.00MMK"
}
]
}
]

}

需要:我想在第一个微调器中设置 valueone 数组标题,在第二个微调器中设置 valuetwo 数组标题。 输出: 对于第一个微调器,我想加载蓝色和红色。对于第二个微调器,我想加载 X 和 XL。请帮我解决 for 循环。

我的 for 循环:

   try {

            JSONObject jsonobjectone = new JSONObject(result);
            Log.e("IMAGE URL", jsonobjectone.getString("producturl"));
            JSONArray jsonarray = jsonobjectone.getJSONArray("options");

            for (int i = 0; i < jsonarray.length(); i++) {
                jsonobjectone = jsonarray.getJSONObject(i);
                String spinnertitle = jsonobjectone.optString("default_title").toString();
                spinone.setPrompt("Select" + " " + spinnertitle);
                Log.e("spinnertitle", spinnertitle);
                JSONArray json_second = jsonobjectone.getJSONArray("values");
                // JSONArray json_second = new JSONArray("values");

                for (int j = 0; j < json_second.length(); j++) {
                    JSONObject jsonObject = json_second.getJSONObject(j);
                    String title = jsonObject.optString("title").toString();
                    Log.e("option_A", title);
                    colorlist.add(title);
                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_spinner_item, colorlist);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinone.setAdapter(adapter);

                JSONArray json_seconds = jsonobjectone.getJSONArray("values1");
                for (int j = 0; j < json_second.length(); j++) {
                    JSONObject jsonObject = json_seconds.getJSONObject(j);
                    String colortitle = jsonObject.optString("title").toString();
                    Log.e("option_B", colortitle);
                    sizelist.add(colortitle);
                }
                ArrayAdapter<String> adaptertwo = new ArrayAdapter<String>(this,
                        android.R.layout.simple_spinner_item, sizelist);
                adaptertwo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spintwo.setAdapter(adaptertwo);

            }

【问题讨论】:

  • 您面临的问题是什么?
  • 对我来说,将所有值加载到第一个微调器本身 i,e(blue,red,x,xl) 中。但我想要一个微调器中的蓝红色和另一个微调器中的 x,xl。
  • 您可以发布您尝试过的内容吗?
  • 现在我已经发布了我的正确回复。请问你现在能帮我吗?如果你愿意,我会分享我的编码。

标签: android arrays json for-loop android-spinner


【解决方案1】:

这是一个可能的解决方案:

 public class Example extends Activity {

    private ArrayList<Srtring> spinnerValueOne;
    private ArrayList<Srtring> spinnerValueTwo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        spinnerValueOne = new ArrayList<>();
        spinnerValueTwo = new ArrayList<>();

        Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
        Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);

        // get your json response
        String response = ....;
        JSONArray jArray = new JSONArray(response);
        JSONArray valueOneArray = jArray.getJsonArray("valueone");
        JSONArray valueTwoArray = jArray.getJsonArray("valuetwo");

        for(int i=0; i < valueOneArray.length(); ++i){
            JsonObject valueOneObject = valueOneArray.getJsonObject(i);
            spinnerValueOne.add(valueOneObject.getString("title"));
        }

        for(int i=0; i < valueTwoArray.length(); ++i){
            JsonObject valueTwoObject = valueTwoArray.getJsonObject(i);
            spinnerValueTwo.add(valueTwoObject.getString("title"));
        }

        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, spinnerValueOne);


        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, spinnerValueTwo);

        spinner1.setAdapter(adapter1);
        spinner2.setAdapter(adapter2);
    }
}

【讨论】:

  • 嗨,我已经发布了我的回复和我的 for 循环部分。请帮我。提前谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多