我正在尝试将来自相同字符串键的JSONObjects分开" serving_description" (这就是我认为它被称为)他们自己的列表项。有5" serving_description"但它们都不同。我有它显示所有5合一列表项和编辑文本。图像在底部。如何使用相同的键分隔每个值?在这种情况下,字符串键是" serving_description"和5个值。
活动
public class RecipeListActivity extends Activity {
EditText search, ans;
Button enter;
ListView meow;
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
ans = (EditText) findViewById(R.id.editText2);
enter = (Button) findViewById(R.id.button1);
meow = (ListView) findViewById(R.id.listView1);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listItems);
meow.setAdapter(adapter);
enter.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... arg0) {
search = (EditText) findViewById(R.id.editText1);
String SEARCH = search.getText().toString();
JSONObject food = getFood(SEARCH);
Log.v("FATSEC", "TEST");
String ret = "";
try {
JSONArray foodName = food.getJSONObject("food")
.getJSONObject("servings")
.getJSONArray("serving");
for (int n = 0; n < foodName.length(); n++) {
JSONObject object = foodName.getJSONObject(n);
String shit = object
.getString("serving_description");
Log.v("FATSEC", "" + shit);
ret += shit + "\n\n";
}
} catch (JSONException e) {
e.printStackTrace();
}
return ret;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Displaying the text
ans.setText("Serving Size " + result);
listItems.add("" + result);
adapter.notifyDataSetChanged();
}
}.execute();
}
});
}
JSON
{
"servings":{
"serving":[
{
"vitamin_a":"0",
"calcium":"2",
"serving_description":"1 cup cooked"
},
{
"vitamin_a":"0",
"calcium":"6",
"serving_description":"1 cup, dry, yields"
},
{
"vitamin_a":"0",
"calcium":"1",
"serving_description":"1 oz, dry, yields"
},
{
"vitamin_a":"0",
"calcium":"1",
"serving_description":"1 serving (105 g)"
},
{
"vitamin_a":"0",
"calcium":"1",
"serving_description":"100 g"
}
]
},
"food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/rice-white-cooked-regular",
"food_type":"Generic",
"food_name":"White Rice",
"food_id":"4501"
}
ListView结果图片 我想分隔每个值。
答案 0 :(得分:1)
shit.split(",");
将执行它会返回String
数组的技巧,您可以迭代它。