gpt4 book ai didi

java - 我正在丢失已滚动到 View 之外的 ListView 项目的值

转载 作者:行者123 更新时间:2023-12-01 14:12:25 28 4
gpt4 key购买 nike

我有一个应用程序可以从 ListView 中的复选框收集点。复选框与 ListView 的项目一起动态添加。每次单击复选框时,我都会将分数添加到总数中。只要所有列表项都适合屏幕,我执行此操作的方式就可以正常工作。当列表变得足够长以使其滚动时,当我向下滚动列表时,我会丢失之前检查过的值。因此,我滚动列表会导致点重置。我非常有信心这与从复选框中失去焦点和/或从单击 ListView 本身获得焦点有关,这会导致点重置。

重要编辑:好的,所以实际上并不需要简单的单击和 ListView 的轻微滚动来导致这种情况发生。我实际上必须将前一个复选框滚动到 View 之外,足以使点重置。搞什么?

这是一些代码...

这是我处理复选框的整个自定义适配器:

public class ScoreListAdapter extends BaseAdapter {

private ArrayList<ScoringInfo> data;
Context c;
ScoringInfo scr;

ScoreListAdapter (ArrayList<ScoringInfo> data, Context c){
this.data = data;
this.c = c;
}

public int getCount() {
// TODO Auto-generated method stub
return data.size();
}

public Object getItem(int pos) {
// TODO Auto-generated method stub
return data.get(pos);
}

public long getItemId(int pos) {
// TODO Auto-generated method stub
return pos;
}

public View getView(int pos, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;

if (v == null)
{
LayoutInflater vi = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.score_list_row, null);
}

TextView subtaskView = (TextView)v.findViewById(R.id.subtask);
TextView maxPointsView = (TextView)v.findViewById(R.id.max_points);

scr = data.get(pos);
subtaskView.setText(scr.subtask);
maxPointsView.setText("Points: " + Integer.toString(scr.maxPoints));

final CheckBox checkBox = (CheckBox) v.findViewById(R.id.score_box);
checkBox.setTag(R.string.subtask_num, scr.subtaskNum);
checkBox.setTag(R.string.score, scr.maxPoints);
checkBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int subNum = Integer.parseInt(checkBox.getTag(R.string.subtask_num).toString());
int score = (Integer) checkBox.getTag(R.string.score);
if (((CheckBox)v).isChecked()) {

score =(Integer) checkBox.getTag(R.string.score);
Challenge.subtaskScores.put(subNum, score);
scr.addToTotalPoints(score);
updatePoints(scr.getTotalPoints());
}
else {
if (Challenge.subtaskScores.containsKey(subNum))
Challenge.subtaskScores.remove(subNum);
scr.addToTotalPoints(-score);
updatePoints(scr.getTotalPoints());
}
}
});
return v;
}

public void updatePoints(int total){
TextView scrUpdate = (TextView) ((Activity)c).findViewById(R.id.curr_score_view);
Challenge.totalPoints1 = total;
int grandTotal = Challenge.totalPoints1 + Challenge.totalPoints2;
scrUpdate.setText("Current Score: " + grandTotal);
}
}

以下是我认为来自 Challenge.class 的相关代码:

public void createScoringList() {
// Builds two lists: one for the tasks that do not allow partial points, and
// another for the tasks that DO allow partial points. The lists are stacked
// on top of each other. This was the only way I could come up with to present
// two types of layouts for the two types of point input. This may need to be
// reconsidered.
ListView scoreList = (ListView) findViewById(R.id.score_list);
ListView scoreListPartial = (ListView) findViewById(R.id.score_list_partial);
ArrayList<ScoringInfo> objList = new ArrayList<ScoringInfo>();
ArrayList<ScoringInfo> objListPartial = new ArrayList<ScoringInfo>();
ScoringInfo scrInfo;
// The ScoringInfo object holds the various fields that are associated with each subtask.

infoView = (TextView) findViewById(R.id.chall_team_config_show);
infoView.setText(chall_name + " (id: " + challenge_id + ")\nTeam: " + team_num +
"\nConfiguration: " + randomConfig);

for (int i = 0; i < subTaskList.size(); i++) {
subtask_num = subTaskList.get(i).subtask_num;
max_points = subTaskList.get(i).max_points;
partial_points_allowed = subTaskList.get(i).partial_points_allowed;
task_name = subTaskList.get(i).task_name;

scrInfo = new ScoringInfo();
scrInfo.setMaxPoints(max_points);
scrInfo.setSubtask(task_name);
scrInfo.setSubtaskNum(subtask_num);

if (partial_points_allowed == 1)
objListPartial.add(scrInfo);
else
objList.add(scrInfo);
}
// There is a custom adapter for both possible lists should the challenge need it.
scoreList.setAdapter(new ScoreListAdapter(objList , this));
scoreListPartial.setAdapter(new ScoreListAdapter2(objListPartial, this));
}

毫无疑问我忘记了什么。如果我的问题有任何疑问,请要求澄清。这让我发疯,让我彻夜难眠。

最佳答案

您的问题如 @Patrick 在第一条评论中所述,您没有保存 CheckBox 状态。您需要将其保存到某个地方,例如 boolean 数组。

然后,当您重新创建 View 时,您将从数组中获取保存的值并选中/取消选中CheckBox

关于java - 我正在丢失已滚动到 View 之外的 ListView 项目的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18398918/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com