0

我试图把这个JSON

{
  "display_name": "Type of Business",
  "field_type": "Drop Down Menu",
  "option_value": "Education\r\nHealth\r\nComputers\r\nFood\r\nRetail\r\nOther"
},

进入一个ArrayList然后Spinner用那个填充我的ArrayList。这是代码。

protected void onPostExecute(String string) {
        // dismiss the dialog
        pDialog.dismiss();
        // loop through infoList
        for (int i = 0; i < infoList.size(); i++) {
            // get HashMap
            HashMap<String, String> map = infoList.get(i);

            // Create Spinner
            if (map.get(TAG_FIELD).equals("Drop Down Menu")) {
                Log.v("RESPONSE", "About to create a Drop Down Menu");
                // find
                LinearLayout content = (LinearLayout) view
                        .findViewById(R.id.genA_layout);
                // create
                TextView tv = new TextView(getActivity());
                LinearLayout ll3 = new LinearLayout(getActivity());
                ArrayList<String> spinnerArray = new ArrayList<String>();
                ArrayAdapter<String> aa = new ArrayAdapter<String>(
                        getActivity(),
                        android.R.layout.simple_spinner_dropdown_item,
                        spinnerArray);
                Spinner spinner = new Spinner(getActivity());
                // set
                tv.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));
                spinner.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));
                ll3.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT));
                tv.setText(map.get(TAG_NAME));
                spinnerArray.add(map.get(TAG_VALUE));
                spinner.setAdapter(aa);
                ll3.setOrientation(LinearLayout.HORIZONTAL);
                ll3.setGravity(Gravity.CENTER);
                // add
                ll3.addView(tv);
                ll3.addView(spinner);
                content.addView(ll3);
            }

        }

        // find
        LinearLayout loader = (LinearLayout) view
                .findViewById(R.id.loader_layout);
        Button save = (Button) view
                .findViewById(R.id.generalAssets_save_button_ID);
        // set
        loader.setVisibility(View.GONE);
        save.setVisibility(View.VISIBLE);

    };

我失败了,因为我没有打破String“\r\n”分隔符。现在我正在插入

Education\r\nHealth\r\nComputers\r\nFood\r\nRetail\r\nOther

的一个索引ArrayList

那么我该如何拆分呢?子串?分割功能?

4

2 回答 2

1
    int index=0;
    int index2=0;
    ArrayList<String> strArrayList= new ArrayList<String>();
    while(index!=-1)
    {
        index = str.indexOf("\r\n");
        index2 = index+4;
        if(index!=-1 && index2!=-1)
        {
            strArrayList.add(str.substring(0, index));
            str = str.substring(index2+1, str.length();

        }
    }   
于 2013-06-05T16:42:41.893 回答
0

明白了,我在我的代码中使用了这个:

String s = map.get(TAG_VALUE);
   for (String parts : s.split("\r\n")) {
       spinnerArray.add(parts);       
   }
于 2013-06-05T16:38:40.573 回答