Android-Java如何检查调用了哪个类构造函数

Android-Java如何检查调用了哪个类构造函数,java,android,Java,Android,我有一个类,我用它来幻灯片放映图像,使用sliderAdapter构造函数并传递一个字符串列表,它的工作很好 public class sliderAdapter extends PagerAdapter { private Context context; private List<String> URLs = new ArrayList<>(); sliderAdapter(Context context, List<String

我有一个类,我用它来幻灯片放映图像,使用sliderAdapter构造函数并传递一个字符串列表,它的工作很好

public class sliderAdapter extends PagerAdapter {



    private Context context;

    private List<String> URLs = new ArrayList<>();
    sliderAdapter(Context context, List<String> URLs){
        this.URLs = URLs;
        this.context = context;
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view== object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.slideshow_layout,container,false);
        ImageView img = view.findViewById(R.id.imageview);
        Glide.with(context).load(URLs.get(position)).into(img);



        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout)object);
    }

}
公共类sliderAdapter扩展了PagerAdapter{
私人语境;
私有列表URL=new ArrayList();
sliderAdapter(上下文、列表URL){
this.url=url;
this.context=上下文;
}
@凌驾
public int getCount(){
返回url.size();
}
@凌驾
公共布尔值isViewFromObject(视图,对象){
返回(视图==对象);
}
@凌驾
公共对象实例化项(视图组容器,int位置){
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
视图=布局扁平化。充气(R.layout.slideshow\u布局,容器,假);
ImageView img=view.findViewById(R.id.ImageView);
Glide.with(context).load(url.get(position)).into(img);
container.addView(视图);
返回视图;
}
@凌驾
公共项(视图组容器、int位置、对象){
container.removeView((LinearLayout)对象);
}
}
现在我需要通过添加从drawable文件夹加载图像的功能来改进我的滑块,因此我必须传递一个整数列表,我不知道具体的方法

据我所知,我应该创建一个新的构造函数来接受整数列表 但是,对于所有其他函数,我应该如何更改我的类以使其与这两个构造函数一起正常工作呢


任何想法都将受到欢迎

您可以这样做:

public class Item{
   String url;
   int id;
   boolean link = false;
   boolean resource = false;
   public Item(int id){
       this.id = id;
       resource = true;
   }
   public Item(String url){
       this.url = url;
       link = true;
   }
   public boolean isLink(){
       return link;
   }
   public boolean isResource(){
       return resource;
   }
   public int getId(){
       return id;
   }
   public String getUrl(){
       return url;
   }
}



public class sliderAdapter extends PagerAdapter {

    private Context context;

    private List<Item> items = new ArrayList<>();
    sliderAdapter(Context context, List<Item> items){
        this.items = items;
        this.context = context;
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view== object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.slideshow_layout,container,false);
        ImageView img = view.findViewById(R.id.imageview);
        Item item = items.get(position);
        if(item.isLink()){
            Glide.with(context).load(item.getUrl()).into(img);
        }else{
            img.setImageResource(item.getId());
        }

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout)object);
    }
}
公共类项目{
字符串url;
int-id;
布尔链接=假;
布尔资源=false;
公共项目(int id){
this.id=id;
资源=真;
}
公共项(字符串url){
this.url=url;
link=true;
}
公共布尔isLink(){
返回链接;
}
公共布尔值isResource(){
返回资源;
}
公共int getId(){
返回id;
}
公共字符串getUrl(){
返回url;
}
}
公共类sliderAdapter扩展了PagerAdapter{
私人语境;
私有列表项=新的ArrayList();
滑块适配器(上下文、列表项){
这个项目=项目;
this.context=上下文;
}
@凌驾
public int getCount(){
返回items.size();
}
@凌驾
公共布尔值isViewFromObject(视图,对象){
返回(视图==对象);
}
@凌驾
公共对象实例化项(视图组容器,int位置){
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
视图=布局扁平化。充气(R.layout.slideshow\u布局,容器,假);
ImageView img=view.findViewById(R.id.ImageView);
Item=items.get(位置);
if(item.isLink()){
Glide.with(context).load(item.getUrl()).into(img);
}否则{
setImageResource(item.getId());
}
container.addView(视图);
返回视图;
}
@凌驾
公共项(视图组容器、int位置、对象){
container.removeView((LinearLayout)对象);
}
}

在本例中,我没有将两个不同的列表传递给适配器(
List
List
),而是将其更改为接受
对象的列表。如您所见,
对象可以保存URL或可绘制的资源ID。在适配器中,我检查每个项,查看它是什么类型的项,以便使用适当的方法设置
图像视图
的图像。

您可以执行以下操作:

public class Item{
   String url;
   int id;
   boolean link = false;
   boolean resource = false;
   public Item(int id){
       this.id = id;
       resource = true;
   }
   public Item(String url){
       this.url = url;
       link = true;
   }
   public boolean isLink(){
       return link;
   }
   public boolean isResource(){
       return resource;
   }
   public int getId(){
       return id;
   }
   public String getUrl(){
       return url;
   }
}



public class sliderAdapter extends PagerAdapter {

    private Context context;

    private List<Item> items = new ArrayList<>();
    sliderAdapter(Context context, List<Item> items){
        this.items = items;
        this.context = context;
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view== object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.slideshow_layout,container,false);
        ImageView img = view.findViewById(R.id.imageview);
        Item item = items.get(position);
        if(item.isLink()){
            Glide.with(context).load(item.getUrl()).into(img);
        }else{
            img.setImageResource(item.getId());
        }

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout)object);
    }
}
公共类项目{
字符串url;
int-id;
布尔链接=假;
布尔资源=false;
公共项目(int id){
this.id=id;
资源=真;
}
公共项(字符串url){
this.url=url;
link=true;
}
公共布尔isLink(){
返回链接;
}
公共布尔值isResource(){
返回资源;
}
公共int getId(){
返回id;
}
公共字符串getUrl(){
返回url;
}
}
公共类sliderAdapter扩展了PagerAdapter{
私人语境;
私有列表项=新的ArrayList();
滑块适配器(上下文、列表项){
这个项目=项目;
this.context=上下文;
}
@凌驾
public int getCount(){
返回items.size();
}
@凌驾
公共布尔值isViewFromObject(视图,对象){
返回(视图==对象);
}
@凌驾
公共对象实例化项(视图组容器,int位置){
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
视图=布局扁平化。充气(R.layout.slideshow\u布局,容器,假);
ImageView img=view.findViewById(R.id.ImageView);
Item=items.get(位置);
if(item.isLink()){
Glide.with(context).load(item.getUrl()).into(img);
}否则{
setImageResource(item.getId());
}
container.addView(视图);
返回视图;
}
@凌驾
公共项(视图组容器、int位置、对象){
container.removeView((LinearLayout)对象);
}
}
在本例中,我没有将两个不同的列表传递给适配器(
List
List
),而是将其更改为接受
对象的列表。如您所见,
对象可以保存URL或可绘制资源ID。在适配器中,我检查每个项,查看它是什么类型的项,以便使用适当的方法设置
图像视图
的I