Search code examples
androidlistviewandroid-listviewbaseadapter

Total Hours not updated when listView get deleted


The image below shows the listView of WorkDetails

List View

I use footer to display the total hours layout and footer layout display two buttons

When the third listView gets deleted, it supposes to display 7 hours((18:0 from the second list -10:00 from the first list)-1). But it still display 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();
    }

If I add the new list to listView, the total hours will be updated, but not for listView delete.

Please tell me how should I update the total hours when the list get deleted. Thanks a lot !


Solution

  • 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
                                }
    
                            });
    });