我有一個Json數據,我想在我的listView中顯示信息,因此我將它們解析爲ArrayList<String>
,然後添加到適配器中。我遇到了一些關於IndexOutOfBoundsException的錯誤
{
"areas": [
{
"fieldTag": "1",
"areaId": 2,
"areaName": "No.1",
"devices": [
{
"cameraName": "A",
"busyFields": "null",
"freeFields": "No.1,NO,2",
},{
"cameraName": "B"
"busyFields": "null",
"freeFields": "No.3,No.4",
}
]
}
],
"error": 0,
"message": "ok"
}
我用GSON解析它,我做的數據列表在適配器告訴他們,我的代碼是:
if (mDeviceInfo.getError() == 0) {
for (int i = 0; i<mDeviceInfo.getAreas().size();i++){
adapter.addSectionHeaderItem(mDeviceInfo.getAreas().get(i).getAreaName());
for (int k = 0; k<mDeviceInfo.getAreas().get(i).getDevices().size();k++){
adapter.addItem(mDeviceInfo.getAreas().get(i).getDevices().get(k).getCameraName());
mPositionList.add(mDeviceInfo.getAreas().get(i).getDevices().get(k).getPosition());
mFreeFieldsList.add(mDeviceInfo.getAreas().get(i).getDevices().get(k).getFreeFields());
mBusyFieldsList.add(mDeviceInfo.getAreas().get(i).getDevices().get(k).getBusyFields());
}
}
adapter.addMessage(mPosition,mFreeFields,mBusyFields);
}else {
Toast.makeText(this,"get info failed",Toast.LENGTH_SHORT).show();
}
,這是我的代碼爲適配器:
public class CameraListAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<String> mData = new ArrayList<String>();
private ArrayList<String> mPosition = new ArrayList<>();
private ArrayList<String> mFreeFields = new ArrayList<>();
private ArrayList<String> mBusyFields = new ArrayList<>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
private LayoutInflater mInflater;
public CameraListAdapter(Context context) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSectionHeaderItem(final String item) {
mData.add(item);
sectionHeader.add(mData.size() - 1);
notifyDataSetChanged();
}
public void addMessage(ArrayList<String> position,ArrayList<String> freeFields,ArrayList<String> busyFields){
mPosition = position;
mFreeFields = freeFields;
mBusyFields = busyFields;
}
@Override
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean isEnabled(int position) {
return getItemViewType(position) != TYPE_SEPARATOR;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
int rowType = getItemViewType(position);
if (convertView == null) {
viewHolder = new ViewHolder();
switch (rowType) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.cameradetail, null);
viewHolder.mCameraName = (TextView) convertView.findViewById(R.id.cameraName);
Log.d("CameraListAdapter","position "+position);
viewHolder.mCameraName.setText(mData.get(position));
viewHolder.position = (TextView) convertView.findViewById(R.id.cameraposition);
viewHolder.position.setText(mPosition.get(position));
viewHolder.busyFields = (TextView) convertView.findViewById(R.id.busyfields);
viewHolder.busyFields.setText(mBusyFields.get(position));
viewHolder.freeFields = (TextView) convertView.findViewById(R.id.freefields);
viewHolder.freeFields.setText(mFreeFields.get(position));
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.cameratag, null);
viewHolder.mTag = (TextView) convertView.findViewById(R.id.areaName);
viewHolder.mTag.setText(mData.get(position));
break;
}
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
public static class ViewHolder {
public TextView mTag;
public TextView mCameraName;
public TextView position;
public TextView busyFields;
public TextView freeFields;
}
}
但它總是告訴我,
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at .CameraListAdapter.getView(CameraListAdapter.java:106)
它打亂了我2天那麼,有沒有人幫我嗎?謝謝你這麼多
什麼是CameraListAdapter的第106行?另外擴展ArrayAdapter可能比BaseAdapter更有意義 –
異常意味着你試圖使用帶有非現有索引的get(index)來訪問arraylist元素(在你的情況下太大)。一些調試可能會幫助你找出錯誤 – 2016-03-05 16:22:51
@ cricket_007哪一行?你的意思是ViewHolder? – PPTing