【问题标题】:Total Hours not updated when listView get deleted删除 listView 时未更新总小时数
【发布时间】: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 删除。

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

【问题讨论】:

    标签: android listview android-listview baseadapter


    【解决方案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中点击
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多