mercredi 1 mars 2017

Android - checkbox auto checked item in BaseAdapter after remove item

After I remove an checked item in my ListView AND if the item below is NOT checked - my BaseAdapter checks next item automatically. Even if this item is set to "isDone = false" in my OrderItem Class. After everey Click I set "isDone" to "true).

Every other case (like both checked or the item below checked or both unchecked) works fine while removing.

I tried a lot but unfortunately I cant find a solution for this. Im new to android and java.

Maybe u guys can help me. :) (sorry for my english - im from germany)

For example:

ITEM 1 (checked) ---> remove

ITEM 2 (not checked)

leads to:

ITEM 2 (checked)

Here is the getView from my BaseAdapter.

@Override
public View getView(final int index, View currentView, final ViewGroup parent) {


    //ViewHolder
    final ListViewItemWidgetWrapper viewHolder;


    final OrderItem orderItem = (OrderItem) getItem(index);


    if (currentView == null) {


   currentView = layoutInflater.inflate(R.layout.fragment_two, parent, false);

        viewHolder = new ListViewItemWidgetWrapper();

        // Instanz des ListViewItemWidgetWrapper


        //TextView fuer den Shop
        viewHolder.setTxtListShop((TextView) currentView.findViewById(R.id.txtListShop));

        //TextView fuer die Produktbeschreibung
        viewHolder.setTxtListProduct((TextView) currentView.findViewById(R.id.txtListProduct));

        //TextView fuer den Preis
        viewHolder.setTxtListValue((TextView) currentView.findViewById(R.id.txtListValue));

        //TextView fuer den Bezahldienstleister
        viewHolder.setTxtListPayment((TextView) currentView.findViewById(R.id.txtListPayment));

        //TextView fuer den Eingangsdatum
        viewHolder.setTxtListOrderDate((TextView) currentView.findViewById(R.id.txtListOrderDate));

        //TextView fuer dem TagesStatus of Return
        viewHolder.setTxtStatusOfReturn((TextView) currentView.findViewById(R.id.txtStatusOfReturn));

        viewHolder.setCheckBox((CheckBox) currentView.findViewById(R.id.list_checkbox));

        viewHolder.setImageOrder((ImageView) currentView.findViewById(R.id.image_order));


        if (FileHandler.getInstance().getAllOrders().get(index).getPictureForOrderString() == 0) {

            viewHolder.getImageOrder().setBackgroundResource(R.drawable.retoure_96);


        }

        if (FileHandler.getInstance().getAllOrders().get(index).getPictureForOrderString() == 1) {

            viewHolder.getImageOrder().setBackgroundResource(R.drawable.bank);


        }

        if (FileHandler.getInstance().getAllOrders().get(index).getPictureForOrderString() == 2) {

            viewHolder.getImageOrder().setBackgroundResource(R.drawable.neues_produkt_96);

        }


        viewHolder.getCheckBox().setChecked(orderItem.isDone());



        currentView.setTag(viewHolder);


    } else {


        viewHolder = (ListViewItemWidgetWrapper) currentView.getTag();


    }


    // Befuellen der einzelnen Widgets
    viewHolder.getTxtListShop().setText(orderItem.getShopName());

    viewHolder.getTxtListProduct().setText(orderItem.getProductDescription());

    viewHolder.getTxtListValue().setText(orderItem.getValue() + " EUR");

    viewHolder.getTxtListPayment().setText(orderItem.getPaymentName());

    viewHolder.getTxtListOrderDate().setText("vom " + orderItem.getDateOfOrder());

    viewHolder.getTxtStatusOfReturn().setText(orderItem.getStatusOfReturn());

    /*viewHolder.getCheckBox().setChecked(orderItem.isDone());                MY APP CRASHES AFTER DELETE ITEM IF I ACTIVATE THIS PEACE OF CODE */





    switch (FileHandler.getInstance().getAllOrders().get(index).getPictureForOrderString()) {

        case 0:

            viewHolder.getImageOrder().setBackgroundResource(R.drawable.retoure_96);

            break;

        case 1:

            viewHolder.getImageOrder().setBackgroundResource(R.drawable.bank);

            break;

        case 2:

            viewHolder.getImageOrder().setBackgroundResource(R.drawable.neues_produkt_96);

            break;

    }




   final CheckBox checkBox = viewHolder.getCheckBox();


    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {




            if (isChecked && orderItem.isDone() == false) {



                promptForResultDialog(new PromptRunnable() {
                    // Runnable Thread reagiert NACH der Benutzereingabe
                    public void run() {
                        // Hole den Wert vom Dialog

                        int value = this.getValue();

                        System.out.println("VALLLUUEEEEEEEEEEEEEEEEEEEEE____" + value);



                        // Den Wert zur Abfrage der Eingabe des Nutzers in einem Switch-Case prüfen
                        switch (value) {

                            case 0:
                                viewHolder.getImageOrder().setBackgroundResource(R.drawable.retoure_96);
                                orderItem.setPictureForOrderString(0);
                                orderItem.setIsDone(true);

                                orderItem.setStatusOfReturn("Retourniert");
                                viewHolder.getTxtStatusOfReturn().setText(orderItem.getStatusOfReturn());

                                Toast.makeText(context, "Als Retoure markiert!", Toast.LENGTH_SHORT).show();

                                refreshListFragment();

                                new FlipHorizontalAnimation(viewHolder.getImageOrder()).animate();

                                new SlideInUnderneathAnimation(viewHolder.getTxtStatusOfReturn()).animate();

                                viewHolder.getTxtStatusOfReturn().setTextColor(Color.parseColor("#ff4081"));

                                break;

                            case 1:
                                viewHolder.getImageOrder().setBackgroundResource(R.drawable.bank);
                                orderItem.setPictureForOrderString(1);
                                orderItem.setIsDone(true);
                                Toast.makeText(context, "Als bezahlt markiert!", Toast.LENGTH_SHORT).show();

                                orderItem.setStatusOfReturn("Bezahlt");
                                viewHolder.getTxtStatusOfReturn().setText(orderItem.getStatusOfReturn());

                                refreshListFragment();



                                viewHolder.getTxtStatusOfReturn().setTextColor(Color.parseColor("#ff4081"));

                                new FlipHorizontalAnimation(viewHolder.getImageOrder()).animate();
                                new SlideInUnderneathAnimation(viewHolder.getTxtStatusOfReturn()).animate();

                                break;

                        }
                    }
                });
            } else if (isChecked == false) {



                if(checkBox.isChecked()){
                    checkBox.toggle();
                }


                viewHolder.getImageOrder().setBackgroundResource(R.drawable.neues_produkt_96);

                orderItem.setPictureForOrderString(2);

                orderItem.setIsDone(false);



                orderItem.setStatusOfReturn(orderItem.getDayDifference() + " Tage bis zum " + orderItem.getEndDate());

                updateDates();

                viewHolder.getTxtStatusOfReturn().setText(orderItem.getStatusOfReturn());



                    new FlipHorizontalAnimation(viewHolder.getImageOrder()).animate();
                    new SlideInUnderneathAnimation(viewHolder.getTxtStatusOfReturn()).animate();




                refreshListFragment();

            }


        }

    });


    //rueckgabe der generierten View
    return currentView;
}

My Delete method is in my ListFragment called "TwoFragment" - see here:

  @Override
public boolean onContextItemSelected(MenuItem item){

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)   item.getMenuInfo();

    int index = info.position;



    switch (item.getItemId()){

        case R.id.delete:


            FileHandler.getInstance().getAllOrders().remove(index);

            refreshListFragment();  // FOR THREEFRAGMENT

            listViewActivityAdapter.notifyDataSetChanged();

            Toast.makeText(getActivity(),"Bestellung wurde gelöscht!", Toast.LENGTH_SHORT).show();

            return true;
    }



    return super.onContextItemSelected(item);

}

I would be very happy if you could help me :) Thank you in advance!




Aucun commentaire:

Enregistrer un commentaire