0

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

重要编辑:好的,所以实际上并不需要简单的单击和列表视图的轻微滚动来导致这种情况发生。我实际上必须将以前的 CHECKBOX 滚动到视野之外,以使积分重置。怎么回事?

这是一些代码...

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

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));   
    }

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

4

1 回答 1

1

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

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

于 2013-08-23T09:27:08.013 回答