簡體   English   中英

使用ArrayAdapter(Java Android)返回ListView時,獲取IndexOutOfBoundsException:索引:1,大小:0

[英]Get IndexOutOfBoundsException: Index: 1, Size: 0 when going back to ListView using ArrayAdapter (Java Android)

我有以下自定義Adapater(陣列適配器),它會將我發送到Fragment,以僅向所選服務添加描述。 但是,當我返回呈現此listView的片段時,出現以下錯誤。

賠償服務適配器

public class ReparationServicesAdapter extends ArrayAdapter<Service> {

ViewGroup parent;

public ReparationServicesAdapter(@NonNull Context context, List<Service> repServicesList) {
        super(context, 0, repServicesList);
    }

public ViewGroup getParent() {
         return parent;
    }

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    View listItemView = convertView;
    RepServicesViewHolder holder;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.reparation_service_item, parent, false);
        holder = new RepServicesViewHolder();
        holder.name = (TextView) listItemView.findViewById(R.id.service_name);
        holder.price = (TextView) listItemView.findViewById(R.id.service_price);
        holder.menuBtn = (ImageButton) listItemView.findViewById(R.id.menu_btn);
        holder.checked = (Switch) listItemView.findViewById(R.id.maintenance_service_switch);
        listItemView.setTag(holder);
    } else {
        holder = (RepServicesViewHolder) listItemView.getTag();
        holder.name.setText("");
        holder.price.setText("");
        holder.menuBtn.setOnClickListener(null);
        holder.checked.setOnCheckedChangeListener(null);
        holder.checked.setChecked(false);
    }

    Log.i("Count", String.valueOf(getCount()));

    final Service currentRepService = getItem(position);

    holder.name.setText(currentRepService.getName() + " - ");

    holder.price.setText("$" + String.valueOf(currentRepService.getPrice()));

    this.parent = parent;

    holder.menuBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((DashboardActivity) getContext()).getAppointmentHelper().getRepServices().add(currentRepService);
            ((DashboardActivity) getContext()).setWorkService(new Service(
                    currentRepService.getCode(),
                    currentRepService.getName(),
                    currentRepService.getPrice()));
            ((DashboardActivity) getContext()).setView("A_REPARATION_SERVICES_DESCRIPTION");
        }
    });

    final TextView repServicesAddedLabel = (TextView) ((ViewGroup) parent.getParent()).findViewById(R.id.bottom_available_services_label);
    final TextView repServicesAddedPrice = (TextView) ((ViewGroup) parent.getParent()).findViewById(R.id.bottom_available_services_price);

    holder.checked.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!isChecked) {
                int size = ((DashboardActivity) getContext()).getAppointmentHelper().getRepServices().size();
                for (int i = 0; i < size; i++) {
                    if (((Service) ((DashboardActivity) getContext()).getAppointmentHelper().getRepServices().get(i)).getCode().equals(currentRepService.getCode())) {
                        ((DashboardActivity) getContext()).getAppointmentHelper().getRepServices().remove(i);
                        size--;
                    }
                }

                double price = 0;

                for (int i = 0; i < size; i++) {
                    price += ((Service) ((DashboardActivity) getContext()).getAppointmentHelper().getRepServices().get(i)).getPrice();
                }
                ((DashboardActivity) getContext()).getAppointmentHelper().setRepServicesPrice(price);
                repServicesAddedPrice.setText("$" + String.valueOf(price));
                if (size > 1) {
                    repServicesAddedLabel.setText(size + " servicios seleccionados - ");
                } else {
                    repServicesAddedLabel.setText(size + " servicio seleccionado - ");
                }

            }
            else {
                ((DashboardActivity) getContext()).getAppointmentHelper().getRepServices().add(currentRepService);
                ((DashboardActivity) getContext()).setWorkService(new Service(
                        currentRepService.getCode(),
                        currentRepService.getName(),
                        currentRepService.getPrice()));
                ((DashboardActivity) getContext()).setView("A_REPARATION_SERVICES_DESCRIPTION");
            }
        }
    });

    int size = ((DashboardActivity) getContext()).getAppointmentHelper().getRepServices().size();

    Log.i("RPSize", String.valueOf(((DashboardActivity) getContext()).getAppointmentHelper().getRepServices().size()));

    for (int i = 0; i < size; i++) {
        Service serv = ((Service) ((DashboardActivity) getContext()).getAppointmentHelper().getRepServices().get(i));
        if (serv.getCode().equals(currentRepService.getCode())) {
            Log.i("For", String.valueOf(i));
            Log.i("Data Service", String.valueOf(currentRepService.getName()));
            holder.checked.setChecked(true);
        }
    }

    return listItemView;
}

private static class RepServicesViewHolder {
    public TextView name;
    public TextView price;
    public ImageButton menuBtn;
    public Switch checked;
}

當我在服務描述片段上填寫描述,然后ListView嘗試重新呈現時,出現了此錯誤,它指出ListView相應地沒有size()。

這是創建列表的原始片段:

