📄 voteresults.java
字号:
package com.itpath.examples.voter.ui;
import com.itpath.examples.voter.*;
import com.itpath.examples.voter.intf.*;
import com.itpath.examples.voter.util.*;
import java.util.*;
import javax.microedition.lcdui.*;
public class VoteResults extends Form implements CommandListener
{
private Command voteCommand = new Command("Vote", Command.SCREEN, 99);
private Command exitCommand = new Command("Exit", Command.EXIT, 98);
private ChoiceGroup _voteResults = null;
private ScreenCallback _screenCallback = null;
public VoteResults(ScreenCallback sc)
{
super("Vote Results");
Ticker ticker = new Ticker("What do you think of this article?");
this.setTicker(ticker);
_screenCallback = sc;
VoteSummary voteSummary = ResourceUtility.getVoteSummary();
initialize(voteSummary);
// add Commands
this.addCommand( voteCommand);
this.addCommand( exitCommand);
// set command listener
this.setCommandListener( this);
}
public void initialize(VoteSummary voteSummary)
{
append( getNumVotesString( voteSummary.getNumVotes() )) ;
append( getAvgVoteString( voteSummary.getAvgVote() ));
append( showVoteResults( voteSummary.getVotes() ));
}
public void update(VoteSummary voteSummary)
{
set(0, getNumVotesString( voteSummary.getNumVotes() )) ;
set(1, getAvgVoteString( voteSummary.getAvgVote() ));
set(2, showVoteResults( voteSummary.getVotes() ));
}
private StringItem getNumVotesString(String num)
{
return new StringItem("Num Votes: ", num);
}
private StringItem getAvgVoteString(String num)
{
return new StringItem("Avg. Vote: ", num);
}
private Item showVoteResults(Vector votes)
{
_voteResults = new ChoiceGroup("Please Vote", ChoiceGroup.EXCLUSIVE);
for ( int i = 0; i < votes.size(); i++)
{
_voteResults.append( votes.elementAt(i).toString(), null );
}
return _voteResults;
}
public void commandAction(Command c, Displayable s)
{
String command = c.getLabel();
if ( command.equals("Exit") )
{
_screenCallback.exit();
}
else if ( command.equals("Vote") )
{
// get the selected item
int selectedIndex = _voteResults.getSelectedIndex();
Vote newVote = new Vote(""+ selectedIndex, null, null);
VoteSummary voteSummary = ResourceUtility.addEntry( newVote);
update( voteSummary );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -