Android Open Source - mobile2-android Discussions Activity






From Project

Back to project page mobile2-android.

License

The source code is released under:

Apache License

If you think the Android project mobile2-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ecollege.android;
/*  www.  java2s.c o  m*/
import java.util.ArrayList;
import java.util.HashMap;

import roboguice.inject.InjectView;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;

import com.ecollege.android.activities.ECollegeListActivity;
import com.ecollege.android.adapter.TopicsAdapter;
import com.ecollege.android.adapter.TopicsAdapter.CourseGroupId;
import com.ecollege.android.adapter.TopicsAdapter.TopicAdapterMode;
import com.ecollege.android.adapter.UberItem;
import com.ecollege.android.adapter.UberItem.UberItemType;
import com.ecollege.android.util.CacheConfiguration;
import com.ecollege.api.ECollegeClient;
import com.ecollege.api.model.Course;
import com.ecollege.api.model.UserDiscussionTopic;
import com.ecollege.api.services.discussions.FetchDiscussionTopicsForCourseIds;
import com.google.inject.Inject;

public class DiscussionsActivity extends ECollegeListActivity {
  
  @Inject ECollegeApplication app;
  @Inject SharedPreferences prefs;
  @InjectView(R.id.reload_button) Button reloadButton;
  @InjectView(R.id.course_dropdown) Spinner courseDropdown;
  
  protected ECollegeClient client;
  private TopicsAdapter topicAdapter;
  LayoutInflater viewInflater;
  private ArrayList<String> courseDropdownTitles;
  private HashMap<String, Course> courseTitleToCourseMap;
  private long selectedCourseId;
  private boolean firstLoadFinished = false;
  
  
    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.discussions);
        client = app.getClient();
        viewInflater = getLayoutInflater();
     
        loadCourseTitles();
        configureControls();

        topicAdapter = new TopicsAdapter(this,TopicAdapterMode.GROUP_BY_COURSE_FILTER_INACTIVE);
        setListAdapter(topicAdapter);
    updateCurrentTopics(false);
    }
    
    @Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {
      if (requestCode == UserTopicActivity.VIEW_TOPIC_REQUEST) {
        updateCurrentTopics(true);
      }
    }
    
  private void loadCourseTitles() {
        courseDropdownTitles = new ArrayList<String>();
        courseTitleToCourseMap = new HashMap<String, Course>();
        courseDropdownTitles.add(getString(R.string.all_courses));
        String courseTitle;
        for (Course course : app.getCurrentCourseList()) {
          courseTitle = Html.fromHtml(course.getTitle()).toString();
          courseDropdownTitles.add(courseTitle);
          courseTitleToCourseMap.put(courseTitle, course);
        }
  }

  private void configureControls() {
        reloadButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        updateCurrentTopics(true);
      }
    });
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, R.layout.transparent_spinner_text_view, courseDropdownTitles);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        courseDropdown.setAdapter(adapter);
    courseDropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
      public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        courseSelected(arg2);
      }

      public void onNothingSelected(AdapterView<?> arg0) { }
    });
  }

  protected void courseSelected(int position) {
    if (firstLoadFinished) {
      if (position == 0) {
        selectedCourseId = 0;
      } else {
        String title = courseDropdownTitles.get(position);
        selectedCourseId = courseTitleToCourseMap.get(title).getId();
      }
      updateCurrentTopics(false);
    }
  }

  private void updateCurrentTopics(boolean reload) {
    topicAdapter.beginLoading();
    CacheConfiguration cacheConfiguration = new CacheConfiguration();
    cacheConfiguration.bypassFileCache = reload;
    cacheConfiguration.bypassResultCache = reload;
    ArrayList<String> courseIds = getSelectedCourseId();
    buildService(new FetchDiscussionTopicsForCourseIds(courseIds))
      .configureCaching(cacheConfiguration)
      .execute();
  }
  
  public void onServiceCallException(FetchDiscussionTopicsForCourseIds service, Exception ex) {
    firstLoadFinished = true;
    topicAdapter.hasError();
  }
  
  public void onServiceCallSuccess(FetchDiscussionTopicsForCourseIds service) {
    firstLoadFinished = true;
    topicAdapter.setLastUpdatedAt(service.getCompletedAt());
    topicAdapter.updateItems(service.getResult());
  }

  private ArrayList<String> getSelectedCourseId() {
    boolean allCoursesSelected = (selectedCourseId == 0);
    ArrayList<String> ids = new ArrayList<String>();
    if (allCoursesSelected) { // all courses selected
      for (Course course : app.getCurrentCourseList()) {
        ids.add(Long.toString(course.getId()));
      }
    } else {
      ids.add(Long.toString(selectedCourseId));
    }
    return ids;
  }
  
    @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    
    @SuppressWarnings("unchecked")
    UberItem<UserDiscussionTopic> item = (UberItem<UserDiscussionTopic>)l.getItemAtPosition(position);
    
    if (item.getItemType() == UberItemType.FOOTER) {
      CourseGroupId groupId = (CourseGroupId) item.getGroupId();
      long courseId = groupId.getCourseId();

      Intent intent = new Intent(this, CourseDiscussionsActivity.class);
      intent.putExtra(CourseDiscussionsActivity.COURSE_ID_EXTRA, courseId);
      startActivity(intent);
    } else {
      UserDiscussionTopic selectedTopic = item.getDataItem();
      Intent intent = new Intent(this, UserTopicActivity.class);
      intent.putExtra(UserTopicActivity.USER_TOPIC_EXTRA, selectedTopic);
      startActivityForResult(intent, UserTopicActivity.VIEW_TOPIC_REQUEST);
    }
  }
    

}