public class ReparationServicesFragment extends Fragment implements LoaderManager.LoaderCallbacks<JSONObject> {

private static final int REPARATION_SERVICES_LIST_LOADER_ID = 0;

@BindView(R.id.reparation_services_toolbar)
Toolbar reparationServicesToolbar;
@BindView(R.id.reparation_services_list)
ListView reparationServicesList;
@BindView(R.id.bottom_available_services_label)
TextView bottomAvailableServicesLabel;
@BindView(R.id.bottom_available_services_price)
TextView bottomAvailableServicesPrice;
Unbinder unbinder;

private ProgressDialog pDialog;
List<Service> services;
private ReparationServicesAdapter adapter;
private JSONObject appointmentData;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_reparation_services, container, false);

    unbinder = ButterKnife.bind(this, rootView);

    services = new ArrayList<>();
    adapter = new ReparationServicesAdapter(this.getContext(), new ArrayList<Service>());
    reparationServicesList.setAdapter(adapter);

    reparationServicesToolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            UiUtils.hideKeyboard(getContext());
            ((DashboardActivity) getActivity()).setView("A_SET");

        }
    });

    Log.i("RepServices", "Am i here?");
    Log.i("AppointmentHelper !!", String.valueOf(((DashboardActivity) getContext()).getAppointmentHelper().getRepServices().size()));

    /*for (int i = 0; i < ((DashboardActivity) getContext()).getAppointmentHelper().getRepServices().size(); i++) {
        Log.i("RepServices",((Service) ((DashboardActivity) getContext()).getAppointmentHelper().getRepServices().get(i)).getCode());
        Log.i("RepServices",((Service) ((DashboardActivity) getContext()).getAppointmentHelper().getRepServices().get(i)).getDescription());
    }*/

    appointmentData = new JSONObject();
    try {
        JSONArray workTypes = ((DashboardActivity) getActivity()).getAppointmentHelper().getBranch().getWorkTypes();

        for (int i = 0; i < workTypes.length(); i++) {
            if (workTypes.getJSONObject(i).get("tipo").toString().trim().equals("REPARACION")) {
                appointmentData.put("branchId", ((DashboardActivity) getActivity()).getAppointmentHelper().getBranch().getId());
                appointmentData.put("svcType", "REPARACION");
                appointmentData.put("vin", ((DashboardActivity) getActivity()).getVehicle().getVin());
                getLoaderManager().restartLoader(REPARATION_SERVICES_LIST_LOADER_ID, null, this);
            }
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return rootView;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
}

@Override
public Loader<JSONObject> onCreateLoader(int id, Bundle args) {
    pDialog = new ProgressDialog(getContext());
    pDialog.setMessage("Procesando...");
    pDialog.setCancelable(false);
    pDialog.show();
    Log.i("CreateLoader", "Cargando reportes de reparación");
    return new RepWorkServiceLoader(getContext());
}

@Override
public void onLoadFinished(Loader<JSONObject> loader, JSONObject data) {
    Log.i("JONN",  data.toString());

    if (pDialog.isShowing())
        pDialog.dismiss();

    if (data != null) {
        try {
            if (!data.get("data").toString().equals("null")) {
                JSONArray values = new JSONArray(data.get("data").toString());
                adapter.clear();
                if (values.length() > 0) {
                    for (int i = 0; i < values.length(); i++) {
                        JSONObject properties = values.getJSONObject(i);
                        services.add(new Service(
                                properties.getString("opcode").trim(),
                                properties.getString("nombre").trim(),
                                properties.getDouble("precio")
                        ));
                    }

                    Log.i("Servicios", String.valueOf(services.size()));

                    adapter.addAll(services);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void onLoaderReset(Loader<JSONObject> loader) {
    adapter.clear();
}

我越來越

java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 at java.util.ArrayList.get(ArrayList.java:411) at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:349) at com.ccstudio.android.petroautos.ui.adapters.ReparationServicesAdapter.getView(ReparationServicesAdapter.java:69

錯誤似乎在這里:

final Service currentRepService = getItem(position);

當我從服務描述片段返回片段時 ,適配器中的getItem的索引為1,大小為0。

感謝您的幫助。

-編輯-當我單擊列表中項目上的開關時,轉到以下片段:

public class ServiceDescriptionFragment extends Fragment {

@BindView(R.id.reparation_services_toolbar)
Toolbar reparationServicesToolbar;
@BindView(R.id.service_description)
EditText serviceDescription;
ArrayList services;
Unbinder unbinder;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_reparation_service_description, container, false);

    unbinder = ButterKnife.bind(this, rootView);

    services = ((DashboardActivity) getContext()).getAppointmentHelper().getRepServices();

    reparationServicesToolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            UiUtils.hideKeyboard(getContext());
            ((DashboardActivity) getActivity()).setView("A_REPARATION_SERVICES");

        }
    });
    return rootView;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
}

@OnClick(R.id.service_description_btn)
public void onViewClicked() {
    for (int i = 0; i < services.size(); i++) {
        if (((Service) services.get(i)).getCode().equals(((DashboardActivity) getContext()).getWorkService().getCode().toString())) {
            ((Service) services.get(i)).setDescription(serviceDescription.getText().toString());
            ((DashboardActivity) getContext()).getAppointmentHelper().setRepServices(services);
        }
    }
    ((DashboardActivity) getActivity()).setView("A_REPARATION_SERVICES");
}

}

當我返回帶有列表的Reparation Services視圖時,它給了我錯誤。

你為什么不做:

public class ReparationServicesAdapter extends ArrayAdapter<Service> {

    List<Service> serviceList; // store here for access later

    public ReparationServicesAdapter(@NonNull Context context, List<Service> repServicesList) {
        super(context, 0, repServicesList);
        serviceList = repServicesList
    }
}

並訪問如下數據:

// instead of this
final Service currentRepService = getItem(position);

// some more code
final Service currentRepService = serviceList.get(position);

我不認為這會導致內存開銷,因為Java中的每個非原始數據類型都被引用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM