带有listview

时间:2017-03-22 16:53:26

标签: java android arraylist adapter indexoutofboundsexception

我正在尝试处理多个复选框,以便从列表中删除或编辑元素,列表中有自定义适配器及其自定义布局,在此布局中我创建了复选框,但是当我到达时,我得到一个IndexOutOfBounds异常尝试评估一个布尔数组,即使它已被初始化。

public MeasureTableAdapter(Activity context, ArrayList<MeasureEvi> myMeasureEvi) 
{
    super(context, R.layout.adapter_tablamedida_item, myMeasureEvi);
    this.context = context;
    this.myMeasureEvi = myMeasureEvi;
    checked= new boolean[myMeasureEvi.size()]; //this is where I initialize the array
}

这是我在以下地方获得例外的地方:

适配器中的

 public View getView

if (checked[position]) {
        holder.checkBox.setChecked(true);
    } else {
        holder.checkBox.setChecked(false);
    }

登录调试窗口

checked[position]= java.lang.IndexOutOfBoundsException : Invalid array range: 0 to 0

登录android监视器

03-22 17:18:03.121 2024-3372/com.google.android.gms.persistent W/GLSUser: [AppCertManager] IOException while requesting key: 
                                                                      java.io.IOException: Invalid device key response.
                                                                          at evk.a(:com.google.android.gms:274)
                                                                          at evk.a(:com.google.android.gms:4238)
                                                                          at evj.a(:com.google.android.gms:45)
                                                                          at evd.a(:com.google.android.gms:50)
                                                                          at evc.a(:com.google.android.gms:104)
                                                                          at com.google.android.gms.auth.account.be.legacy.AuthCronChimeraService.b(:com.google.android.gms:4049)
                                                                          at ecm.call(:com.google.android.gms:2041)
                                                                          at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                          at llt.run(:com.google.android.gms:450)
                                                                          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                          at lqc.run(:com.google.android.gms:17)
                                                                          at java.lang.Thread.run(Thread.java:761)

我放了一个断点,我可以看到myMeasureEvi的大小不是0,但由于某种原因,检查总是为0

有关此的任何提示吗?如果您需要更多信息,请告诉我

编辑:完整的适配器代码

