Pooveshin
Newbie
Hi All
I am trying the pass JSON objects from my main activity to my second activity however Android studio picks up an error and makes it red. How would I be able to get the data to the second activity if someone can help
I am trying to get name, version, released and api to the second activity textViews.
I am trying the pass JSON objects from my main activity to my second activity however Android studio picks up an error and makes it red. How would I be able to get the data to the second activity if someone can help
I am trying to get name, version, released and api to the second activity textViews.
Code:
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
// URL to get Android Version Data JSON
private static String url = "http://codetest.cobi.co.za/androids.json";
ArrayList<HashMap<String, String>> androidversions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
androidversions = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetVersions().execute();
}
private class GetVersions extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray versions = jsonObj.getJSONArray("versions");
// looping through All Versions
for (int i = 0; i < versions.length(); i++) {
JSONObject c = versions.getJSONObject(i);
String name = c.getString("name");
String version = c.getString("version");
String released = c.getString("released");
String api = c.getString("api");
// tmp hash map for single version
HashMap<String, String> Version = new HashMap<>();
// adding each child node to HashMap key => value
Version.put("name", name);
Version.put("version", version);
Version.put("released", released);
Version.put("api", api);
// adding Data to version list
androidversions.add(Version);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, Second_Level.class);
intent.putExtra("json",c.toString());
startActivity(intent);
}
});
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, androidversions,
R.layout.list_item, new String[]{"name", "version",
"released", "api"}, new int[]{R.id.name,
R.id.version, R.id.released, R.id.api});
lv.setAdapter(adapter);
}
}
}