我想把这个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);
};
我失败了,因为我没有打破“{r \ n”分隔符的String
。现在我正在插入
Education\r\nHealth\r\nComputers\r\nFood\r\nRetail\r\nOther
到ArrayList
的一个索引。
那么我该如何拆分呢?子?分裂功能?
答案 0 :(得分: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();
}
}
答案 1 :(得分:0)
知道了,我在我的代码中使用了这个:
String s = map.get(TAG_VALUE);
for (String parts : s.split("\r\n")) {
spinnerArray.add(parts);
}