Okay I have went through tons of examples on PARSing my JSON results, with no luck. I have the below JSON example I don\'t want status info or geoLocation right now. I just
First parse your JSON object like so:
String str_json = "your json string";
try {
JSONObject obj = new JSONObject(str_json);
JSONArray stations = obj.getJSONArray("stations");
//etc etc...
} catch (JSONException e) {
e.printStackTrace();
}
Then parse this JSONArray into an ArrayList of custom Station objects that you have created, eg:
public class Station {
public String country;
public int reg_price;
// etc etc...
}
Put items from the JSONArray into your ArrayList:
ArrayList stationsArrList = new ArrayList();
int len = stations.size();
for ( int i = 0; i < len; i++ ){
JSONObject stationObj = stations.getJSONObject(i);
Station station = new Station();
for ( int j = 0; j < stationObj.len(); j++ ){
//add items from stationObj to station
}
stationsArrList.add(station);
}
Then create an adapter (assuming you want more than two pieces of info displayed):
public class StationListAdapter extends BaseAdapter {
private static ArrayList stationArrayList;
private LayoutInflater inflator;
public StationListAdapter(Context context, ArrayList results) {
stationArrayList = results;
inflator = LayoutInflater.from(context);
}
public int getCount() {
if (stationArrayList == null)
return 0;
else
return stationArrayList.size();
}
public Object getItem(int position) {
try {
return stationArrayList.get(position);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflator.inflate(R.layout.list_item_station, null);
holder = new ViewHolder();
holder.country = (TextView) convertView.findViewById(R.id.station_listview_item_one);
holder.reg_price = (TextView) convertView.findViewById(R.id.station_listview_item_two);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.country.setText(stationArrayList.get(position).getCountry());
holder.reg_price.setText( stationArrayList.get(position).getRegPrice());
return convertView;
}
static class ViewHolder {
TextView country;
TextView reg_price;
//etc
}
}
In the adapter, you'll be using a listview xml layout that you will have defined for each of the list rows.
Finally, you get the reference to the list and add the data on the main activity code:
stationList = (ListView) findViewById(R.id.station_list_view);
stationListAdapter = new StationListAdapter(this, stationsArrList);
stationList.setAdapter(stationListAdapter);