我丢失了已滚出视图的ListView项目的值

时间:2013-08-23 09:04:14

标签: java android listview

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

重要编辑:好的,所以它实际上并没有采用listview的简单点击和轻微滚动来导致这种情况发生。我必须实际上将之前的CHECKBOX滚动到视图之外,以便重置点数。 WTF?

这是一些代码......

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

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

毫无疑问我忘记了什么。如果对我的问题感到困惑,请要求澄清。这让我疯了,让我彻夜不眠。

1 个答案:

答案 0 :(得分:1)

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

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