android - Separating Numeric values from Text Values -
android - Separating Numeric values from Text Values -
i retrieving info rest api , displays:
"per 78g - calories: 221kcal | fat: 12.65g | carbs: 16.20g | protein: 10.88g"
i have imported items listview , when user clicks item string each numeric value individually below without text. based on illustration above:
string calories = 221; string fat = 12.65; string carbs = 16.20; string protein = 10.88;
i got rid of "per 78g" with:
string sd = food.getstring("food_description"); string[] row = sd.split("-"); tems.add(new item(food.getstring("food_name"), row[1]));
which displays next in each list item.
"calories: 221kcal | fat: 12.65g | carbs: 16.20g | protein: 10.88g"
when user clicks on list item: how separate , eliminate text well? 1 time numeric values individually fine.
@override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { // todo auto-generated method stub string calories = adapter.getitem(arg2).getdescription(); string[] calrow = calories.split("?|?"); toast.maketext(getactivity(), "" + calrow[1], toast.length_long).show(); mcallback.onfoodselected(adapter.getitem(arg2).gettitle(), calrow[1]); }
the api documentation bad , outdated. using because has great information. believes bad practice illustration homecoming json:
{ "foods":{ "food":{ "food_description":"per 342g - calories: 835kcal | fat: 32.28g | carbs: 105.43g | protein: 29.41g", "food_id":"4384", "food_name":"plain french toast", "food_type":"generic", "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/french-toast-plain" }, "max_results":"20", "page_number":"0", "total_results":"228" } }
changing json follows, create job more easier:
{ "foods":{ "food":{ "food_description":{ "per" : "342g", "calories": "835kcal", " fat": "32.28g", "carbs":" 105.43g", " protein": "29.41g" }, "food_id":"4384", "food_name":"plain french toast", "food_type":"generic", "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/french-toast-plain" }, "max_results":"20", "page_number":"0", "total_results":"228" } }
i assuming know how parse it. , in case not able alter json, seek utilize stringtokenizer or utilize string.split().
android text separator textutils
Comments
Post a Comment