Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中访问新的自定义BaseAdapter函数_Android_Inheritance_Baseadapter_Horizontallistview - Fatal编程技术网

在Android中访问新的自定义BaseAdapter函数

在Android中访问新的自定义BaseAdapter函数,android,inheritance,baseadapter,horizontallistview,Android,Inheritance,Baseadapter,Horizontallistview,在我的主要活动中,我有一个calendarListView,类型为HorizontalListView(自定义) 为此设置的适配器如下所示: calendarListView.setAdapter(new CustomCalendarAdapter(this, listArrayX, listArrayY)); 这很好,我可以访问CustomCalendarAdapter中的所有函数,因为默认情况下它们只是覆盖基本适配器 由于适配器被传递到listview类中,因此HorizontalList

在我的主要活动中,我有一个calendarListView,类型为
HorizontalListView
(自定义)

为此设置的适配器如下所示:

calendarListView.setAdapter(new CustomCalendarAdapter(this, listArrayX, listArrayY));
这很好,我可以访问CustomCalendarAdapter中的所有函数,因为默认情况下它们只是覆盖基本适配器

由于适配器被传递到listview类中,因此HorizontalListView本身对CustomCalendarAdapter一无所知,即只要基本适配器类中存在函数,它就可以正常构建

现在,在自定义日历适配器中,我添加了一个新函数

例如:

public class CustomCalendarAdapter extends BaseAdapter{

...

public int getItemDate()
{
    return 5;
}
如何从HorizontalListView中访问该函数

我正在尝试使用mAdapter.getItemDate

(mAdapter在setAdapter函数中设置)

正如我提到的,适配器是在setAdapter中传递的,因此在构建时不理解CustomCalendarAdapter中的函数

我能想到的唯一方法是将CustomCalendarAdapter硬连接到HorizontalListView,我不希望这样做

这样做,即将ListAdapter的所有实例更改为CustomCalendarAdapter可以正常工作,但我认为这不是一种明智的工作方式


非常感谢您的帮助。

我要做的是将自定义功能放入一个界面中

public interface CustomAdapterFunctions {
    int getItemDate();
}
然后在setAdapter方法中,将ListAdapter强制转换为CustomAdapterFunctions

try {
    CustomAdapterFunctions custom = (CustomAdapterFunctions) listAdapter;
catch (ClassCastException ex){
    throw new ClassCastException("Adapters must implement CustomAdapterFunctions!");
}

在将此接口与CalendarListView一起使用之前,请确保您使用的任何适配器都实现了此接口。

您可以保留对适配器的引用,但问题在于构建,因为listview需要ListAdapter类型的项,因此,它无法在生成时查看自定义函数。
BaseAdapter
是一个
ListAdapter
,因此您试图从CalendarListView访问CustomCalendarAdapter中的自定义功能?谢谢。我从没想过!:)没问题!界面很棒!
try {
    CustomAdapterFunctions custom = (CustomAdapterFunctions) listAdapter;
catch (ClassCastException ex){
    throw new ClassCastException("Adapters must implement CustomAdapterFunctions!");
}