Android Open Source - YesNoGame Poll Result Activity






From Project

Back to project page YesNoGame.

License

The source code is released under:

GNU General Public License

If you think the Android project YesNoGame 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 example.swa.yesnogame;
/*ww  w .j  av  a  2 s .  co m*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import example.swa.yesnogame.domain.Vote;
import example.swa.yesnogame.domain.util.VoteComparator;
import example.swa.yesnogame.service.IPollService.IFindVotesByPollListener;

/**
 * An activity to show poll results.
 * 
 * @author Hendrik.Stilke@siemens.com
 * 
 */
public class PollResultActivity extends BaseActivity {

  /**
   * Timespan to request a refresh of the vote result list
   */
  private static final long REFRESH_VOTES_TIMESPAN = 30000; // 30sec

  /**
   * Default adapter on the votes list for the "vote" list view.
   */
  private ArrayAdapter<Vote> voteAdapter;

  private List<Vote> votes = new ArrayList<Vote>();

  private Timer refreshTimer;

  private Runnable checkVotes = new Runnable() {

    @Override
    public void run() {
      updateVoteListView();
    }
  };

  /**
   * Initializes the activity.
   */
  private void init() {
    TextView label = (TextView) findViewById(R.id.result_tvName);
    label.setText(getStringFromIntent(PROP_USERNAME));

    initSetupVoteListView();
    initRegisterButtonListeners();
    initSetupRefreshTimer();
  }

  /**
   * Registers listeners on all buttons.
   */
  private void initRegisterButtonListeners() {
    Button cancelButton = (Button) findViewById(R.id.result_btnCancel);

    cancelButton.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        callMainActivity();
      }
    });
  }

  private void initSetupRefreshTimer() {
    this.refreshTimer = new Timer();
    this.refreshTimer.schedule(new TimerTask() {
      @Override
      public void run() {
        // This method is called directly by the timer
        // and runs in the same thread as the timer.

        // We call the method that will work with the UI
        // through the runOnUiThread method.
        PollResultActivity.this.runOnUiThread(PollResultActivity.this.checkVotes);
      }

    }, REFRESH_VOTES_TIMESPAN, REFRESH_VOTES_TIMESPAN);
  }

  /**
   * Sets up the list of votes.
   */
  private void initSetupVoteListView() {
    ListView voteView = (ListView) findViewById(R.id.result_listViewVotes);

    // set up adapter
    this.voteAdapter = new ArrayAdapter<Vote>(PollResultActivity.this, android.R.layout.simple_list_item_1,
        this.votes);
    voteView.setAdapter(this.voteAdapter);

    // request items (async)
    updateVoteListView();
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_poll_result);

    init();
  }

  /**
   * Updates the vote view. Requests new data and updates on new votes
   * received.
   */
  private void updateVoteListView() {

    Long pollId = getLongFromIntent(PROP_POLLID);

    // make an async call and add a callback listener here
    this.pollService.findVotesByPoll(pollId, new IFindVotesByPollListener() {

      @Override
      public void onVotesFound(List<Vote> votes) {
        // sort
        Collections.sort(votes, new VoteComparator());

        // update data
        PollResultActivity.this.votes.clear();
        PollResultActivity.this.votes.addAll(votes);

        // notify changes via adapter
        PollResultActivity.this.voteAdapter.notifyDataSetChanged();
      }

    });
  }
}




Java Source Code List

example.swa.yesnogame.BaseActivity.java
example.swa.yesnogame.InitPollActivity.java
example.swa.yesnogame.MainActivity.java
example.swa.yesnogame.PollResultActivity.java
example.swa.yesnogame.VoteActivity.java
example.swa.yesnogame.domain.Poll.java
example.swa.yesnogame.domain.User.java
example.swa.yesnogame.domain.Vote.java
example.swa.yesnogame.domain.dto.PollSimpleDto.java
example.swa.yesnogame.domain.dto.UserSimpleDto.java
example.swa.yesnogame.domain.dto.VoteSimpleDto.java
example.swa.yesnogame.domain.simple.BaseObject.java
example.swa.yesnogame.domain.simple.PollSimple.java
example.swa.yesnogame.domain.simple.UserSimple.java
example.swa.yesnogame.domain.simple.VoteSimple.java
example.swa.yesnogame.domain.util.PollComparator.java
example.swa.yesnogame.domain.util.VoteComparator.java
example.swa.yesnogame.service.IPollService.java
example.swa.yesnogame.service.PollServiceBase.java
example.swa.yesnogame.service.PollServiceCloud.java
example.swa.yesnogame.service.PollServiceMock.java
example.swa.yesnogame.service.PollServiceProvider.java
example.swa.yesnogame.service.PollService.java
example.swa.yesnogame.service.util.EntityFormatEnum.java
example.swa.yesnogame.service.util.RequestMethodEnum.java
example.swa.yesnogame.service.util.RequestUrlParams.java
example.swa.yesnogame.service.util.RequestUrlTask.java
example.swa.yesnogame.ui.YesNoArrayAdapter.java