異なるグループの子供向けに 2 つの異なるレイアウトを使用して、消耗品リスト ビューを実装しようとしています。かなりの検索の後、アダプター用の次のコードを思いつきました。アプリケーションを実行して最後のグループ (4 つのグループのうち) を展開しようとすると、次のようにクラッシュします。
ArrayIndexOutOfBoundsException: length=2; index=2
getChildType で条件をチェックするグループに関係なく:
public int getChildType (int groupPosition, int childPosition) {
Log.i(TAG, "getChildType("+groupPosition+","+childPosition+") called");
if (groupPosition == 3)
return 1;
else return 0;
}
助けてくれてありがとう!
アダプターのコード:
public class ExpListAdapter extends BaseExpandableListAdapter {
private String TAG = "ExpListAdapter";
private String arrGroupelements[];
private String arrChildelements[][][];
private Context myContext;
public ExpListAdapter(Context context, String[] groupElements, String[][][] childElements) {
myContext = context;
arrGroupelements = groupElements;
arrChildelements = childElements;
}
public Object getChild(int groupPosition, int childPosition) {
return arrChildelements[groupPosition][childPosition];
}
public int getChildType (int groupPosition, int childPosition) {
Log.i(TAG, "getChildType("+groupPosition+","+childPosition+") called");
if (groupPosition == 3)
return 1;
else return 0;
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView( int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int gp = groupPosition;
int cp = childPosition;
Log.d(TAG, "groupPosition: "+gp);
Log.d(TAG, "childPosition: "+cp);
Log.d(TAG, "isLastChild: "+isLastChild);
if (convertView == null) {
if (getChildType(groupPosition,childPosition)==0)
convertView = inflater.inflate(R.layout.child_row, null);
else
convertView = inflater.inflate(R.layout.child_row_slide_btn, null);
}
if (getChildType(groupPosition,childPosition)==0) {
TextView fieldInfo = (TextView) convertView.findViewById(R.id.fieldInfo);
fieldInfo.setText(arrChildelements[gp][cp][0]);
EditText editField = (EditText) convertView.findViewById(R.id.editField);
editField.setText(arrChildelements[gp][cp][1], null);
editField.addTextChangedListener( new TextWatcher() {
public void afterTextChanged(Editable s) {
// Log.i(TAG,"afterTextChanged called");
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
} else {
CheckBox cb = (CheckBox)convertView.findViewById(R.id.onOff);
cb.setText("konj");
}
return convertView;
}
public int getChildTypeCount (int groupPosition) {
if (groupPosition==3)
return 2;
else
return 1;
}
public int getChildrenCount(int groupPosition) {
return arrChildelements[groupPosition].length;
}
public Object getGroup(int groupPosition) {
return arrGroupelements[groupPosition];
}
public int getGroupCount() {
return arrGroupelements.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.group_row, null);
}
TextView groupTitle = (TextView) convertView.findViewById(R.id.groupTitle);
groupTitle.setText(arrGroupelements[groupPosition]);
return convertView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}