2013-08-19 47 views
2

我已经使用自定义适配器创建了自定义ListView。我有一个定义每一行的xml文件,每一行都有一个在这个xml文件中定义的复选框。我的应用程序是一个判断应用程序,其中ListView上的每个项目都是一个“任务”,用于计算特定数量的点。这个想法是,如果任务完成,然后法官点击复选框,并将该任务的分数添加到总分中。如何将值与ListView中的每个复选框相关联?

不幸的是,我看不到与复选框关联的值。有没有办法做到这一点?我会发布一些代码,我希望这足以得到我的问题的一般想法。

该行的XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/score_list_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 


    <CheckBox 
     android:id="@+id/score_box" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentRight="true" 
     android:paddingTop="30dp" 
     android:scaleX="2" 
     android:scaleY="2" 
     android:onClick="chkBoxClicked" /> 
    <TextView 
     android:id="@+id/subtask" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@id/score_box" 
     android:paddingRight="30dp" 
     android:textSize="20sp"/> 
    <TextView 
     android:id="@+id/max_points" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/subtask" /> 


</RelativeLayout> 

从创建列表中的活动的方法:

... 
public void createScoringList() { 
    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; 

    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: " + max_points); 
     scrInfo.setSubtask(task_name); 

     if (partial_points_allowed == 1) 
      objListPartial.add(scrInfo); 
     else 
      objList.add(scrInfo); 
    } 
    scoreList.setAdapter(new ScoreListAdapter(objList , this)); 
    scoreListPartial.setAdapter(new ScoreListAdapter2(objListPartial, this)); 

} 

如果需要为清楚起见更多的代码,问,我会提供。我只是不想用大量我认为可能不必要的代码溢出问题。

回答

2

您可以将此值存储在您的模型中(我认为它被称为ScoringInfo),也可以使用setTag("score", value)方法将此值分配给每个复选框,并通过调用getTag("score")来读取它。

您可以像这样在您的适配器类中设置和读取标签。您的适配器应执行OnClickListener并管理ScoringInfo项目列表。

public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     convertView = LayoutInflater.from(this) 
      .inflate(R.layout.<your_layout>, parent, false); 
    } 

    ScoringInfo item = this.getItem(position); 

    CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox_id); 
    checkBox.setTag("score", item.max_points); 
    checkBox.setOnClickListener(this); 
} 

public void onClick(View view) { 
    if (view instanceof CheckBox) { 
     boolean checked = ((CheckBox) view).isChecked(); 
     int score = view.getTag("score"); 
     // do the rest 
    } 
} 
+0

谢谢。我曾考虑将它添加到ScoringInfo对象中,并且set/getTag很好地了解信息,但是如果我不是动态添加复选框,如何设置标记?值是已添加到我的模型中的max_points,但我不知道如何为每个复选框指定它。我会用行的复选框ID创建一个新的CheckBox项目并在那里执行? –

+0

我向答案添加了示例代码。 –

+0

谢谢!这帮助我指出了正确的方向。我必须采取与你的建议有所不同的一些事情是在内部类中实现一个新的OnClickListener,而不是使用“this”。另外,Android/Java强制你在设置标签时使用预编译密钥,所以我必须在strings.xml中创建一个字符串来将密钥标记为“分数”。 –

相关问题