Android - 删除项目

时间:2017-03-02 01:45:59

标签: android listview checkbox fragment baseadapter

删除ListView中的已检查项目后如果未检查下面的项目 - 我的BaseAdapter会自动检查下一项目。即使在我的OrderItem类中将此项设置为“isDone = false”。在everey之后点击并回答我的对话 - 我将“isDone”设置为“true”。

其他情况(如已检查或下面检查的项目或两者都未选中)在删除时工作正常。

我尝试了很多,但不幸的是我无法找到解决方案。我是android和java的新手。

也许你们可以帮助我。 :)(对不起我的英语 - 来自德国)

例如:

项目1(已检查)--->删除

项目2(未选中)

导致:

项目2(已检查)

以下是我的BaseAdapter中的getView。

@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;
}

我的Delete方法在我的ListFragment中,名为“TwoFragment” - 见这里:

  @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);

}

如果你能帮助我,我会很高兴:)提前谢谢!

1 个答案:

答案 0 :(得分:0)

更新:

大家好,经过几个小时我自己解决了!

每次我删除一个Item时,都会调用现有项的else方法 - 见下文:

else {


        viewHolder = (ListViewItemWidgetWrapper) currentView.getTag();


    }

问题是,当我在重建ListView时检查我的Checkbox时 - 我的OnCheckedChangeListener被调用了!

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

由于我的监听器中的动画线程Runnalbe,应用程序在监听器中调用else方法(取消选中)时崩溃。

我通过在Checkbox重建之前禁用OnCheckedChangeListener来修复它。

  viewHolder.getCheckBox().setOnCheckedChangeListener(null);

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

请勿忘记在此过程后重新激活监听器!

希望它会有所帮助!