我在列表视图中显示一些团队成员。计划是让teamId获得最大步数。以下方法演示了我如何实现这一目标。
public void readLeagueMembers(JSONObject response){
JSONArray teamsArray = null;
try {
teamsArray = response.getJSONArray("teams");
for (int i = 0; i < teamsArray.length(); i++) {
leagueMembers = new LeagueMembers();
JSONObject team = teamsArray.getJSONObject(i);
leagueMembers.setTeamId(team.getString("ID"));
leagueMembers.setTeams(team.getString("team_name"));
leagueMembers.setLeaguePoints(team.getString("team_points"));
leagueMembersList.add(leagueMembers);
listView.setAdapter(leagueAdapter);
//String[] team_points = new String[]{leagueMembers.getLeaguePoints()};
teamId = leagueMembersList.get(0).getTeamId();
Log.d("teamId",teamId);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
你可以从那个方法中看到变量teamId(它是静态的),它存储了第一行的teamId,即645.
现在我将此值传递给另一个类CountDownTimer。
public class CounterClass1 extends CountDownTimer {
public CounterClass1(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
String time = days+"d"+ ":" + hours % 24+"h" + ":" + minutes % 60 +"m"+ ":" + seconds % 60+"s";
diffDays.setText("League finishes in: " + time);
}
@Override
public void onFinish() {
diffDays.setText("League Finished");
viewResults.setVisibility(View.VISIBLE);
leagueInvitation.setVisibility(View.GONE);
winnerId = League.teamId;
Log.d("team id",winnerId);
viewResults(leagueId, winnerId, results, username, password);
}
}
如图所示,我从另一个类
中读取了静态变量teamIdwinnerId = League.teamId;
并将其存储到名为winnerId的变量中。不幸的是,winsId从调试器读为null。有什么建议?当然这是一个Java问题而不是Android问题。
感谢。
答案 0 :(得分:0)
首先,你的联盟适应者在哪里。哇你初始化它。第二件事你是在内部设置适配器是错误的。如果你在json数组中有更多的oblect,它会一次又一次地设置adpter。
对于团队ID,您可以创建一个单例类,并在实例变量中设置团队ID。并根据需要获取该ID。这是您需要的单个类的代码。
public class ReferenceWrapper {
private static ReferenceWrapper wrapper;
private int teamId;
private Context context;
private ReferenceWrapper(Context context) {
this.context=context;
}
public static ReferenceWrapper getInstance(Context context) {
if (wrapper == null) {
wrapper = new ReferenceWrapper(context);
}
return wrapper;
}
public int getTeamId(int teamId) {
return teamId;
}
public int setTeamId() {
this.teamId=teamId;
}
}