我有一个 ListView 和一个自定义适配器。我想更改 MyListAdapter 对象中的变量。 这是我的自定义列表适配器中的一些代码:
public class MyListAdapter extends ArrayAdapter<Something> {
........
........
private int myPosition= -1;
........
public void setMyPosition(int pos) {
myPosition = pos;
}
................
}
现在在我的 Activity 中:
MyListAdapter<Something> listAdapter = new MyListAdapter(this,
R.layout.list_item_transaction, accounts);
listAdapter.setMyPosition(2);
但我无法访问 setMyPosition 方法。 在 Eclipse 中,我得到The method setMyPosition() is undefined for the type ArrayAdapter
最佳答案
将 listAdapter 声明为 MyListAdapter 类型。否则它不存在,如果父类中不存在,Java 就无法调用子类的函数。
这仅在 TransListAdapter 是子类或 MyListAdapter 时才有效。否则 setMyPosition 函数也不存在。
关于java - 访问自定义ArrayAdapter中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16721075/