Java Source Code List

com.ecollege.android.AnnouncementActivity.java
com.ecollege.android.CourseActivity.java
com.ecollege.android.CourseAnnouncementsActivity.java
com.ecollege.android.CourseDiscussionsActivity.java
com.ecollege.android.CourseGradebookActivity.java
com.ecollege.android.CoursePeopleActivity.java
com.ecollege.android.CourseThreadActivity.java
com.ecollege.android.CoursesActivity.java
com.ecollege.android.DiscussionsActivity.java
com.ecollege.android.DropboxMessageActivity.java
com.ecollege.android.ECollegeApplication.java
com.ecollege.android.GradeActivity.java
com.ecollege.android.HomeActivity.java
com.ecollege.android.HtmlContentActivity.java
com.ecollege.android.LoginActivity.java
com.ecollege.android.MainActivity.java
com.ecollege.android.PersonActivity.java
com.ecollege.android.ProfileActivity.java
com.ecollege.android.SingleSignonActivity.java
com.ecollege.android.SplashActivity.java
com.ecollege.android.UserDiscussionActivity.java
com.ecollege.android.UserResponseActivity.java
com.ecollege.android.UserTopicActivity.java
com.ecollege.android.activities.ECollegeActivityHelper.java
com.ecollege.android.activities.ECollegeActivity.java
com.ecollege.android.activities.ECollegeDefaultActivity.java
com.ecollege.android.activities.ECollegeListActivity.java
com.ecollege.android.activities.ECollegeTabActivity.java
com.ecollege.android.adapter.ActivityFeedAdapter.java
com.ecollege.android.adapter.GroupedAdapter.java
com.ecollege.android.adapter.LoadMoreAdapter.java
com.ecollege.android.adapter.ParentAdapterObserver.java
com.ecollege.android.adapter.ResponseAdapter.java
com.ecollege.android.adapter.TopicsAdapter.java
com.ecollege.android.adapter.UberAdapter.java
com.ecollege.android.adapter.UberItem.java
com.ecollege.android.adapter.UpcomingEventsAdapter.java
com.ecollege.android.adapter.WaitingForApiAdapter.java
com.ecollege.android.errors.ECollegeAlertException.java
com.ecollege.android.errors.ECollegeException.java
com.ecollege.android.errors.ECollegePromptException.java
com.ecollege.android.errors.ECollegePromptRetryException.java
com.ecollege.android.tasks.ECollegeAsyncTask.java
com.ecollege.android.tasks.ServiceCallTask.java
com.ecollege.android.tasks.TaskPostProcessor.java
com.ecollege.android.util.CacheConfiguration.java
com.ecollege.android.util.DateTimeUtil.java
com.ecollege.android.util.FileCacheManager.java
com.ecollege.android.util.VolatileCacheManager.java
com.ecollege.android.view.HeaderView.java
com.ecollege.android.view.helpers.ResponseCountViewHelper.java