0

質問のリストがあり、各項目に「はい」と「いいえ」のチェックボックスがあります。これは、(リストがたくさんあるため) 抽象クラス、子クラス、および配列アダプターを使用して作成されます。リストを作成する抽象クラスのコードは次のとおりです。

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);
 }

これが質問アダプターです。

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);
}

このビューでは、チェックボックスのステータスを変更できます。この変更はデータベースに反映されるのですが、メインリストに戻ると更新時のみ反映されます。onRestart() をオーバーライドしました。

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

アダプターは質問 ArrayList からデータを取得しているため、再ポーリングし、データが変更されたことをアダプターに通知していますが、これは私の見解を変更しません。ビューを更新すると、すべての現在の状態が更新されます。これは長い質問だと思いますが、何か助けていただければ幸いです。

4

1 に答える 1