简体   繁体   English

列表适配器中的自定义类

[英]Custom Class in List Adapter

I'm having some trouble understanding how scope is affecting my code. 我在理解范围如何影响我的代码时遇到了一些麻烦。 I can't seem to access the public attributes of a public class. 我似乎无法访问公共类的公共属性。

I created a custom class ArtistPacket which has a block of information I would like to send to my custom adapter ( ArtistListAdapter ). 我创建了一个自定义类ArtistPacket ,该类具有要发送到自定义适配器( ArtistListAdapter )的信息块。

The custom class is below: 自定义类如下:

public class ArtistPacket{

    public String name;
    public int id;

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

}

It is defined in MainActivityFragment , where I create an ArtistListAdapter which takes these ArtistPackets . 它在MainActivityFragment中定义,在这里我创建一个ArtistListAdapter这些ArtistPackets

public class MainActivityFragment extends Fragment{

...

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

...

I then define the ArtistListAdapter and getView 然后,我定义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) {

...

In the getView , I need the name and id from the ArtistPacket object (in this case artist ). getView ,我需要ArtistPacket对象(在本例中为artist )的nameid So I try calling 所以我尝试打电话

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

But I get a compile error. 但是我得到一个编译错误。 In the debugger, it appears as if the full object is coming through - it just doesn't seem like the Adapter access the name or id attribute. 在调试器中,看起来好像是整个对象都通过了-似乎适配器不访问nameid属性。

The error I get is: 我得到的错误是:

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

Is there an issue with scope in my implementation? 我的实现范围存在问题吗? Why would the Adapter not be able to see the contents of the ArtistPacket object if it is plainly seen in the debugger? 如果适配器在调试器中清楚可见,为什么适配器不能看到ArtistPacket对象的内容?

Here's the full getView: 这是完整的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;
    }

Subtle yet important answer to this. 微妙而重要的答案。

The following class definition: 下面的类定义:

private class ArtistListAdapter<ArtistPacket> extends ArrayAdapter<ArtistPacket>

can be broken up to be better understood. 可以分解以更好地理解。

ArtistListAdapter<ArtistPacket>

Implies that the ArtistListAdapter is defining the type argument as ArtistPacket . 表示ArtistListAdapter将类型参数定义ArtistPacket This means that anytime ArtistPacket is referenced, it is referencing this type declaration - not the class defined above. 这意味着只要引用了ArtistPacket,便会引用此类型声明-而不是上面定义的类。

On the other hand, 另一方面,

extends ArrayAdapter<ArtistPacket>

Implies that the ArtistListAdapter extends an ArrayAdapter which uses the aforementioned ArtistPacket class. 表示ArtistListAdapter扩展了一个使用上述ArtistPacket类的ArrayAdapter

In other words, the first <> is about defined types, while the second <> is about used types. 换句话说,第一个<>与定义的类型有关,而第二个<>与已使用的类型有关。

Thus, I used the following declaration: 因此,我使用了以下声明:

private class ArtistListAdapter extends ArrayAdapter<ArtistPacket>

Which implies the ArrayAdapter will be extending the ArtistListAdapter using the type ArtistPacket - without confusing the situation by defining it's own, local, ArtistPacket type. 这意味着ArrayAdapter将使用ArtistListAdapter类型ArtistPacket不会通过定义其自身的本地ArtistPacket类型而混淆情况。

Source 资源

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

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