I am developing an app, It reads data from a JSON Object file online. I have successfully gotten the data into the app and reading fine into the listview. How would I implement an OnclickListener for the listview so when a user clicks the heading they are taken to a new activity which then displays all the data for that item in textViews. My code to get the data is below with my layout.
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
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");
//String image = c.getString("image");
// 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);
// 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) {
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"}, new int[]{R.id.name,
R.id.version, R.id.released});
lv.setAdapter(adapter);
}
}
}
Listview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="www.cobi.co.za.cobiinteractive.MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>