删除 listView 时未更新总小时数

Posted

技术标签:

【中文标题】删除 listView 时未更新总小时数【英文标题】:Total Hours not updated when listView get deleted 【发布时间】:2016-04-01 16:32:25 【问题描述】:

下图显示了WorkDetails

listView

List View

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

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

工作详情

  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】:
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
                            

                        );
);

【讨论】:

所以我有两种方式?一个在 onActivityResult 另一个在 listView click ? 你可以定义一个像updateTotalHour()这样的新函数然后你可以在onActivityResult中调用它并在listview中点击

以上是关于删除 listView 时未更新总小时数的主要内容,如果未能解决你的问题,请参考以下文章

winform。 listview 更新数据后刷新

从ArrayAdapter每毫秒更新Android ListView中的一行

ListView 总是从 CustomAdaptor 中删除最后一行

Flutter 在 ListView 中不显示有状态的子 Widget

从模型中删除项目时未更新表单验证。漏洞?

尝试在 ListView 中使用其内容时未找到 Flutter FutureProvider