Back to project page SELP2013.
The source code is released under:
License ======= This work is licensed under the BSD 2-clause license as follows. # BSD 2-clause license Copyright (c) 2013, Sky Welch All rights reserved. Redistribution and use in source and ...
If you think the Android project SELP2013 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 uk.co.skywelch.selp2013; //www. j a v a 2 s . c o m import java.util.Locale; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v7.app.ActionBarActivity; import android.text.Editable; import android.text.InputType; import android.text.TextWatcher; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.EditText; import android.widget.ListView; public class CourseListActivity extends ActionBarActivity { public static final String TAG = "CourseListActivity"; private EditText txtSearch; private MenuItem courseFilterChangeButton; private final String[] options = new String[] { "Name / Acronym", "Type (AI/CG/CS/SE)", "Level", "Points", "Year", "Semester", "Lecturer" }; public static int course_filter = -1; private static int new_course_filter = -1; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set the appropriate content view setContentView(R.layout.activity_course_list); // Enable the back button in the action bar getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Set up "long press" functionality for course information ListView courses_list = (ListView) findViewById(R.id.courseListView); courses_list.setAdapter(TimetableActivity.courses_adapter); courses_list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> list, View v, int position, long id) { CourseListAdapter adapter = (CourseListAdapter) list.getAdapter(); Course course = (Course) adapter.getItem(position); if (course != null) { onItemLongClicked(course); return true; } return false; } }); // Set up filtering functionality txtSearch = (EditText) findViewById(R.id.txtCourseSearch); txtSearch.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence cs, int start, int before, int count) { Log.d(TAG, "Requesting filter for CharSequence: " + cs); TimetableActivity.courses_adapter.getFilter().filter(cs.toString()); } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void afterTextChanged(Editable arg0) { } }); // Invalidate course filter for resetting course_filter = -1; SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); int saved_course_filter = preferences.getInt("course_filter", 0); changeFilter(saved_course_filter); Log.d(TAG, "Course filter SharedPreference reported as " + Integer.toString(saved_course_filter)); // Set an appropriate empty view for the courses list View emptyView = findViewById(R.id.emptyView); courses_list.setEmptyView(emptyView); } public void onItemLongClicked(final Course course) { // Double check that we can use the course code passed to this function if (course.acronym.isEmpty()) { Log.e(TAG, "Didn't get a course code from the long click, not showing menu."); return; } // There's only one option available for the course list information dialog // Build it String[] info_options = new String[] { "Course (" + course.acronym + ")" }; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.dialog_info_title); builder.setItems(info_options, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: { Intent i = new Intent(CourseListActivity.this, CourseInformationActivity.class); i.putExtra(CourseInformationActivity.COURSE_CODE, course.acronym); startActivity(i); break; } default: Log.e(TAG, "Unknown information option selected, not doing anything."); break; } } }); builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { Log.d(TAG, "Cancel button clicked, ignoring information options"); } }); builder.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); // Show the course filter change button courseFilterChangeButton = menu.findItem(R.id.action_courses_change_filter); courseFilterChangeButton.setVisible(true); return super.onCreateOptionsMenu(menu); } private void changeFilter(int new_course_filter) { if (course_filter != new_course_filter) { // Reset filter input type hint // Eg: filter by name/acronym needs a text input // filter by year needs a number input String hint = "Filter by " + options[new_course_filter].toLowerCase(Locale.US); if (new_course_filter == 0) { txtSearch.setInputType(InputType.TYPE_TEXT_VARIATION_FILTER); } else if (new_course_filter == 1) { txtSearch.setInputType(InputType.TYPE_TEXT_VARIATION_FILTER); } else if (new_course_filter == 2) { txtSearch.setInputType(InputType.TYPE_CLASS_NUMBER); } else if (new_course_filter == 3) { txtSearch.setInputType(InputType.TYPE_CLASS_NUMBER); } else if (new_course_filter == 4) { txtSearch.setInputType(InputType.TYPE_CLASS_NUMBER); } else if (new_course_filter == 5) { txtSearch.setInputType(InputType.TYPE_CLASS_NUMBER); } else if (new_course_filter == 6) { txtSearch.setInputType(InputType.TYPE_TEXT_VARIATION_FILTER); } txtSearch.setText(""); txtSearch.setHint(hint); } course_filter = new_course_filter; // Save the course filter preference SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = preferences.edit(); editor.putInt("course_filter", course_filter); editor.commit(); Log.d(TAG, "Chose + saved new filter " + Integer.toString(course_filter) + ": " + options[course_filter]); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_home: { Intent i = new Intent(this, TimetableActivity.class); startActivity(i); return true; } case android.R.id.home: { // If the back button is pressed, end this activity finish(); return true; } case R.id.action_courses_change_filter: { new_course_filter = course_filter; // Build and show the course filter selection dialog AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Pick filter"); builder.setSingleChoiceItems(options, course_filter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int tWhich) { new_course_filter = tWhich; Log.d(TAG, "New prospective filter " + Integer.toString(new_course_filter) + ": " + options[new_course_filter]); } }); builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { changeFilter(new_course_filter); } }); builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { Log.d(TAG, "Threw away prospective filter choice " + Integer.toString(new_course_filter) + ": " + options[new_course_filter] + " in favour of " + Integer.toString(course_filter) + ": " + options[course_filter]); } }); builder.show(); return true; } default: return super.onOptionsItemSelected(item); } } }