简体   繁体   English

Android中的ArrayAdapter和ListAdapter之间的区别?

[英]Difference between ArrayAdapter and ListAdapter in Android?

I know that ListAdapter is an interface and ArrayAdapter is a class. 我知道ListAdapter是一个接口而ArrayAdapter是一个类。 So we can only instantiate ArrayAdapter. 所以我们只能实例化ArrayAdapter。 I met up a code 我遇到了一个代码

ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songNames);

But I was able to do the same thing with 但我能够做同样的事情

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songsArray);

So I want to know, in what exact places do we need ListAdapter ? 所以我想知道,在什么地方我们需要ListAdapter?

Thank you 谢谢

This is not related to Android. 这与Android无关。 But a general Java question. 但是一般的Java问题。

When you use ListAdapter as the type for your variable, you are really interested in the interface. 当您使用ListAdapter作为变量的类型时,您真的对界面感兴趣。 You may not be interested in calling some specific method of ArrayAdapter<String> not available in ListAdapter . 您可能对调用ListAdapter不可用的ArrayAdapter<String>某些特定方法ListAdapter That is mainly because you will just assign the adapter as the list adapter for the view. 这主要是因为您只需将适配器指定为视图的列表适配器。

You may use the precise type of ArrayAdapter<String> if you really need to call some specific methods. 如果确实需要调用某些特定方法,可以使用精确类型的ArrayAdapter<String>

It all depends on your use case. 这一切都取决于您的用例。

BaseAdapter Its a very generic adapter that allows you to do pretty much whatever you want. BaseAdapter它是一个非常通用的适配器,可以让你做任何你想做的事情。 It's a base class of all adapter. 它是所有适配器的基类。

ArrayAdapter Its a more complete implementation that works well for data in arrays or ArrayLists. ArrayAdapter它是一个更完整的实现,适用于数组或ArrayLists中的数据。 Similarly,there is a related CursorAdapter that you should use if your data is in a Cursor. 同样,如果您的数据位于Cursor中,则应使用相关的CursorAdapter。 Both of these extend BaseAdapter 这两个都扩展了BaseAdapter

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

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