Clean up code
This commit is contained in:
@@ -1,40 +1,30 @@
|
||||
package massiveattendancescannerapplication;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.app.ListActivity;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ListAdapter;
|
||||
import android.widget.ListView;
|
||||
import android.widget.SimpleAdapter;
|
||||
import android.widget.TextView;
|
||||
|
||||
import massiveattendancescannerapplication.Services.JSONParser;
|
||||
import massiveattendancescannerapplication.Services.ServiceHandler;
|
||||
|
||||
public class CourseActivity extends ListActivity {
|
||||
|
||||
private ProgressDialog pDialog;
|
||||
JSONParser jsonParser = new JSONParser();
|
||||
ArrayList<HashMap<String, String>> courseList;
|
||||
JSONArray course = null;
|
||||
private static final String URL = "http://192.168.0.101:3000/courses";
|
||||
// ALL JSON node names
|
||||
private static final String TAG_ID = "_id";
|
||||
private static final String TAG_NAME = "name";
|
||||
private static final String TAG_CODE = "code";
|
||||
@@ -45,40 +35,25 @@ public class CourseActivity extends ListActivity {
|
||||
setContentView(R.layout.course_activity);
|
||||
|
||||
|
||||
courseList = new ArrayList<HashMap<String, String>>();
|
||||
courseList = new ArrayList<>();
|
||||
new LoadCourses().execute();
|
||||
|
||||
ListView lv = getListView();
|
||||
|
||||
/**
|
||||
* Listview item click listener
|
||||
* SectionActivity will be lauched by passing album id
|
||||
* */
|
||||
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
|
||||
long arg3) {
|
||||
// on selecting a single album
|
||||
// SectionActivity will be launched to show tracks inside the album
|
||||
Intent i = new Intent(getApplicationContext(), SectionActivity.class);
|
||||
|
||||
// send album id to tracklist activity to get list of songs under that album
|
||||
String course_id = ((TextView) view.findViewById(R.id.course_id)).getText().toString();
|
||||
i.putExtra("course_id", course_id);
|
||||
|
||||
startActivity(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Background Async Task to Load all Courses by making http request
|
||||
* */
|
||||
class LoadCourses extends AsyncTask<String, String, String> {
|
||||
|
||||
/**
|
||||
* Before starting background thread Show Progress Dialog
|
||||
* */
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
super.onPreExecute();
|
||||
@@ -88,13 +63,8 @@ public class CourseActivity extends ListActivity {
|
||||
pDialog.setCancelable(false);
|
||||
pDialog.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* getting Courses JSON
|
||||
* */
|
||||
protected String doInBackground(String... args) {
|
||||
|
||||
List<NameValuePair> params = new ArrayList<NameValuePair>();
|
||||
ServiceHandler sh = new ServiceHandler();
|
||||
String JSONString = null;
|
||||
try {
|
||||
@@ -106,46 +76,29 @@ public class CourseActivity extends ListActivity {
|
||||
try {
|
||||
course = new JSONArray(JSONString);
|
||||
|
||||
// looping through All courses
|
||||
for (int i = 0; i < course.length(); i++) {
|
||||
JSONObject c = course.getJSONObject(i);
|
||||
|
||||
// Storing each json item values in variable
|
||||
String id = c.getString(TAG_ID);
|
||||
String name = c.getString(TAG_NAME);
|
||||
String code = c.getString(TAG_CODE);
|
||||
|
||||
// creating new HashMap
|
||||
HashMap<String, String> map = new HashMap<String, String>();
|
||||
|
||||
// adding each child node to HashMap key => value
|
||||
map.put(TAG_ID, id);
|
||||
map.put(TAG_NAME, name);
|
||||
map.put(TAG_CODE, code);
|
||||
|
||||
// adding HashList to ArrayList
|
||||
courseList.add(map);
|
||||
}
|
||||
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* After completing background task Dismiss the progress dialog
|
||||
* **/
|
||||
protected void onPostExecute(String file_url) {
|
||||
// dismiss the dialog after getting all courses
|
||||
pDialog.dismiss();
|
||||
// updating UI from Background Thread
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
/**
|
||||
* Updating parsed JSON data into ListView
|
||||
* */
|
||||
ListAdapter adapter = new SimpleAdapter(
|
||||
CourseActivity.this, courseList,
|
||||
R.layout.list_item_courses, new String[] { TAG_ID,
|
||||
|
@@ -19,7 +19,6 @@ public class MainActivity extends ActionBarActivity {
|
||||
setContentView(R.layout.activity_main);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
|
@@ -1,37 +1,28 @@
|
||||
package massiveattendancescannerapplication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.app.ListActivity;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ListAdapter;
|
||||
import android.widget.ListView;
|
||||
import android.widget.SimpleAdapter;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import massiveattendancescannerapplication.Services.JSONParser;
|
||||
import massiveattendancescannerapplication.Services.ServiceHandler;
|
||||
|
||||
public class SectionActivity extends ListActivity {
|
||||
|
||||
private ProgressDialog pDialog;
|
||||
JSONParser jsonParser = new JSONParser();
|
||||
ArrayList<HashMap<String, String>> sectionList;
|
||||
JSONArray courses = null;
|
||||
String course_id, course_name;
|
||||
private static final String URL = "http://192.168.0.101:3000/sections";
|
||||
private static final String TAG_ID = "_id";
|
||||
@@ -50,22 +41,13 @@ public class SectionActivity extends ListActivity {
|
||||
new LoadSections().execute();
|
||||
ListView lv = getListView();
|
||||
|
||||
/**
|
||||
* Listview on item click listener
|
||||
* StudentActivity will be lauched by passing album id, song id
|
||||
* */
|
||||
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
|
||||
long arg3) {
|
||||
// On selecting single track get song information
|
||||
Intent i = new Intent(getApplicationContext(), StudentActivity.class);
|
||||
|
||||
// to get song information
|
||||
// both album id and song is needed
|
||||
String course_id = ((TextView) view.findViewById(R.id.course_id)).getText().toString();
|
||||
String section_id = ((TextView) view.findViewById(R.id.section_id)).getText().toString();
|
||||
|
||||
i.putExtra("course_id", course_id);
|
||||
i.putExtra("section_id", section_id);
|
||||
|
||||
@@ -77,9 +59,6 @@ public class SectionActivity extends ListActivity {
|
||||
|
||||
class LoadSections extends AsyncTask<String, String, String> {
|
||||
|
||||
/**
|
||||
* Before starting background thread Show Progress Dialog
|
||||
* */
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
super.onPreExecute();
|
||||
@@ -92,8 +71,6 @@ public class SectionActivity extends ListActivity {
|
||||
|
||||
|
||||
protected String doInBackground(String... args) {
|
||||
// Building Parameters
|
||||
List<NameValuePair> params = new ArrayList<NameValuePair>();
|
||||
ServiceHandler sh = new ServiceHandler();
|
||||
String JSONString = null;
|
||||
try {
|
||||
@@ -104,7 +81,6 @@ public class SectionActivity extends ListActivity {
|
||||
|
||||
try {
|
||||
JSONArray sections = new JSONArray(JSONString);
|
||||
|
||||
for (int i = 0; i < sections.length(); i++) {
|
||||
JSONObject c = sections.getJSONObject(i);
|
||||
|
||||
@@ -113,38 +89,28 @@ public class SectionActivity extends ListActivity {
|
||||
String name = c.getString(TAG_NAME);
|
||||
String semester = c.getString(TAG_SEMESTER);
|
||||
|
||||
// creating new HashMap
|
||||
HashMap<String, String> map = new HashMap<String, String>();
|
||||
|
||||
HashMap<String, String> map = new HashMap<String, String>();
|
||||
map.put("course_id", course_id);
|
||||
map.put(TAG_ID, section_id);
|
||||
map.put("section_no", section_no + ".");
|
||||
map.put(TAG_NAME, name);
|
||||
map.put(TAG_SEMESTER, semester);
|
||||
|
||||
// adding HashList to ArrayList
|
||||
sectionList.add(map);
|
||||
}
|
||||
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* After completing background task Dismiss the progress dialog
|
||||
* **/
|
||||
protected void onPostExecute(String file_url) {
|
||||
// dismiss the dialog after getting all tracks
|
||||
pDialog.dismiss();
|
||||
// updating UI from Background Thread
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
/**
|
||||
* Updating parsed JSON data into ListView
|
||||
* */
|
||||
|
||||
ListAdapter adapter = new SimpleAdapter(
|
||||
SectionActivity.this, sectionList,
|
||||
R.layout.list_item_sections, new String[] { "course_id", TAG_ID, "track_no",
|
||||
@@ -154,8 +120,6 @@ public class SectionActivity extends ListActivity {
|
||||
setTitle(course_name);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
package massiveattendancescannerapplication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -10,12 +11,9 @@ import android.app.ProgressDialog;
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import android.widget.ListAdapter;
|
||||
import android.widget.ListView;
|
||||
import android.widget.SimpleAdapter;
|
||||
|
||||
import massiveattendancescannerapplication.Services.ServiceHandler;
|
||||
|
||||
|
||||
@@ -41,7 +39,6 @@ public class StudentActivity extends ListActivity {
|
||||
section_id = i.getStringExtra("section_id");
|
||||
studentList = new ArrayList<>();
|
||||
new LoadStudents().execute();
|
||||
|
||||
ListView lv = getListView();
|
||||
}
|
||||
|
||||
@@ -69,8 +66,6 @@ public class StudentActivity extends ListActivity {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Log.d("Section JSON: ", JSONString);
|
||||
|
||||
try {
|
||||
JSONArray sections = new JSONArray(JSONString);
|
||||
|
||||
|
Reference in New Issue
Block a user