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 に答える