질문 목록이 있으며 각 항목에 예 및 아니오 체크 박스가 있습니다. 이것은 추상 클래스 (많은 목록이 있기 때문에), 하위 클래스 및 배열 어댑터를 사용하여 만들어집니다. 여기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)했다. 보기를 새로 고치면 모든 현재 상태가 업데이트됩니다. 나는이 질문이 길다는 것을 알고 있지만 도움이 될 것입니다.