简体   繁体   English

Android:仅在onCreate()中声明/实例化适配器吗?

[英]Android: are adapters only declared/instantiated in onCreate()?

I can boil this down into two questions: 我可以将其归结为两个问题:

  1. As the title suggests, is onCreate() the only place to create adapters? 顾名思义, onCreate()是创建适配器的唯一位置吗?

  2. I'm following this guide to create a Gridview , except I'm pulling from the Spotify API to get a list of playlists. 我正在按照指南创建Gridview ,除了要从Spotify API中提取播放列表。 The thing is that I don't have the playlists on hand when an adapter is created in onCreate() , which means I'm passing in null when I create the GridViewAdapter . 事实是,在onCreate()创建适配器时,手边没有播放列表,这意味着在创建GridViewAdapter时,我传递的是null。 Any design suggestions? 有任何设计建议吗?

Thanks for your feedback and time. 感谢您的反馈和宝贵的时间。

I think you're mixing 2 things because of the example you're following - creating the adapter and setting the data. 我想由于下面的示例,您正在混合两件事-创建适配器和设置数据。 The example uses an ArrayAdapter which requires AFAIK it's data in the constructor. 该示例使用了一个ArrayAdapter ,它需要AFAIK构造函数中的数据。

There's no de facto standard for adapters, but here's what I usually do: 适配器实际上没有标准,但是我通常这样做:

public class GridViewAdapter extends BaseAdapter {
  private final Context context;
  private List<DataClass> data = new ArrayList<>();

  public GridAdapter(Context context) {
    this.context = context;
  }

  public void setData(List<DataClass> data) {
    this.data = data;
    notifyDataSetChanged();
  }

  @Override
  public int getCount() {
    return data.size();
  }

  @Override
  public Object getItem(int i) {
    return data.get(i);
  }

  @Override
  public long getItemId(int i) {
    return i;
  }

  @Override
  public View getView(int i, View view, ViewGroup viewGroup) {
    // usual thing here
  }
}

A couple of things to notice here. 这里有几件事要注意。 DataClass is basically your data class that should hold all info need to populate the items. 基本上, DataClass是您的数据类,应该包含填充项目所需的所有信息。 You should favour List and not ArrayList as the example describes because it's more generic and allows you to change the adapter implementation without changing it's contract. 您应该偏爱List而不是ArrayList ,因为示例更加通用,因为它更通用并且允许您更改适配器实现而无需更改其协定。 You also should specify the type the list holds and not do as the example did where nothing is told to the compiler about the objects the array holds. 您还应该指定列表保存的类型,而不是像示例中那样不告诉编译器有关数组保存的对象的类型。

Now on to the good part and actually answering your questions. 现在到好部分,并实际回答您的问题。 You can create the adapter whenever you want. 您可以随时创建适配器。 I'm passing the context, but you might not even need this. 我正在传递上下文,但是您甚至可能不需要它。 Usually onCreate is a good place to create it, but you can do whenever. 通常, onCreate是创建它的好地方,但是您可以随时进行。

Once you receive the data, you simply call adapter.setData(...) and it will populate the adapter's data and notify it that the data has changed. 收到数据后,只需调用adapter.setData(...) ,它将填充适配器的数据并通知其数据已更改。

You can even separated these last 2 methods - setData and notifyDataSetChanged - and have the set data only set the data and call the notifyDataSetChanged() whenever you want. 你甚至可以分离这些最后的2种方法- setDatanotifyDataSetChanged -并有一组数据只设置数据并调用notifyDataSetChanged()只要你想。

Hope this helps 希望这可以帮助

EDIT 编辑

I'm not trying in anyway to say the example you're following is bad. 无论如何,我并不是要说您要遵循的示例很糟糕。 It's still a valid example and ArrayAdapters do the job when needed. 仍然是一个有效的示例,需要时ArrayAdapters可以完成工作。 I just think you're questions are very valid and that you didn't see the big picture yet because the example is quite specific. 我只是认为您的问题非常有效,并且您尚未看到全局,因为该示例非常具体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM