在适配器接口使用 [英] Interface use in Adapter

查看:212
本文介绍了在适配器接口使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是使用接口的自定义适配器。下面是构造函数,请让我知道这是错误的:

 公共ImageAdapter(上下文C,ArrayList的<&位图GT;的图像,ArrayList的文件夹名,ArrayList的<布尔> isFolder,FolderClickListener听众){        this.context = C;
        this.images =图像;
        this.folderName = FOLDERNAME;
        this.isFolder = isFolder;
       checkBoxState =新的布尔[images.size()];
        // ImageLoader的= ImageLoader.getInstance();
        mListener =侦听器;    }

然后在我的 GetView 在适配器中,我调用接口是这样的:

  mListener.folderClicked(位置); //使用空指针异常失败

下面是我的界面:

 公共接口FolderClickListener {    无效folderClicked(INT位置);}

然后我有我的MainActivity一种方法,其实现的接口:

  @覆盖
    公共无效folderClicked(INT位置){//做的东西这里
}

为什么我收到空指针异常任何想法?
  编辑:这是我在MainActivity适配器,我怎么能接口传递到这个

 适配器=新ImageAdapter(TextDropboxActivity.this,PIX,路径,文件夹);
            lstView.setAdapter(适配器);


解决方案

c您不使用,因为它应该是构造函数根据你的$ C $。

首先,确保你的TextDropboxActivity实施监听。如果是的话,你会看到重写方法。

二,你的新的适配器应该是这样的:

 适配器=新ImageAdapter(TextDropboxActivity.this,PIX,路径,文件夹,TextDropboxActivity.this);
        lstView.setAdapter(适配器);

您的适配器构造code:

 公共ImageAdapter(上下文C,ArrayList的<&位图GT;的图像,ArrayList的文件夹名,ArrayList的<布尔> isFolder,FolderClickListener听众){    this.context = C;
    this.images =图像;
    this.folderName = FOLDERNAME;
    this.isFolder = isFolder;
   checkBoxState =新的布尔[images.size()];
    // ImageLoader的= ImageLoader.getInstance();
    mListener =侦听器;}

第一TextDropboxActivity.this是上下文,并在我的例子,第二个是一样的听众。

另外,还要确保你调用

  mListener.folderClicked(位置);

按钮按下之后。你也可以尝试发送硬codeD值如

  mListener.folderClicked(1);

和您登录活动code得到什么检查,如果一切正常。
例如:

  @覆盖
公共无效folderClicked(INT位置){//做的东西这里
Log.i(测试,+位置);
}

如果其工作,听者运作良好,问题是你是如何发送行的位置。

I have a custom adapter which is using an Interface. Here are the constructors, please let me know if this is wrong:

  public ImageAdapter(Context c, ArrayList<Bitmap> images, ArrayList folderName, ArrayList<Boolean> isFolder, FolderClickListener listener){

        this.context = c;
        this.images = images;
        this.folderName = folderName;
        this.isFolder=isFolder;
       checkBoxState=new boolean[images.size()];
        //imageLoader = ImageLoader.getInstance();
        mListener = listener;

    }

Then in my GetView in the adapter, I call the interface like this:

 mListener.folderClicked(position); //Fails with null pointer exception

Here is my Interface:

public interface FolderClickListener {

    void folderClicked(int position);

}

I then have a method in my MainActivity which implements the Interface:

@Override
    public void folderClicked(int position) {

//do stuff in here
}

Any ideas why I am getting the null pointer exception? EDIT: Here is my adapter in MainActivity, how can I pass the interface to this?

adapter = new ImageAdapter(TextDropboxActivity.this, pix, paths, folders);
            lstView.setAdapter(adapter);

解决方案

According to your code you are not using the constructor as it should be.

First, make sure your TextDropboxActivity Implement the listener. if yes you will see the override method.

Second, your new adapter should look like this:

adapter = new ImageAdapter(TextDropboxActivity.this, pix, paths, folders,TextDropboxActivity.this);
        lstView.setAdapter(adapter);

your adapter constructor code:

public ImageAdapter(Context c, ArrayList<Bitmap> images, ArrayList folderName, ArrayList<Boolean> isFolder,    FolderClickListener listener){

    this.context = c;
    this.images = images;
    this.folderName = folderName;
    this.isFolder=isFolder;
   checkBoxState=new boolean[images.size()];
    //imageLoader = ImageLoader.getInstance();
    mListener = listener;

}

The first TextDropboxActivity.this is the context, and the second one like in my example is the listener.

Also make sure you calling

mListener.folderClicked(position);

only after button clicked. you can also try sending hard coded value like

 mListener.folderClicked(1);

And log what you get in the activity code to check if everything is ok. For example:

@Override
public void folderClicked(int position) {

//do stuff in here
Log.i("test",""+position);
}

If its work, listener working well and the problem is with how are you sending the row position.

这篇关于在适配器接口使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