2014-10-28 54 views
0

我从Android编程开始。我不能编译我的程序,我得到以下错误:ArrayAdapter():找不到合适的构造函数

Error: no suitable constructor found for ArrayAdapter()

这里是我的personnal适配器:

public class StationListAdapter extends ArrayAdapter<Station> { 

    private ArrayList<Station> stations; 
    private Context context; 
    private int resource; 
    LayoutInflater layoutInflater; 

    //Constructor - args : (context, resourceId, ArrayList<MyClass>) 
    public StationListAdapter(Context context, int resource, ArrayList<Station> stations) { 

     this.context = context; 
     this.resource = resource; 
     this.stations = stations; 
     layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

这里是frament该实例的适配器的一部分:

public void onArrayListReady(ArrayList<Station> stations) { 
    TextView textView = (TextView) this.view.findViewById(R.id.fragment_list_textview); 
    ListView listView = (ListView) this.view.findViewById(R.id.fragment_list_stations_list); 
    adapter = new StationListAdapter(getActivity(), R.layout.fragment_list_row, stations); 
    listView.setAdapter(adapter); 
} 
+1

你不会在你的适配器中调用'super' – Achrome 2014-10-28 23:29:45

+0

非常感谢你的帮助。 – valvince 2014-10-28 23:51:41

回答

1

您需要从构造函数中调用超级构造函数才能正确初始化适配器。

+0

非常感谢!就是这样。 这可能听起来像一个愚蠢的问题,但我怎么知道我必须创建一个构造,并在其中调用父构造函数? (Java和Android初学者) – valvince 2014-10-28 23:49:05

+0

唯一的愚蠢问题是未经询问的问题。你应该假设你正在扩展的类也需要初始化。因此,除非有意阻止超级构造函数调用,否则始终调用超级构造函数是一种很好的做法。 – 2014-10-29 01:42:58

+0

那,我不会放弃。再次感谢。 – valvince 2014-10-29 09:01:21

相关问题