简体   繁体   English

我们需要在Android中实现所有抽象方法吗?

[英]Do we need to implement all the abstract methods in Android?

The following code is the typical adapter extends BaseAdapter for GalleryView/GridView etc.: 以下代码是BaseAdapter用于GalleryView / GridView等的典型适配器扩展:

 public class GalleryImageAdapter extends BaseAdapter 
 {
     private Context mContext;

     private Integer[] ImageIds = {
             R.drawable.image1,
             R.drawable.image2,
             R.drawable.image3           
     };

     public GalleryImageAdapter(Context context) 
     {
         mContext = context;
     }
     public int getCount() {
         return mImageIds.length;
     }
     public Object getItem(int position) {
         return position;
     }
     public long getItemId(int position) {
         return position;
     }

     public View getView(int index, View view, ViewGroup viewGroup) 
     {
         // TODO Auto-generated method stub
         ImageView i = new ImageView(mContext);

         i.setImageResource(ImageIds[index]);
         i.setLayoutParams(new Gallery.LayoutParams(200, 200));

         i.setScaleType(ImageView.ScaleType.FIT_XY);

         return i;
     }
 }

If we go to Android website, developer.android.com, we find the following abstract methods. 如果访问Android网站developer.android.com,则会发现以下抽象方法。

Adapter>BaseAdapter(subclass of Adapter) 适配器> BaseAdapter(适配器的子类)

abstract int  getCount() 
abstract Object  getItem(int position) 
abstract long  getItemId(int position) 
abstract int  getItemViewType(int position) 
abstract View  getView(int position, View convertView, ViewGroup parent) 
abstract int  getViewTypeCount() 
abstract boolean  hasStableIds() 
abstract boolean  isEmpty()  
abstract void  registerDataSetObserver(DataSetObserver observer) 
abstract void  unregisterDataSetObserver(DataSetObserver observer) 

My question is do we need to implement all the above abstract methods? 我的问题是我们是否需要实现所有上述抽象方法? In my example code above, we use getCount(), getItem(), getItemId and getView only. 在上面的示例代码中,我们仅使用getCount(),getItem(),getItemId和getView。 Why? 为什么?

Abstract methods you posted here are from Adapter interface. 您在此处发布的抽象方法来自Adapter接口。 BaseAdapter implements most of them, leaving you to just put the ones you have (and of course you can override default implementation). BaseAdapter实现了它们中的大多数,只剩下您拥有的(当然,您可以覆盖默认实现)。

I asked myself this same question myself this morning and then I did some investigations. 今天早上我问自己同样的问题,然后进行了一些调查。

If you look at the code for BaseAdapter class: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/widget/BaseAdapter.java you will see that some of the abstract methods in your list are already implemented through BaseAdapter. 如果您查看BaseAdapter类的代码: http ://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/widget/BaseAdapter.java,看到列表中的某些抽象方法已经通过BaseAdapter实现。

You therefore only need to implement the methods that are not already implemented within BaseAdapter, such as getCount() or getItem(). 因此,您仅需要实现BaseAdapter中尚未实现的方法,例如getCount()或getItem()。 Android only forces you to implement those methods that are not already implemented and you can override the methods already implemented of course. Android仅强制您实现尚未实现的那些方法,并且您可以覆盖当然已经实现的方法。

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

相关问题 为什么我们需要重写接口中的抽象方法? - Why do we need to override abstract methods from an interface? 我们是否需要将所有具有不同dpi的图像添加到Android Apps - Do we need to add all images with different dpi to Android Apps 接口的所有方法都是抽象的吗? - Are all methods of interface abstract? Android:扩展Application类。 为什么我们需要实现单例模式? - Android: extending Application class. Why do we need to implement singleton pattern? 我们如何创建接口以及如何在android中实现接口方法? - How can we create Interface and how to implement Interface methods in android? Android活动和片段生命周期-我需要在代码中使用所有方法吗? - Android activity and fragment life cycle - do I need to use all methods in my code? Android SDK文件夹占用了大量磁盘空间。我们需要保留所有系统映像吗? - Android SDK folder taking a lot of disk space. Do we need to keep all of the System Images? 为什么我们需要将View对象传递给某些方法? - Why do we need to pass View objects to some methods? 为什么我们不需要在Android中实例化LayoutInflater? - Why do we not need to instantiate LayoutInflater in Android? 为什么我们需要在Android中使用Intent过滤器? - why do we need intent filter in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM