无法解析ListView适配器中的getSystemService方法

我正在研究John Horton的初学者Android编程 ,目前正在尝试创建一个笔记应用程序。 Horton刚刚推出了ListViews 。 但是,我遇到了adapter class

 public class NoteAdapter extends BaseAdapter { List mNoteList = new ArrayList(); @Override public int getCount(){ return mNoteList.size(); } @Override public Note getItem(int whichItem){ return mNoteList.get(whichItem); } @Override public long getItemId(int whichItem){ return whichItem; } @Override public View getView(int whichItem, View view, ViewGroup viewGroup){ // check if view has been inflated already if (view == null){ LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); // ERROR HERE view = inflater.inflate(R.layout.listitem, viewGroup, false); } return view; } } 

问题出在getView方法中,我试图给layout inflateAndroid Studio throws an error: 'Cannot resolve getSystemService(java.lang.String)'.

作为一个完整的新人,只是通过这本书,我不知道从这里去哪里或尝试解决它 – 可以有人帮忙吗?

获取LayoutInflater的最佳方法是在Activity上调用getLayoutInflater() 。 这样,活动的主题就被考虑在内了。 如果在Activity内部定义了NoteAdapter ,则只需调用getLayoutInflater() 。 如果NoteAdapter在其自己的单独Java类文件中定义, LayoutInflater通过构造函数传入LayoutInflater

为了更直接地解决您的问题,任何View (如ListView )都可以调用getContext()来获取Context 。 这就是定义getSystemService()地方。 因此,用viewGroup.getContext().getSystemService()替换getSystemService() viewGroup.getContext().getSystemService()将起作用。

使用view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.listitem, viewGroup,false);

您应该将Context传递给适配器,然后替换此行:

  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

我希望这将有所帮助。

为适配器创建一个类变量和一个构造函数:

 Context context; public NoteAdapter(Context context){ this.context = context; } 

然后通过以下方式初始化layoutinflater:

 LayoutInflater inflater = LayoutInflater.from(context); 

尝试

 public class NoteAdapter extends BaseAdapter { Context mContext = null; public NoteAdapter(Context context){ mContext = context; } @Override public View getView(int whichItem, View view, ViewGroup viewGroup){ // check if view has been inflated already if (view == null){ LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); // ERROR HERE view = inflater.inflate(R.layout.listitem, viewGroup, false); } return view; } } 

首先制作适配器的构造函数:如下:

 Context context; public NoteAdapter(Context context) { this.context = context } 

现在使用此上下文:

 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

在我看来,如果你正在学习,那么学习RecyclerView。 bcz它比ListView好。 我并不是说ListView已被删除。 但是有很多内部事物,其中RecyclerView更好。

以下是适配器的示例

 public class NoteAdapter extends BaseAdapter { List mNoteList = new ArrayList(); Context context; public NoteAdapter(Context context){ this.context = context; LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount(){ return mNoteList.size(); } @Override public Note getItem(int whichItem){ return mNoteList.get(whichItem); } @Override public long getItemId(int whichItem){ return whichItem; } @Override public View getView(int whichItem, View view, ViewGroup viewGroup){ // check if view has been inflated already if (view == null){ view = inflater.inflate(R.layout.listitem, viewGroup, false); } return view; } } 

在MainActivity.java中

 NoteAdapter noteA = new NoteAdapter(MainActivity.this); 

要么

 NoteAdapter noteA = new NoteAdapter(getContext()); 

要么

NoteAdapter noteA = new NoteAdapter(getActivity);

//如果在Fragment中

要么

NoteAdapter noteA = new NoteAdapter(getApplicationContext);

//会工作但不需要使用它。 bcz这是整个应用的背景。 对于适配器,您不需要整个应用程序的上下文。

mContext是您传递给Custom Adapter的Context

  public boolean CheckInternet() { ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) { //we are connected to a network return true; } return false; }//end of check internet