特定の要素(と呼ばれる)ExpandableListViewに基づいて子ビューを膨らませるウィジェットを使用しています。これは正常に機能しています。ArrayListq
問題は、すべての子ビューの下に、ビューを追加したいということです。これは非常に単純なはずです。アダプタの方法では、関連するのサイズgetChildrenCount()を追加します。次に、このメソッドでは、次の2つのケースでswitch(position)ステートメントを使用します。1ArrayListgetChildView()
- ArrayList内の各オブジェクトの通常の子ビューを拡張するデフォルトのケース
TextViewケース:-1は、下部に配置される特別なビュー(現時点では)を作成します。
ただし、IndexOutOfBoundsアダプターでエラーが発生します。おそらく、メソッドgetChildrenCount()やgetChildView()メソッドへの変更を適切にコーディングしていないためです。最後の子として特別なビューを膨らませるのではなく、別の配列要素(存在しない)を探しているのではないかと思います。
アダプタのgetChildView()とメソッドのコードは次のとおりです。getChildrenCount()アダプタの完全なコードを確認する必要がある場合はお知らせください。
@Override
public View getChildView(int groupPos, int childPos, boolean arg2, View convertView,
ViewGroup arg4) {
if (convertView == null) {
switch (childPos) {
case -1:
TextView post = new TextView(null);
post.setText("post an answer");
post.setTextColor(Color.BLUE);
convertView = post;
break;
default:
convertView = getLayoutInflater().inflate(R.layout.answerbox, null);
}
TextView ansText = (TextView)convertView.findViewById(R.id.answerText);
TextView ansAuthor = (TextView)convertView.findViewById(R.id.answerAuthor);
TextView ansUV = (TextView)convertView.findViewById(R.id.answerUpvotes);
ansText.setText(q.get(groupPos).answers.get(childPos).text);
ansAuthor.setText(q.get(groupPos).answers.get(childPos).author);
ansUV.setText(Integer.toString(R.id.answerUpvotes));
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return q.get(groupPosition).answers.size() + 1;
}
次のIndexOutOfBounds行でエラーがスローされています。
ansText.setText(q.get(groupPos).answers.get(childPos).text);
更新されたコード(現在機能しています):
@Override
public View getChildView(int groupPos, int childPos, boolean arg2, View convertView,
ViewGroup arg4) {
if (convertView == null){
//switch (childPos){
if (childPos == q.get(groupPos).answers.size()){
convertView = getLayoutInflater().inflate(R.layout.answerbox, null);
TextView ansText = (TextView)convertView.findViewById(R.id.answerText);
TextView ansAuthor = (TextView)convertView.findViewById(R.id.answerAuthor);
TextView ansUV = (TextView)convertView.findViewById(R.id.answerUpvotes);
ansText.setText("POST NEW");
ansUV.setText(Integer.toString(R.id.answerUpvotes));
}
else{
convertView = getLayoutInflater().inflate(R.layout.answerbox, null);
TextView ansText = (TextView)convertView.findViewById(R.id.answerText);
TextView ansAuthor = (TextView)convertView.findViewById(R.id.answerAuthor);
TextView ansUV = (TextView)convertView.findViewById(R.id.answerUpvotes);
ansText.setText(q.get(groupPos).answers.get(childPos).text);
ansAuthor.setText("by " + q.get(groupPos).answers.get(childPos).author);
ansUV.setText(Integer.toString(R.id.answerUpvotes));
}
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return q.get(groupPosition).answers.size()+1;
}