Back to project page RiceCourses.
The source code is released under:
MIT License
If you think the Android project RiceCourses listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package info.kevinlin.ricecourses; //from w ww . j a v a 2 s . c o m import android.app.ActionBar; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.net.Uri; import android.os.AsyncTask; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.support.v7.widget.SearchView; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; import java.io.IOException; import java.util.ArrayList; public class CourseList extends ActionBarActivity { ProgressDialog mProgressDialog; AlertDialog sortDialog; final CharSequence[] items = {"Course code (default)", "Course title"}; Elements courseNumber; Elements courseCode; Elements courseLength; Elements courseTitle; Elements courseInstructor; Elements courseTime; Elements courseCredits; String URL = ""; String sessionCode = ""; int numResults = 0; int sortOption = 0; private boolean isLoaded = false; SearchView search; ListView list; public final static String CLICKED_COURSE_NUMBER = "info.kevinlin.ricecourses.clickedCourseNumber"; public final static String CLICKED_COURSE_CODE = "info.kevinlin.ricecourses.clickedCourseCode"; public final static String CLICKED_COURSE_LENGTH = "info.kevinlin.ricecourses.clickedCourseLength"; public final static String CLICKED_COURSE_TITLE = "info.kevinlin.ricecourses.clickedCourseTitle"; public final static String CLICKED_COURSE_INSTRUCTOR = "info.kevinlin.ricecourses.clickedCourseInstructor"; public final static String CLICKED_COURSE_TIME = "info.kevinlin.ricecourses.clickedCourseTime"; public final static String CLICKED_COURSE_CREDITS = "info.kevinlin.ricecourses.clickedCourseCredits"; public final static String SESSION_CODE = "info.kevinlin.ricecourses.sessionCode"; ArrayList<Course> courses; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_course_list); ActionBar actionBar = getActionBar(); actionBar.setBackgroundDrawable(new ColorDrawable(Color.rgb(11, 35, 69))); Intent intent = getIntent(); URL = intent.getStringExtra(MainActivity.URL); sessionCode = intent.getStringExtra(MainActivity.SESSION_CODE); if (!isLoaded) new Search().execute(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.course_list, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return(true); case R.id.action_sort: sortingDialog(); return true; case R.id.action_esther: openPage(); return true; case R.id.action_refresh: recreateActivity(); return true; default: return super.onOptionsItemSelected(item); } } private void sortingDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Sort by"); builder.setSingleChoiceItems(items, sortOption, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { switch(item) { case 0: sortOption = 0; new Search().execute(); break; case 1: sortOption = 1; new Search().execute(); break; } sortDialog.dismiss(); } }); sortDialog = builder.create(); sortDialog.show(); } private void recreateActivity() { this.recreate(); } private void showToast() { Toast toast = Toast.makeText(getApplicationContext(), "You tapped an action bar button", Toast.LENGTH_LONG); toast.show(); } private class Search extends AsyncTask<Void, Void, Void> { String numCoursesFound = "No courses found that meet the search criteria"; @Override protected void onPreExecute() { super.onPreExecute(); mProgressDialog = new ProgressDialog(CourseList.this); mProgressDialog.setTitle("Loading course list..."); mProgressDialog.setMessage("Retrieving courses from ESTHER..."); mProgressDialog.setIndeterminate(false); mProgressDialog.setCancelable(false); mProgressDialog.setCanceledOnTouchOutside(false); mProgressDialog.show(); } @Override protected Void doInBackground(Void... params) { try { //Retrieve data from server Document doc = Jsoup.connect(URL).get(); courseNumber = doc.select("td.searchSection"); courseCode = doc.select("td.searchCourse"); courseLength = doc.select("td.searchSession"); courseTitle = doc.select("td.searchTitle"); courseInstructor = doc.select("td.searchInstructor"); courseTime = doc.select("td.searchMeeting"); courseCredits = doc.select("td.credits"); numResults = courseNumber.size(); if (numResults > 1) numCoursesFound = "Found " + numResults + " courses matching search criteria"; else if (numResults == 1) numCoursesFound = "Found " + numResults + " course matching search criteria"; else numCoursesFound = "No courses found that meet the search criteria"; //Parse data into ArrayList<Course> courses = new ArrayList<Course>(); for (int i=0; i < numResults; i++) { courses.add(new Course(courseNumber.get(i).text(), courseCode.get(i).text(), courseLength.get(i).text(), courseTitle.get(i).text(), courseInstructor.get(i).text(), courseTime.get(i).text(), courseCredits.get(i).text())); } if (sortOption == 1) java.util.Collections.sort(courses); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { //Report number of courses found Toast toast = Toast.makeText(getApplicationContext(), numCoursesFound, Toast.LENGTH_SHORT); toast.show(); //Tell activity that list is loaded isLoaded = true; //Populate ListView if (numResults > 0) { ArrayAdapter<Course> adapter = new listAdapter(); list = (ListView) findViewById(R.id.courseListView); list.setAdapter(adapter); } //Dismiss loading dialog mProgressDialog.dismiss(); //Handle list clicks list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) { Course clickedCourse = courses.get(position); Intent intent = new Intent(CourseList.this, CourseDetails.class); intent.putExtra(CLICKED_COURSE_NUMBER, clickedCourse.getCourseNumber()); intent.putExtra(CLICKED_COURSE_CODE, clickedCourse.getCourseCode()); intent.putExtra(CLICKED_COURSE_LENGTH, clickedCourse.getCourseLength()); intent.putExtra(CLICKED_COURSE_TITLE, clickedCourse.getCourseTitle()); intent.putExtra(CLICKED_COURSE_INSTRUCTOR, clickedCourse.getCourseInstructor()); intent.putExtra(CLICKED_COURSE_TIME, clickedCourse.getCourseTime()); intent.putExtra(CLICKED_COURSE_CREDITS, clickedCourse.getCredits()); intent.putExtra(SESSION_CODE, sessionCode); startActivity(intent); } }); } } private class listAdapter extends ArrayAdapter<Course> { public listAdapter() { super(CourseList.this, R.layout.item_view, courses); } public View getView(int position, View convertView, ViewGroup parent) { View itemView = convertView; if (itemView == null) itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false); Course currentCourse = courses.get(position); TextView courseCodeText = (TextView)itemView.findViewById(R.id.item_courseCodeTextView); courseCodeText.setText(currentCourse.getCourseCode()); TextView courseTitleText = (TextView)itemView.findViewById(R.id.item_courseTitleTextView); courseTitleText.setText(currentCourse.getCourseTitle()); TextView courseInstructorText = (TextView)itemView.findViewById(R.id.item_courseInstructorTextView); courseInstructorText.setText(currentCourse.getCourseInstructor()); TextView courseTimeText = (TextView)itemView.findViewById(R.id.item_courseTimeTextView); courseTimeText.setText(currentCourse.getCourseTime()); return itemView; } } public void openPage() { Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(URL)); startActivity(browserIntent); } }