public class MeasureTableAdapter extends ArrayAdapter<MeasureEvi> {

private final Activity context;
ArrayList<MeasureEvi> myMeasureEvi;
boolean[] checked;

public OnHeadlineSelectedListener mCallback;
public interface OnHeadlineSelectedListener { public void onMeasureInDrawActiom(int position, boolean delete); }
public void setCallback(OnHeadlineSelectedListener mCallback){ this.mCallback = mCallback; }

public MeasureTableAdapter(Activity context, ArrayList<MeasureEvi> myMeasureEvi) {
    super(context, R.layout.adapter_tablamedida_item, myMeasureEvi);

    this.context = context;
    this.myMeasureEvi = myMeasureEvi;
    checked= new boolean[myMeasureEvi.size()];
}

private class ViewHolder {
    TextView txtIndex;
    EditText txtCoordX;
    EditText txtCoordY;
    ImageView imgEvidence;
    TextView txtEvidence;
    TextView txtName;
    TextView txtDescription;
    CheckBox checkBox;
}

@Override
public View getView(final int position, View rowView, ViewGroup parent){
    final ViewHolder holder;

    LayoutInflater inflater = context.getLayoutInflater();

    if(rowView == null) {
        rowView = inflater.inflate(R.layout.adapter_tablamedida_item, null, true);
        holder = new ViewHolder();

        holder.txtIndex =       (TextView)  rowView.findViewById(R.id.TxtIndex);
        holder.txtCoordX =      (EditText)  rowView.findViewById(R.id.TxtCoordX);
        holder.txtCoordY =      (EditText)  rowView.findViewById(R.id.TxtCoordY);
        holder.imgEvidence =   (ImageView) rowView.findViewById(R.id.ImgIcon);
        holder.txtEvidence =    (TextView)  rowView.findViewById(R.id.TxtEvidenciaId);
        holder.txtName =        (TextView)  rowView.findViewById(R.id.TxtCategory);
        holder.txtDescription = (TextView)  rowView.findViewById(R.id.TxtDescription);
        holder.checkBox=        (CheckBox)  rowView.findViewById(R.id.checkBox);

        rowView.setTag(holder);

    } else {
        holder= (ViewHolder) rowView.getTag();
    }

    MeasureEvi currentItem = getItem(position);

    if (currentItem != null) {
        int suma = currentItem.getmOrderIndex()+1;
        Evidence myEvidence = DataIpat.evidencetArray.get(currentItem.geteIndex());

        holder.checkBox.setChecked(false);
        if (holder.txtIndex != null) holder.txtIndex.setText("" + suma );
        if (holder.imgEvidence != null) holder.imgEvidence.setImageDrawable(myEvidence.geteImage().getDrawable());
        if (holder.txtEvidence != null) holder.txtEvidence.setText("" + myEvidence.geteId());
        if (holder.txtName != null) holder.txtName.setText(myEvidence.geteCategory() + " " + (myEvidence.getcIndex() + 1));

        if (holder.txtDescription != null) holder.txtDescription.setText(currentItem.getmDescription() + " - " + currentItem.getmObservation());

        if (holder.txtCoordX != null) {
            holder.txtCoordX.setSelectAllOnFocus(true);
            holder.txtCoordX.setText("" + currentItem.getmCoordenate().x);
            holder.txtCoordX.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus)
                        if(!(setPosition(holder.txtCoordX.getText().toString(), holder.txtCoordY.getText().toString(), position))){
                            PointF coord = DataIpat.measureEviArray.get(position).getmCoordenate();
                            holder.txtCoordX.setText("" + coord.x);
                        }
                }
            });
        }

        if (holder.txtCoordY != null){
            holder.txtCoordY.setSelectAllOnFocus(true);
            holder.txtCoordY.setText("" + currentItem.getmCoordenate().y);
            holder.txtCoordY.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus)
                        if(!(setPosition(holder.txtCoordX.getText().toString(), holder.txtCoordY.getText().toString(), position))){
                            PointF coord = DataIpat.measureEviArray.get(position).getmCoordenate();
                            holder.txtCoordY.setText("" + coord.y);
                        }
                }
            });
        }
    }

    if (checked[position]) {
        holder.checkBox.setChecked(true);
    } else {
        holder.checkBox.setChecked(false);
    }

    holder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(holder.checkBox.isChecked())
                checked[position] = true;
            else
                checked[position] = false;
        }
    });

    return rowView;
}



private boolean setPosition(String coordX, String coordY, int position){
    try {
        float X = Float.parseFloat(coordX);
        float Y = Float.parseFloat(coordY);
        PointF coord = DataIpat.measureEviArray.get(position).getmCoordenate();

        if (X != 0 && Y != 0 ) { // Valido que los x y y sean diferentes de cero
            if(X != coord.x || Y != coord.y) { // valida que el dato alla cambiado
                for(MeasureEvi myMeasure: DataIpat.measureEviArray){  //
                    if (myMeasure.geteIndex() == DataIpat.measureEviArray.get(position).geteIndex()
                    && myMeasure.getmIndex() != DataIpat.measureEviArray.get(position).getmIndex()){
                        if(X == myMeasure.getmCoordenate().x && Y == myMeasure.getmCoordenate().y) {
                            Toast.makeText(getContext(), "Error no se permiten coordenadas iguales.", Toast.LENGTH_SHORT).show();
                            return false;
                        }

                    }
                }
                DataIpat.measureEviArray.get(position).setmCoordenate(new PointF(X, Y));
                mCallback.onMeasureInDrawActiom(position, false); // true for delete
            }
        }
        return true;
    } catch (Exception ex) {
        return false;
    }
}

}

1 个答案:

答案 0 :(得分:0)

通过在我的应用程序上进行更深入的调试,我注意到适配器正在以前的方式进行初始化,因此它不再用于构造函数,除非再次初始化,因此数组的大小将始终为0。

解决方案:

当数组询问自身的大小时,我决定使用ArrayList

创建变量来存储复选框:

public static ArrayList<MeasureEvi> myChecked = new ArrayList<MeasureEvi>();

创建一个监听器事件来存储您的选择:

holder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(holder.checkBox.isChecked()) {
                myChecked.add(DataIpat.measureEviArray.get(position));
            }
            else {
                myChecked.remove(DataIpat.measureEviArray.get(position));
            }
            for (int i=0; i<myChecked.size(); i++){
                MeasureEvi e= myChecked.get(i);
                Log.d("mOrder", String.valueOf(e.getmOrderIndex()));
            }
        }
    });

和KABOOM,现在我收到了我检查的对象!