Java 无法解析ListView适配器中的getSystemService方法
我正在研究John Horton的Android初学者编程,目前正在尝试创建一个记笔记应用程序。霍顿刚刚推出了Java 无法解析ListView适配器中的getSystemService方法,java,android,listview,Java,Android,Listview,我正在研究John Horton的Android初学者编程,目前正在尝试创建一个记笔记应用程序。霍顿刚刚推出了列表视图。但是,适配器类有问题: public class NoteAdapter extends BaseAdapter { List<Note> mNoteList = new ArrayList<Note>(); @Override public int getCount(){ return mNoteList.
列表视图
。但是,适配器
类
有问题:
public class NoteAdapter extends BaseAdapter {
List<Note> mNoteList = new ArrayList<Note>();
@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;
}
}
公共类NoteAdapter扩展了BaseAdapter{
List mNoteList=new ArrayList();
@凌驾
public int getCount(){
返回mNoteList.size();
}
@凌驾
公共注释getItem(int whichItem){
返回mNoteList.get(whichItem);
}
@凌驾
公共长getItemId(int-whichItem){
返回whichItem;
}
@凌驾
公共视图getView(int whichItem、视图视图、视图组视图组){
//检查视图是否已充气
如果(视图==null){
LayoutFlater充气器=(LayoutFlater)getSystemService(Context.LAYOUT\u充气器\u服务);//此处出错
视图=充气机。充气(R.layout.listitem,viewGroup,false);
}
返回视图;
}
}
问题出在getView
方法中,我正在尝试充气
布局:Android Studio抛出一个错误:“无法解析getSystemService(java.lang.String)”。
作为一个刚刚看完这本书的新手,我不知道从这里走到哪里,也不知道如何解决它-有人能帮忙吗?获得LayoutInflater
的最佳方法是在活动上调用getLayoutInflater()
。这样,活动的主题就得到了考虑。如果NoteAdapter
是在活动中定义的,只需调用getLayoutFlater()
。如果NoteAdapter
是在其单独的Java类文件中定义的,则通过构造函数传入LayoutInflater
为了更直接地回答您的问题,任何视图
,如列表视图
,都可以调用getContext()
来获取上下文
。这就是定义getSystemService()
的地方。因此,将getSystemService()
替换为viewGroup.getContext().getSystemService()
是可行的。获取LayoutInflater
的最佳方法是对活动调用getLayoutInflater()
。这样,活动的主题就得到了考虑。如果NoteAdapter
是在活动中定义的,只需调用getLayoutFlater()
。如果NoteAdapter
是在其单独的Java类文件中定义的,则通过构造函数传入LayoutInflater
为了更直接地回答您的问题,任何视图
,如列表视图
,都可以调用getContext()
来获取上下文
。这就是定义getSystemService()
的地方。因此,将getSystemService()
替换为viewGroup.getContext().getSystemService()
会起作用。您应该将上下文传递给适配器,然后替换此行:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
我希望这会有所帮助。您应该将上下文传递给适配器,然后替换此行:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
我希望这会有所帮助。为适配器创建类变量和构造函数:
Context context;
public NoteAdapter(Context context){
this.context = context;
}
然后按以下方式初始化LayoutFlater:
LayoutInflater inflater = LayoutInflater.from(context);
为适配器创建类变量和构造函数:
Context context;
public NoteAdapter(Context context){
this.context = context;
}
然后按以下方式初始化LayoutFlater:
LayoutInflater inflater = LayoutInflater.from(context);
在我看来,如果你正在学习,那么就学习RecyclerView。bcz它比ListView好。我并不是说ListView被抹黑了。但在很多内部事物中,RecyclerView更好
下面是适配器的示例
public class NoteAdapter extends BaseAdapter {
List<Note> mNoteList = new ArrayList<Note>();
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;
}
}
或
或
NoteAdapter noteA=新的NoteAdapter(getActivity)代码>
//如果是碎片
或
NoteAdapter noteA=新的NoteAdapter(getApplicationContext)代码>
//将工作,但不需要使用它。bcz这是整个应用程序的上下文。对于适配器,您不需要整个应用程序的上下文。在我看来,如果您正在学习,请学习RecyclerView。bcz它比ListView好。我并不是说ListView被抹黑了。但在很多内部事物中,RecyclerView更好
下面是适配器的示例
public class NoteAdapter extends BaseAdapter {
List<Note> mNoteList = new ArrayList<Note>();
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;
}
}
或
或
NoteAdapter noteA=新的NoteAdapter(getActivity)代码>
//如果是碎片
或
NoteAdapter noteA=新的NoteAdapter(getApplicationContext)代码>
//将工作,但不需要使用它。bcz这是整个应用程序的上下文。对于适配器,您不需要整个应用程序的上下文。试试看
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);
首先创建适配器的构造函数:如下所示:
Context context;
public NoteAdapter(Context context)
{
this.context = context
}
现在使用以下上下文:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
使用
view=LayoutInflater.from(viewGroup.getContext()).flate(R.layout.listitem,viewGroup,false)代码>使用
view=LayoutInflater.from(viewGroup.getContext()).flate(R.layout.listitem,viewGroup,false)代码>mContext是传递给自定义适配器的上下文
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
mContext是传递给自定义适配器的上下文
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
这基本上意味着它在当前类(NoteAdapter)或超类(BaseAdapter)中找不到getSystemService。是否已在文件顶部导入BaseAdapter?你能看到BaseAdapter的来源吗?伙计们,我是个白痴。我把这个类放在一个单独的文件中,但它应该在MainActivity中。(-‸ლ). 当你把它放在那里时,它可以正常工作。谢谢你的回答。这基本上意味着它在当前类(NoteAdapter)或超类(BaseAdapter)中找不到getSystemService.你在文件顶部导入BaseAdapter了吗?你能看到BaseAdapter的来源吗?伙计们,我是个傻瓜。我把这个类放在一个单独的文件中,但它应该在MainActivity中(?