【问题标题】:Calling notifydatasetchange for one adapter from another adapter从另一个适配器调用一个适配器的 notifydatasetchange
【发布时间】:2012-12-31 17:29:32
【问题描述】:

在我的活动中,我有两个列表视图,因此我使用了两个不同的适配器。我的要求是在两个列表项中我都有一个按钮。单击任何列表视图中的按钮时,列表视图中的数据都应更改。现在的问题是如何通过单击另一个适配器中的按钮来访问一个适配器的 adapter.notifyDataSetChanged() ?

【问题讨论】:

    标签: android listview listadapter onclicklistener baseadapter


    【解决方案1】:

    只需在您的 onClick 方法中

    public void onClick(View v)
    {
     adapter1.notifyDataSetChanged();
     adapter2.notifyDataSetChanged();
    }
    

    如果您使用列表视图行中的按钮,那么您应该在 getView() 方法中执行此操作

    @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            View row=convertView;
            if(row==null)
            {
                LayoutInflater inflater=((Activity)context).getLayoutInflater();
                row=inflater.inflate(layoutResourceId, parent,false);
    
                holder=new YourHodler();
                holder.button=(Button)row.findViewById(R.id.bt);
    
    
                row.setTag(holder);
            }
            else
            {
                holder=(YourHolder)row.getTag();
            }
    
            holder.button.setOnClickListner(new View.onClickListener()
                    {
                       //update your Any data set which is beign attached to both the adapters
                       //for example if you are using List<Collection> then you should first
                       //update it or change it
                       //then
                       adapter1.notifyDataSetChanged();
                       adapter2.notifyDataSetChanged();
                    }
    
            return row;
        }
    

    【讨论】:

    • 我对两个适配器都有不同的类
    • 没问题,只需更新附加到两个类的数据集即可完成
    • 你找到我还是需要更多说明
    • 我猜你正在实现某种 1.List&lt;Class1&gt; list1=new ArrayList&lt;Class1&gt;(); 2.List&lt;Class2&gt; list2=new ArrayList&lt;Class2&gt;(); 只需在你的 onClick() 方法中更新你想要更新的列表并调用adaoter.notifyDataSetChanged()
    • 只是更新数据集?我有 2 个不同的数组适配器,我将它们传递给两个不同类别的列表适配器
    【解决方案2】:

    在您的 Activity 中实现一个执行以下操作的方法:

    CustomActivity1 extends Activity {
    ....
    //make it public
    public void updateLists() {
       CustomAdapter1 adapter1 = (CustomAdapter1) ((ListView) findViewById(R.id.list1)).getListAdapter();
       CustomAdapter2 adapter2 = (CustomAdapter2) ((ListView) findViewById(R.id.list2)).getListAdapter();
    
       //update the adapters
       adapter1.notifyDatasetChanged();
       adapter2.notifyDatasetChanged();
    }
    ....
    }
    

    为了使自定义适配器起作用,您必须传递一个Activity 上下文。因此,您可以在两个适配器中添加 onclick 侦听器,例如:

    CustomAdapter1 { //and in CustomAdapter2 
    ....
    private OnClickListener ButtonClick = new OnClickListener() {
         public void onClick(View v) {
             //...your code             
             //having the context of the CustomActivity1 stored in a variable from the constructor you can simply do:
             customActivity1Context.updateLists();
         }
    }
    ....
    }
    

    【讨论】:

    • 我只能在按钮单击时执行 .notifyDatasetChanged() 并且该按钮是在适配器类中定义的。并且这两个适配器都是不同的类,因此它们没有彼此的实例。
    • 我已经更新了我的答案。无需制作内部类或静态方法。只需将您想要的方法公开,并在两个适配器中通过活动的上下文随时访问它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 2019-02-07
    相关资源
    最近更新 更多