I am creating an app for my school students, where students will log in with their enrollment, the subjects will be placed in a listview and when the student clicks on a lisview item will be shown the grades in another activity. Note: Disciplines will be filled in the listview by json.
protected Void doInBackground (Void ... arg0) { HttpHandler httpHandler = new HttpHandler ();
// request to json data url and getting response
String Jsonurl = "http://192.168.0.12/ProjetoNewWebService/Dados.php?codAlu="+code;
String jsonString = httpHandler.makeServiceCall(Jsonurl);
Log.e(TAG, "Response from url: " + jsonString);
if (jsonString != null) {
try {
JSONObject jsonObject = new JSONObject(jsonString);
// Getting JSON Array node
JSONArray contacts = jsonObject.getJSONArray("dados");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
Aluno = c.getString("nomeAlu");
String nomeDisc = c.getString("nomeDisc");
String nomeProf = c.getString("nomeProf");
String emailProf = c.getString("emailProf");
String nota1 = c.getString("nota1");
String nota2 = c.getString("nota2");
// Phone
//JSONObject phone = c.getJSONObject("phone");
//String mobile = phone.getString("mobile");
//String home = phone.getString("home");
//String office = phone.getString("office");
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("nomeAlu", Aluno);
contact.put("name", nomeDisc);
contact.put("professor", nomeProf);
contact.put("email", emailProf);
contact.put("nota1", nota1);
contact.put("nota2", nota2);
// adding contact to contact list
contactJsonList.add(contact);
}
} 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, "Could not get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Could not get json from server.",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (progressDialog.isShowing())
progressDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactJsonList,
R.layout.list_item, new String[]{"name", "professor",
"email","nota1","nota2"}, new int[]{R.id.name,
R.id.professor, R.id.email, R.id.nota1, R.id.nota2});
listView.setAdapter(adapter);
txtAluno.setText(Aluno);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
}
});
}