Java 我正在丢失已从视图中滚出的ListView项的值
我有一个从列表视图中的复选框中收集点的应用程序。复选框与listview的项目一起动态添加。每次单击复选框时,我都会将点数添加到总数中。我这样做的方式很好,只要所有列表项都适合屏幕。当列表长到足以使其滚动时,我会丢失以前在向下滚动列表时检查的值。因此,滚动列表会导致点重置。我很有信心,这与复选框失去焦点和/或点击listview本身获得焦点有关,这会导致点重置 重要编辑:好的,所以实际上不需要简单的点击和轻微的滚动listview就可以做到这一点。实际上,我必须将上一个复选框从视图中滚动出来,以使点复位。WTF 这里有一些代码 下面是我处理复选框的整个自定义适配器:Java 我正在丢失已从视图中滚出的ListView项的值,java,android,listview,Java,Android,Listview,我有一个从列表视图中的复选框中收集点的应用程序。复选框与listview的项目一起动态添加。每次单击复选框时,我都会将点数添加到总数中。我这样做的方式很好,只要所有列表项都适合屏幕。当列表长到足以使其滚动时,我会丢失以前在向下滚动列表时检查的值。因此,滚动列表会导致点重置。我很有信心,这与复选框失去焦点和/或点击listview本身获得焦点有关,这会导致点重置 重要编辑:好的,所以实际上不需要简单的点击和轻微的滚动listview就可以做到这一点。实际上,我必须将上一个复选框从视图中滚动出来,以
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);
}
}
公共类ScoreListAdapter扩展了BaseAdapter{
私有数组列表数据;
上下文c;
ScoringInfo scr;
ScoreListAdapter(ArrayList数据,上下文c){
这个数据=数据;
这个.c=c;
}
public int getCount(){
//TODO自动生成的方法存根
返回data.size();
}
公共对象getItem(int-pos){
//TODO自动生成的方法存根
返回数据。获取(pos);
}
公共长getItemId(int-pos){
//TODO自动生成的方法存根
返回pos;
}
公共视图getView(int pos、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视图v=转换视图;
如果(v==null)
{
LayoutInflater vi=(LayoutInflater)c.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.布局.得分列表,空);
}
TextView子任务视图=(TextView)v.findViewById(R.id.subtask);
TextView maxPointsView=(TextView)v.findViewById(R.id.max_points);
scr=数据获取(pos);
subtaskView.setText(scr.subtask);
maxPointsView.setText(“点:+Integer.toString(scr.maxPoints));
最终复选框=(复选框)v.findViewById(R.id.score_框);
setTag(R.string.subtask_num,scr.subtaskNum);
checkBox.setTag(R.string.score、scr.maxPoints);
checkBox.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
int subNum=Integer.parseInt(checkBox.getTag(R.string.subtask_num).toString());
int score=(整数)checkBox.getTag(R.string.score);
如果(((复选框)v).isChecked()){
score=(整数)checkBox.getTag(R.string.score);
挑战。子任务核心。投入(亚微米,分数);
scr.加总分数(分数);
更新点(scr.getTotalPoints());
}
否则{
if(Challenge.subtaskScores.containsKey(subNum))
挑战。子任务核心。移除(子任务);
scr.加总分数(-分);
更新点(scr.getTotalPoints());
}
}
});
返回v;
}
公共void更新点(整数总计){
TextView scrUpdate=(TextView)((活动)c).findViewById(R.id.curr\u score\u view);
Challenge.totalPoints1=总计;
int grandTotal=Challenge.totalPoints1+Challenge.totalPoints2;
setText(“当前分数:+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));
}
public void createScoringList(){
//生成两个列表:一个用于不允许部分点数的任务,另一个用于
//另一个用于允许部分分数的任务。列表是堆叠的
//这是我唯一能想到的展示的方式
//两种类型的点输入布局。这可能需要
//重新考虑。
ListView得分列表=(ListView)findViewById(R.id.score\u列表);
ListView scoreListPartial=(ListView)findViewById(R.id.score\u list\u partial);
ArrayList objList=新的ArrayList();
ArrayList objListPartial=新的ArrayList();
ScoringInfo scrInfo;
//ScoringInfo对象保存与每个子任务关联的各种字段。
infoView=(TextView)findViewById(R.id.chall\u team\u config\u show);
infoView.setText(chall_name+“(id:+challenge_id+))\nTeam:+team_num+
“\n配置:”+randomConfig);
对于(int i=0;i
毫无疑问,我忘了什么。如果对我的问题有疑问,请要求澄清。这让我抓狂,让我彻夜未眠。你的问题正如@Patrick在第一条评论中所说的那样,你没有保存
Ch