简体   繁体   English

访问自定义ArrayAdapter中的方法

[英]Access a method in custom ArrayAdapter

I have a ListView and a custom adapter. 我有一个ListView和一个自定义适配器。 I want to change a variable in MyListAdapter object. 我想更改MyListAdapter对象中的变量。 This is some code from my custom list Adapter: 这是我的自定义列表适配器中的一些代码:

public class MyListAdapter extends ArrayAdapter<Something> {
    ........
    ........
    private int myPosition= -1;
    ........

    public void setMyPosition(int pos) {
        myPosition = pos;
    }
................
}

now in my Activity: 现在在我的活动中:

MyListAdapter<Something> listAdapter = new MyListAdapter(this,
                R.layout.list_item_transaction, accounts);

listAdapter.setMyPosition(2);

But I cannot access setMyPosition method. 但是我无法访问setMyPosition方法。 In Eclipse I get The method setMyPosition() is undefined for the type ArrayAdapter 在Eclipse中,我得到了ArrayAdapter类型的setMyPosition()方法未定义

Declare listAdapter to be of type MyListAdapter. 将listAdapter声明为MyListAdapter类型。 Otherwise it doesn't exist, Java can't call a function of a subclass if it doesn't exist on the parent. 否则它就不存在,如果父类中不存在子类的函数,Java将无法调用它。

This will only work if TransListAdapter is a subclass or MyListAdapter. 仅当TransListAdapter是子类或MyListAdapter时,这才起作用。 Otherwise the setMyPosition function won't exist there either. 否则,setMyPosition函数也不存在。

Try this : 尝试这个 :

public class MyListAdapter<Something> extends ArrayAdapter<Something> {
    ........
    ........
    private int myPosition= -1;
    ........

    public void setMyPosition(int pos) {
        myPosition = pos;
    }
    ........
}

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

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