梁君 2015-12-09 09:21 采纳率: 0%
浏览 680

SlideExpandableListView适配器改变时,未通知到Activity

The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131230834, class com.wlyc.warehousechampions.ui.views.slideexpandable.ActionSlideExpandableListView) with Adapter(class com.wlyc.warehousechampions.ui.views.slideexpandable.SlideExpandableListAdapter)]

package com.wlyc.warehousechampions.ui.views.slideexpandable;

import com.wlyc.warehousechampions.R;

import android.view.View;
import android.widget.ListAdapter;

/**

  • ListAdapter that adds sliding functionality to a list.
  • Uses R.id.expandalbe_toggle_button and R.id.expandable id's if no
  • ids are given in the contructor. *
  • @author tjerk
  • @date 6/13/12 8:04 AM
    */
    public class SlideExpandableListAdapter extends AbstractSlideExpandableListAdapter {
    private int toggle_button_id;
    private int expandable_view_id;

    public SlideExpandableListAdapter(ListAdapter wrapped, int toggle_button_id, int expandable_view_id) {
    super(wrapped);
    this.toggle_button_id = toggle_button_id;
    this.expandable_view_id = expandable_view_id;
    }

    public SlideExpandableListAdapter(ListAdapter wrapped) {
    this(wrapped, R.id.expandable_toggle_button, R.id.expandable);
    }

    @Override
    public View getExpandToggleButton(View parent) {
    return parent.findViewById(toggle_button_id);
    }

    @Override
    public View getExpandableView(View parent) {
    return parent.findViewById(expandable_view_id);
    }
    }

package com.wlyc.warehousechampions.ui.views.slideexpandable;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;

/**

  • A more specific expandable listview in which the expandable area
  • consist of some buttons which are context actions for the item itself. *
  • It handles event binding for those buttons and allow for adding
  • a listener that will be invoked if one of those buttons are pressed. *
  • @author tjerk
  • @date 6/26/12 7:01 PM
    */
    public class ActionSlideExpandableListView extends SlideExpandableListView {
    private OnActionClickListener listener;
    private int[] buttonIds = null;

    public ActionSlideExpandableListView(Context context) {
    super(context);
    }

    public ActionSlideExpandableListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    }

    public ActionSlideExpandableListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    }

    public void setItemActionListener(OnActionClickListener listener, int ... buttonIds) {
    this.listener = listener;
    this.buttonIds = buttonIds;
    }

    /**

    • Interface for callback to be invoked whenever an action is clicked in
    • the expandle area of the list item. / public interface OnActionClickListener { /*
      • Called when an action item is clicked. *
      • @param itemView the view of the list item
      • @param clickedView the view clicked
      • @param position the position in the listview */ public void onClick(View itemView, View clickedView, int position); }

    public void setAdapter(ListAdapter adapter) {
    super.setAdapter(new WrapperListAdapterImpl(adapter) {

        @Override
        public View getView(final int position, View view, ViewGroup viewGroup) {
            final View listView = wrapped.getView(position, view, viewGroup);
            // add the action listeners
            if(buttonIds != null && listView!=null) {
                for(int id : buttonIds) {
                    View buttonView = listView.findViewById(id);
                    if(buttonView!=null) {
                        buttonView.findViewById(id).setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if(listener!=null) {
                                    listener.onClick(listView, view, position);
                                }
                            }
                        });
                    }
                }
            }
            return listView;
        }
    });
    

    }
    }

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-04 23:40
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在Android中,SlideExpandableListView的适配器(Adapter)是通过继承自AbstractSlideExpandableListAdapter实现的。这个适配器的主要目的是处理列表中的数据,并为用户显示相应的视图。

    当你的适配器发生变化时,你可能希望在适配器更新后立即通知ListView,以便它能够重新计算和显示新的内容。为了实现这一点,你可以使用以下步骤:

    1. 在适配器的构造函数或方法中调用notifyDataSetChanged()
    2. 调用super.notifyDataSetInvalidated()来触发适配器的数据更改通知。

    这是一个基本的例子,展示如何在适配器发生更改时通知ListView

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        notifyDataSetInvalidated();
    }
    

    这样,适配器的任何更改都会被通知给ListView,并允许它重新渲染新内容。

    请注意,如果你的适配器包含嵌套的列表,那么你需要确保在每个子列表的适配器中也调用notifyDataSetChanged()

    此外,你应该避免在后台线程上修改适配器的内容,因为这可能会导致同步问题。适配器应该只在UI线程上调用notifyDataSetChanged()或者notifyDataChanged()

    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格