2013-08-19 1 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

이 값을 모델에 저장하거나 (스코 링 정보라고 생각 함)이 값을 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 객체에 추가하는 방법에 대한 생각, 그리고 세트/GetTag의는 정보를 알고 좋은,하지만 동적으로 체크 박스를 추가하고 있지 않다 경우, 어떻게 태그를 설정합니까? 그리고이 값은 이미 모델에 추가 된 max_points이지만 각 확인란마다 지정하는 방법을 모르겠습니다. 행의 확인란 ID로 새 CheckBox 항목을 만들고 거기에서 수행합니까? –

+0

답변에 예제 코드를 추가했습니다. –

+0

감사합니다. 이것은 올바른 방향으로 나를 도왔습니다. 제안과 다르게해야했던 몇 가지 사항은 "this"를 사용하는 대신 내부 클래스에 새 OnClickListener를 구현하는 것이 었습니다. 또한, 태그를 설정할 때 안드로이드/자바 힘 당신은 미리 컴파일 된 키를 사용하는, 그래서 "점수"로 키 레이블을 strings.xml의 문자열을 만들어야했습니다. –

관련 문제