删除listView时未更新总小时数

时间:2015-12-27 17:47:22

标签: android listview android-listview baseadapter

下图显示了 WorkDetails

listView

List View

我使用footer显示总小时布局,footer layout显示两个按钮

当第三个listView被删除时,它假设显示7个小时((第二个列表中的18:0,来自第一个列表的-10:00)-1)。但它仍显示18:32

WorkDetails

  listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { // long press on listView
                public boolean onItemLongClick(final AdapterView<?> p, View v, final int po, long id) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(WorkDetailsTable.this);
                    builder.setTitle("Delete");
                    builder.setMessage("Are you sure you want to delete?");
                    builder.setIcon(android.R.drawable.ic_dialog_alert);
                    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int ii) {
                            objMyCustomBaseAdapter.removeItem(po);
                        }

                    });
            });

  @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
        if (resultCode == RESULT_OK) {
            if (requestCode == PROJECT_REQUEST_CODE) {
                ReceiveProject = data.getStringExtra("Project");
                ReceiveDescription = data.getStringExtra("Description");
                ReceiveProgress = data.getIntExtra("progress", 0);
                ReceiveTimeIn = data.getStringExtra("TimeIn");
                ReceiveTimeOut = data.getStringExtra("TimeOut");

                int a = SplitTime(objMyCustomBaseAdapter.getFistTime()); // calculate timeIn
                int b = SplitTime(objMyCustomBaseAdapter.getLastTime()); //calculate timeOut
                long difference = 0;
                if (a > b) {
                    difference = (b + (24 * 60) - a) - (1 * 60);
                    int minutes = (int) (difference % 60);
                    int hours = (int) ((difference / 60) % (24 * 60));
                    totalHours.setText(("Total hours : " + hours + ":" + minutes));

                } else {
                    difference = (b - a) - (1 * 60);
                    int minutes = (int) (difference % 60);
                    int hours = (int) ((difference / 60) % (24 * 60));
                    totalHours.setText(("Total hours : " + hours + ":" + minutes));
                }

            }
        }
            }
        }

MyCustomBaseAdapter

     public void removeItem(int position) {
            searchArrayList.remove(position);
            addOrRemoveFooter();
            this. notifyDataSetChanged();
        }

     public void addOrRemoveFooter(){
            if(searchArrayList.size() == 0 && listview.getFooterViewsCount() > 0){  // if no listView
                listview.removeFooterView(footer);
                listview.removeFooterView(footerLayout);
            }else if(listview.getFooterViewsCount() == 0 && searchArrayList.size() > 0){ // if exists listView
                listview.addFooterView(footer);
                listview.addFooterView(footerLayout);
            }
        }

  public String getFistTime() {
        SearchResults firstTime = this.searchArrayList.get(0);
        return firstTime.getTimeIn();
       }

    public String getLastTime() {
        SearchResults lastTime = this.searchArrayList.get(searchArrayList.size() - 1);
        return lastTime.getTimeOut();
    }

如果我将新列表添加到listView,则会更新总小时数,但不会更新listView删除。

请告诉我如何更新列表被删除的总小时数。非常感谢!

1 个答案:

答案 0 :(得分:0)

listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { // long press on listView
                    public boolean onItemLongClick(final AdapterView<?> p, View v, final int po, long id) {
                        ...
                        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int ii) {
                                objMyCustomBaseAdapter.removeItem(po);
                                // calculate total hour then update it in here
                            }

                        });
});