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));
}
如果需要爲清楚起見更多的代碼,問,我會提供。我只是不想用大量我認爲可能不必要的代碼溢出問題。
謝謝。我曾考慮將它添加到ScoringInfo對象中,並且set/getTag很好地瞭解信息,但是如果我不是動態添加複選框,如何設置標記?值是已添加到我的模型中的max_points,但我不知道如何爲每個複選框指定它。我會用行的複選框ID創建一個新的CheckBox項目並在那裏執行? –
我向答案添加了示例代碼。 –
謝謝!這幫助我指出了正確的方向。我必須採取與你的建議有所不同的一些事情是在內部類中實現一個新的OnClickListener,而不是使用「this」。另外,Android/Java強制你在設置標籤時使用預編譯密鑰,所以我必須在strings.xml中創建一個字符串來將密鑰標記爲「分數」。 –