Android - Custom ListView Adapter - Multi Selection remove - Indexoutofbounds - why?

前端 未结 6 502
终归单人心
终归单人心 2021-01-18 11:01

I have a custom Listview using a adapter class to extend ArrayAdapter of a Item class. I have the ability to change between choice modes of NONE,Single and Multi. This all

6条回答
  •  北荒
    北荒 (楼主)
    2021-01-18 11:29

    Filter using AlertDialog.Builder fully customized :-

    ((TextView) TabBarWithCustomStack.vPublic.findViewById(R.id.tv_edit)) .setVisibility(View.GONE);
        class DialogSelectionClickHandler implements
                DialogInterface.OnMultiChoiceClickListener {
            public void onClick(DialogInterface dialog, int clicked,
                    boolean selected) {
                Log.i("ME", hosts.toArray()[clicked] + " selected: " + selected);
            }
        }
    
        class DialogButtonClickHandler implements
                DialogInterface.OnClickListener {
            public void onClick(DialogInterface dialog, int clicked) {
                switch (clicked) {
                case DialogInterface.BUTTON_POSITIVE:
                    filteredList.clear();
                    statusesSelected.clear();
    
                    for (int i = 0; i < statusesStringArray.length; i++) {
                        // Log.i( "ME", statuses.toArray()[ i ] + " selected: "
                        // + isSelectedStatuses[i] );
    
                        if (isSelectedStatuses[i] == true) {
    
                            if (statuses.get(i).toString()
                                    .equalsIgnoreCase("Accepted")) {
                                statusesSelected.add("1");
                            } else if (statuses.get(i).toString()
                                    .equalsIgnoreCase("Rejected")) {
                                statusesSelected.add("2");
                            } else if (statuses.get(i).toString()
                                    .equalsIgnoreCase("Pending")) {
                                statusesSelected.add("3");
                            }
    
                        }
                        isSelectedStatuses[i] = false;
                    }
    
                    Calendar currentCalender = Calendar.getInstance(Locale
                            .getDefault());
    
                    Date currentDate = new Date(
                            currentCalender.getTimeInMillis());
    
                    if (listSelected == 0) {
                        for (int j = 0; j < arr_BLID.size(); j++) {
                            if (Helper.stringToDate(
                                    arr_BLID.get(j).getStart_ts().toString(),
                                    Helper.SERVER_FORMAT).after(currentDate)) {
                                if (statusesSelected.contains(arr_BLID.get(j)
                                        .getStatus())) {
                                    filteredList.add(arr_BLID.get(j));
                                }
                            }
                        }
    
                    } else {
                        for (int j = 0; j < arr_BLID.size(); j++) {
                            if (currentDate.after(Helper.stringToDate(arr_BLID
                                    .get(j).getStart_ts().toString(),
                                    Helper.SERVER_FORMAT))) {
                                if (statusesSelected.contains(arr_BLID.get(j)
                                        .getStatus())) {
                                    filteredList.add(arr_BLID.get(j));
                                }
                            }
                        }
    
                    }
    
                    lvBeeps.setAdapter(new EventsAdapter(ctx));
    
                    break;
    
                case DialogInterface.BUTTON_NEGATIVE: {
    
                    currentCalender = Calendar.getInstance(Locale.getDefault());
    
                    currentDate = new Date(currentCalender.getTimeInMillis());
    
                    if (listSelected == 0) {
    
                        filteredList.clear();
                        for (int i = 0; i < arr_BLID.size(); i++) {
                            if (Helper.stringToDate(
                                    arr_BLID.get(i).getStart_ts().toString(),
                                    Helper.SERVER_FORMAT).after(currentDate)) {
                                filteredList.add(arr_BLID.get(i));
                            }
    
                            if (i < isSelectedStatuses.length) {
                                isSelectedStatuses[i] = false;
                            } else {
                                continue;
                            }
                        }
    
                        lvBeeps.setAdapter(new EventsAdapter(ctx));
                    } else {
    
                        filteredList.clear();
                        for (int i = 0; i < arr_BLID.size(); i++) {
                            if (currentDate.after(Helper.stringToDate(arr_BLID
                                    .get(i).getStart_ts().toString(),
                                    Helper.SERVER_FORMAT))) {
                                filteredList.add(arr_BLID.get(i));
                            }
    
                            if (i < isSelectedStatuses.length) {
                                isSelectedStatuses[i] = false;
                            } else {
                                continue;
                            }
    
                        }
    
                        lvBeeps.setAdapter(new EventsAdapter(ctx));
    
                    }
                }
                    break;
                }
            }
        }
    
        btnHost = (Button) view.findViewById(R.id.btnHost);
        btnHost.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                // ((TextView)((RelativeLayout)getActivity().getLayoutInflater().inflate(R.layout.event_filter_title,
                // null)).findViewById(R.id.tvDialogTitle)).setText("Hosts");
    
                // Log.d( "Dialog object",
                // " static made dialog in checkbox--> "+((TextView)((RelativeLayout)getActivity().getLayoutInflater().inflate(R.layout.event_filter_title,
                // null)).findViewById(R.id.tvDialogTitle)).getText());
    
                final LayoutInflater inflater123 = (LayoutInflater) getActivity()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
                final View view123 = inflater123.inflate(
                        R.layout.event_filter_title, null);
    
                // Log.d( "Dialog object",
                // " static made dialog in view123--> "+view123);
                //
                // Log.d( "Dialog object",
                // " static made dialog in checkbox--> "+((CheckBox)view123.findViewById(R.id.cbSelectAll)));
                //
                // Log.d( "Dialog object",
                // " static made dialog in checkbox--> "+((CheckBox)inflater.inflate(R.layout.event_filter_title,
                // (ViewGroup) ((AlertDialog)
                // BeepsFragment.dialog).getCurrentFocus(),
                // true).findViewById(R.id.cbSelectAll)));
                //
                // Log.d( "Dialog object",
                // " static made dialog in checkbox--> "+((CheckBox)(((RelativeLayout)getActivity().getLayoutInflater().inflate(R.layout.event_filter_title,
                // null)).findViewById(R.id.cbSelectAll))));
                hostsDialog = new AlertDialog.Builder(getActivity())
                        .setCustomTitle(/*
                                         * Html.fromHtml(
                                         * "  Host"
                                         * )
                                         */view123)
                        .setIcon(
                                getActivity().getResources().getDrawable(
                                        R.drawable.add_host))
                        .setMultiChoiceItems(hostsStringArray, isSelectedHosts,
                                new OnMultiChoiceClickListener() {
    
                                    // android.content.DialogInterface.OnShowListener
                                    // ocl = new
                                    // DialogInterface.OnShowListener() {
                                    //
                                    // @Override
                                    // public void onShow(DialogInterface
                                    // dialog) {
                                    // // TODO Auto-generated method stub
                                    // BeepsFragment.dialog = dialog;
                                    // }
                                    // };
    
                                    public void onClick(DialogInterface dialog,
                                            int clicked, boolean selected) {
    
                                        boolean all = true;
                                        Log.i("ME", hosts.toArray()[clicked]
                                                + " selected: " + selected);
    
                                        // for (int i = 0; i <
                                        // isSelectedHosts.length; i++ ){
                                        //
                                        // if(isSelectedHosts[i]==true){
                                        // all = true;
                                        // }else{
                                        // all = false;
                                        // }
                                        // }
                                        //
                                        // Log.i( "ME", all + " selected:--- "
                                        // +((CheckBox)view123.findViewById(R.id.cbSelectAll)));
                                        //
                                        // if(all = true){
                                        // ((CheckBox)view123.findViewById(R.id.cbSelectAll)).setChecked(true);
                                        // }else{
                                        // ((CheckBox)view123.findViewById(R.id.cbSelectAll)).setChecked(false);
                                        // }
    
                                        Log.d("Dialog object",
                                                " static made dialog --> "
                                                        + BeepsFragment.dialog
                                                        + " from parameter dialog --> "
                                                        + dialog);
    
                                    }
    
                                })
                        .setPositiveButton(
                                Html.fromHtml("Apply Filter"),
                                new DialogButtonClickHandler() {
    
                                    // android.content.DialogInterface.OnShowListener
                                    // ocl = new
                                    // DialogInterface.OnShowListener() {
                                    //
                                    // @Override
                                    // public void onShow(DialogInterface
                                    // dialog) {
                                    // // TODO Auto-generated method stub
                                    // BeepsFragment.dialog = dialog;
                                    // }
                                    // };
    
                                    public void onClick(DialogInterface dialog,
                                            int clicked) {
                                        switch (clicked) {
                                        case DialogInterface.BUTTON_POSITIVE:
    
                                            filteredList.clear();
                                            hostsSelected.clear();
    
                                            for (int i = 0; i < hostsStringArray.length; i++) {
                                                Log.i("ME",
                                                        hosts.toArray()[i]
                                                                + " selected: "
                                                                + isSelectedHosts[i]
                                                                + "\n\n\t hostStringArray-->"
                                                                + hostsStringArray[i]);
    
                                                if (isSelectedHosts[i] == true) {
    
                                                    hostsSelected.add(hosts
                                                            .get(i));
                                                    isSelectedHosts[i] = false;
                                                }
                                                // isSelectedHosts[i] = false;
                                            }
    
                                            Calendar currentCalender = Calendar
                                                    .getInstance(Locale
                                                            .getDefault());
    
                                            Date currentDate = new Date(
                                                    currentCalender
                                                            .getTimeInMillis());
    
                                            if (listSelected == 0) {
                                                for (int j = 0; j < arr_BLID
                                                        .size(); j++) {
                                                    if (Helper
                                                            .stringToDate(
                                                                    arr_BLID.get(
                                                                            j)
                                                                            .getStart_ts()
                                                                            .toString(),
                                                                    Helper.SERVER_FORMAT)
                                                            .after(currentDate)) {
                                                        if (hostsSelected
                                                                .contains(arr_BLID
                                                                        .get(j)
                                                                        .getHost_name())) {
                                                            filteredList
                                                                    .add(arr_BLID
                                                                            .get(j));
                                                        }
                                                        if (hostsSelected
                                                                .contains("Me"))
                                                            if (BeepApplication
                                                                    .getSelfId() == arr_BLID
                                                                    .get(j)
                                                                    .getHost_id())
                                                                filteredList
                                                                        .add(arr_BLID
                                                                                .get(j));
                                                    }
                                                }
    
                                            } else {
                                                for (int j = 0; j < arr_BLID
                                                        .size(); j++) {
                                                    if (currentDate.after(Helper
                                                            .stringToDate(
                                                                    arr_BLID.get(
                                                                            j)
                                                                            .getStart_ts()
                                                                            .toString(),
                                                                    Helper.SERVER_FORMAT))) {
                                                        if (hostsSelected
                                                                .contains(arr_BLID
                                                                        .get(j)
                                                                        .getHost_name())) {
                                                            filteredList
                                                                    .add(arr_BLID
                                                                            .get(j));
                                                        }
                                                        if (hostsSelected
                                                                .contains("Me"))
                                                            if (BeepApplication
                                                                    .getSelfId() == arr_BLID
                                                                    .get(j)
                                                                    .getHost_id())
                                                                filteredList
                                                                        .add(arr_BLID
                                                                                .get(j));
                                                    }
                                                }
    
                                            }
                                            lvBeeps.setAdapter(new EventsAdapter(
                                                    ctx));
                                            break;
                                        }
                                    }
    
                                })
                        .setNegativeButton(
                                Html.fromHtml("Remove Filter"),
                                new DialogButtonClickHandler() {
    
                                    public void onClick(
                                            final DialogInterface dialog,
                                            int clicked) {
    
                                        Calendar currentCalender = Calendar
                                                .getInstance(Locale
                                                        .getDefault());
    
                                        Date currentDate = new Date(
                                                currentCalender
                                                        .getTimeInMillis());
    
                                        if (listSelected == 0) {
    
                                            filteredList.clear();
                                            for (int i = 0; i < arr_BLID.size(); i++) {
                                                if (Helper.stringToDate(
                                                        arr_BLID.get(i)
                                                                .getStart_ts()
                                                                .toString(),
                                                        Helper.SERVER_FORMAT)
                                                        .after(currentDate)) {
                                                    filteredList.add(arr_BLID
                                                            .get(i));
                                                    if (i < isSelectedHosts.length) {
                                                        isSelectedHosts[i] = false;
                                                    } else {
                                                        continue;
                                                    }
                                                }
                                            }
    
                                            lvBeeps.setAdapter(new EventsAdapter(
                                                    ctx));
                                        } else {
    
                                            filteredList.clear();
                                            for (int i = 0; i < arr_BLID.size(); i++) {
                                                if (currentDate.after(Helper
                                                        .stringToDate(
                                                                arr_BLID.get(i)
                                                                        .getStart_ts()
                                                                        .toString(),
                                                                Helper.SERVER_FORMAT))) {
                                                    filteredList.add(arr_BLID
                                                            .get(i));
                                                    if (i < isSelectedHosts.length) {
                                                        isSelectedHosts[i] = false;
                                                    } else {
                                                        continue;
                                                    }
                                                }
                                            }
    
                                            lvBeeps.setAdapter(new EventsAdapter(
                                                    ctx));
    
                                        }
                                    }
                                });
    
                final AlertDialog dlg = hostsDialog.create();
    
                dlg.show();
    
                ((TextView) view123.findViewById(R.id.tvDialogTitle))
                        .setText("Hosts");
    
                ((CheckBox) view123.findViewById(R.id.cbSelectAll))
                        .setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                            @Override
                            public void onCheckedChanged(
                                    CompoundButton buttonView, boolean isChecked) {
                                // TODO Auto-generated method stub
    
                                if (isChecked == true) {
    
                                    ListView list = dlg.getListView();
                                    for (int i = 0; i < list.getCount(); i++) {
    
                                        isSelectedHosts[i] = true;
                                        list.setItemChecked(i, true);
    
                                    }
    
                                }
    
                                else if (isChecked == false) {
    
                                    ListView list = dlg.getListView();
                                    for (int j = 0; j < list.getCount(); j++) {
    
                                        isSelectedHosts[j] = false;
                                        list.setItemChecked(j, false);
                                    }
                                    // }
                                }
    
                            }
                        });
    
            }
        });
    
        btnStatus = (Button) view.findViewById(R.id.btnStatus);
        btnStatus.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                LayoutInflater inflater123 = (LayoutInflater) getActivity()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
                View view123 = inflater123.inflate(R.layout.event_filter_title,
                        null);
    
                statusDialog = new AlertDialog.Builder(getActivity())
                        .setIcon(
                                getActivity().getResources().getDrawable(
                                        R.drawable.add_host))
                        .setCustomTitle(/*
                                         * Html.fromHtml(
                                         * "  Status"
                                         * )
                                         */view123)
                        .setIcon(
                                getActivity().getResources().getDrawable(
                                        R.drawable.add_host))
                        .setMultiChoiceItems(statusesStringArray,
                                isSelectedStatuses,
                                new DialogSelectionClickHandler())
                        .setPositiveButton(
                                Html.fromHtml("Apply Filter"),
                                new DialogButtonClickHandler() {
    
                                })
                        .setNegativeButton(
                                Html.fromHtml("Remove Filter"),
                                new DialogButtonClickHandler());
    
                final AlertDialog dlg = statusDialog.create();
    
                dlg.show();
    
                ((TextView) view123.findViewById(R.id.tvDialogTitle))
                        .setText("Status");
                ((CheckBox) view123.findViewById(R.id.cbSelectAll))
                        .setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                            @Override
                            public void onCheckedChanged(
                                    CompoundButton buttonView, boolean isChecked) {
                                // TODO Auto-generated method stub
    
                                if (isChecked == true) {
    
                                    ListView list = dlg.getListView();
                                    for (int i = 0; i < list.getCount(); i++) {
    
                                        isSelectedStatuses[i] = true;
                                        list.setItemChecked(i, true);
    
                                    }
    
                                }
    
                                else if (isChecked == false) {
    
                                    ListView list = dlg.getListView();
                                    for (int j = 0; j < list.getCount(); j++) {
    
                                        isSelectedStatuses[j] = false;
                                        list.setItemChecked(j, false);
                                    }
                                    // }
                                }
    
                            }
                        });
    
            }
        });
    

提交回复
热议问题