I'm trying to get list from JSON in the fragment on button click. Just when I thought I had all figured out I hit the wall. So if anyone can take a look and if you know whats wrong please explain it to me since I'm android beginner(php and js only).
public class ExampleFragment extends Fragment {
ListView list;
TextView id;
TextView zip;
TextView city;
TextView state;
TextView country;
Button Btngetdata;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
//URL to get JSON Array
private static String url = "http://url.com/web_services/get_cities.php";
//JSON Node Names
private static final String TAG_ARRAY = "cities";
private static final String TAG_ID = "id";
private static final String TAG_CITY = "city_text";
private static final String TAG_ZIP = "zipcode_text";
private static final String TAG_STATE = "state_text";
private static final String TAG_COUNTRY = "country_text";
JSONArray cities = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
oslist = new ArrayList<HashMap<String, String>>();
Btngetdata = (Button)getView().findViewById(R.id.getdata);
Btngetdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new JSONParse().execute();
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
JSONparser jParser = new JSONparser();
private JSONObject json;
@Override
protected void onPreExecute() {
super.onPreExecute();
city = (TextView)getView().findViewById(R.id.city);
state = (TextView)getView().findViewById(R.id.state);
country = (TextView)getView().findViewById(R.id.country);
id = (TextView)getView().findViewById(R.id.id);
zip = (TextView)getView().findViewById(R.id.zip);
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
// Getting JSON from URL
json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array from URL
cities = json.getJSONArray(TAG_ARRAY);
for(int i = 0; i < cities.length(); i++){
JSONObject c = cities.getJSONObject(i);
// Storing JSON item in a Variable
String id = c.getString(TAG_ID);
String zip = c.getString(TAG_ZIP);
String city = c.getString(TAG_CITY);
String state = c.getString(TAG_STATE);
String country = c.getString(TAG_COUNTRY);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_ZIP, zip);
map.put(TAG_CITY, city);
map.put(TAG_STATE, state);
map.put(TAG_COUNTRY, country);
oslist.add(map);
list = (ListView)getView().findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
R.layout.list_item,
new String[] { TAG_CITY, TAG_STATE, TAG_COUNTRY, TAG_ID, TAG_ZIP }, new int[] {
R.id.city, R.id.state, R.id.country, R.id.id, R.id.zip});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
EDIT: Logcat posting