2013-07-10 3 views
0

질문 목록이 있으며 각 항목에 예 및 아니오 체크 박스가 있습니다. 이것은 추상 클래스 (많은 목록이 있기 때문에), 하위 클래스 및 배열 어댑터를 사용하여 만들어집니다. 여기Android - notifyDataSetChanged가 맞춤보기와 작동하지 않습니다.

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    List<Question> questions = getQuestions(1L); 
    setContentView(R.layout.activity_questions); 
    items = (ListView) findViewById(R.id.items); 
    adapter = new QuestionsAdapter(this, getCurrentContext(), questions, 1L, getDbData()); 
    items.setAdapter(adapter); 
} 

QuestionsAdapter된다 :

public View getView(int position, final View convertView, ViewGroup parent) { 
    View row = convertView; 
    if (row == null) { 
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    row = inflater.inflate(R.layout.row_questions, parent, false); 
    holder = new QuestionHolder(); 
    holder.question = (TextView) row.findViewById(R.id.question); 
    holder.yes = (CheckBox) row.findViewById(R.id.yes); 
    holder.no = (CheckBox) row.findViewById(R.id.no); 
    row.setTag(holder); 
    } else { 
    holder = (QuestionHolder) row.getTag(); 
    } 
    Question question = questions.get(position); 
    holder.question.setText(question.getQuestion());  
    setStateCheckboxes(holder, question); 
    holder.yes.setTag(getItem(position)); 
    holder.no.setTag(getItem(position)); 
    holder.yes.setOnCheckedChangeListener(listen); 
    holder.no.setOnCheckedChangeListener(listen); 
    return row; 
} 

내가 체크 박스와 목록보기를 할 수 있도록 홀더를 만들 필요가 여기에 목록을 작성 추상 클래스 코드입니다. 지금까지 모든 것이 잘 작동합니다.

그런 다음 목록의 각 요소에 대한보기가 있습니다. 그것은 매우 기본입니다 :

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    dbData = new DbData(this); 
    this.setContentView(R.layout.single_question); 
    CheckBox yes = (CheckBox) findViewById(R.id.yes_single); 
    CheckBox no = (CheckBox) findViewById(R.id.no_single); 
} 

이보기에서 나는 체크 박스의 상태를 변경할 수 있습니다. 이 변경 사항은 db에 반영되지만 기본 목록으로 돌아 가면 새로 고침에만 반영됩니다.

@Override 
protected void onRestart() { 
    // Change this 
    questions = getQuestions(1L); 
    adapter.notifyDataSetChanged(); 
    super.onRestart(); 
} 

이 어댑터는 질문의 ArrayList에서 데이터를 당기고, 그래서 그것을 repolling과 데이터가 변경 어댑터를 통지하고, 그러나 이것은 내보기를 변경하지 않습니다 나는 onRestart()를 오버라이드 (override)했다. 보기를 새로 고치면 모든 현재 상태가 업데이트됩니다. 나는이 질문이 길다는 것을 알고 있지만 도움이 될 것입니다.

답변

0

onResume() 메서드를 사용하여 notifyDataSetChanged를 호출합니다.

어댑터가 생성되었는지 여부를 관리하려면 코드가 필요합니다. 이 과정에서 너무 일찍 전화하고 싶지 않거나 예외가 발생할 위험이 있습니다.

일반적으로 어댑터를 초기화하고 listview에 추가하는 방법을 통해이 작업을 수행합니다.이 작업은 처음으로 활동/조각이 시작되었는지 또는 다시 돌아 오는 지 여부와 상관없이이를 호출하는 것이 더 안전합니다 다른 활동 (예 : 뒤로 단추를 통해), 어댑터 재 작성 또는 목록보기의 기존 어댑터 교체를 피할 수 있습니다.

또한 코드 앞에 super.onRestart()를 호출하는 것이 좋습니다.

관련 문제