【问题标题】:Custom Class in List Adapter列表适配器中的自定义类
【发布时间】:2015-07-01 16:01:16
【问题描述】:

我在理解范围如何影响我的代码时遇到了一些麻烦。我似乎无法访问公共类的公共属性。

我创建了一个自定义类 ArtistPacket,其中包含我想发送到我的自定义适配器 (ArtistListAdapter) 的信息块。

自定义类如下:

public class ArtistPacket{

    public String name;
    public int id;

    public ArtistPacket(String name, int id){
        this.name = name;
        this.id = id;
    }

}

它在MainActivityFragment 中定义,我在其中创建了一个ArtistListAdapter,它采用这些ArtistPackets

public class MainActivityFragment extends Fragment{

...

ArtistListAdapter<ArtistPacket> artistListAdapter  = 
  new ArtistListAdapter<ArtistPacket>(getActivity(), artistData);

...

然后我定义ArtistListAdaptergetView

private class ArtistListAdapter<ArtistPacket> extends ArrayAdapter<ArtistPacket>{

    public ArtistListAdapter(Context context,ArrayList<ArtistPacket> artists){
        super(getActivity(),0,artists);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

...

getView 中,我需要来自ArtistPacket 对象的nameid(在本例中为artist)。所以我试着打电话

ArtistPacket artist = getItem(position);    
textItemContent.setText((CharSequence) artist.name);

但是我得到一个编译错误。在调试器中,似乎完整的对象正在通过 - 似乎适配器没有访问 nameid 属性。

我得到的错误是:

Error:(98, 58) error: cannot find symbol variable name
where ArtistPacket is a type-variable:
ArtistPacket extends Object declared in class      
  MainActivityFragment.ArtistListAdapter

我的实施中是否存在范围问题?如果 ArtistPacket 对象在调试器中清晰可见,为什么 Adapter 看不到它的内容?

这是完整的 getView:

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        // Find the artist packet at a given position
        ArtistPacket artist = getItem(position);

        if (view == null) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }

        TextView textItemContent = (TextView) view.findViewById(R.id.list_item_content);
        ImageView imageViewContent = (ImageView) view.findViewById(R.id.list_item_image);

        textItemContent.setText((CharSequence) artist.name);
        imageViewContent.setImageResource(artist.id);

        return view;
    }

【问题讨论】:

  • 您是从艺术家列表中提取 ArtistPacket 实例吗?
  • 你能展示一下你是如何创建对象artist的吗?
  • 你能发布整个 getView 吗?你得到什么样的错误
  • 我认为 ArtistPacket 是从 getItem(position) 方法中提取的,并且正确的对象显示在调试器中。我认为范围有问题?编译器允许我按照我认为合适的方式操作对象 - 只是无法访问它的属性。

标签: java android oop listadapter


【解决方案1】:

对此的微妙而重要的答案。

以下类定义:

private class ArtistListAdapter<ArtistPacket> extends ArrayAdapter<ArtistPacket>

可以分解以便更好地理解。

ArtistListAdapter<ArtistPacket>

暗示ArtistListAdapter定义类型参数为ArtistPacket。这意味着无论何时引用 ArtistPacket,它都在引用这个类型声明——而不是上面定义的类。

另一方面,

extends ArrayAdapter<ArtistPacket>

暗示ArtistListAdapter 扩展了ArrayAdapter,它使用上述ArtistPacket 类。

换句话说,第一个是关于定义类型,而第二个是关于使用类型。

因此,我使用了以下声明:

private class ArtistListAdapter extends ArrayAdapter<ArtistPacket>

这意味着ArrayAdapter 将使用ArtistPacket 类型扩展ArtistListAdapter - 通过定义它自己的本地ArtistPacket 类型不会混淆情况。

Source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 2011-02-14
    • 2011-12-31
    • 1970-01-01
    • 2012-02-21
    相关资源
    最近更新 更多