且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将数据从活动传递到自定义适配器

更新时间:2023-10-16 18:19:28

这是因为,除非在适配器类中声明了名为getIntent()的函数,否则在适配器类中getIntent()不可用.在适配器中添加一个函数以接收适配器中的数据,然后调用notifyDataSetChanged()刷新列表.例如:

That is because getIntent() is not available in adapter class unless you declare a function named getIntent() in it. Add a function in the adapter to receive the data in the adapter and call notifyDataSetChanged() to refresh the list. for example:

class DataAdapter extends ArrayAdapter<String> {

    private ArrayList<String> items = new ArrayList<>();

    public DataAdapter(@NonNull Context context, @LayoutRes int resource) {
        super(context, resource);
    }


    public void setItems(ArrayList<String> items) {
        this.items = items;
        notifyDataSetChanged();
    }
    //some more code
}

在您的活动中创建适配器的实例,并将其绑定到列表/旋转器,然后调用该函数以在适配器中添加数据.

in your activity create an instance of the adapter and bind it to the list/spinner and then call the function to add data in the adapter.