Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.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
Java 无法解析ListView适配器中的getSystemService方法_Java_Android_Listview - Fatal编程技术网

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

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.

我正在研究John Horton的Android初学者编程,目前正在尝试创建一个记笔记应用程序。霍顿刚刚推出了
列表视图
。但是,
适配器
有问题:

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 inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

我希望这会有所帮助。

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

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这是整个应用程序的上下文。对于适配器,您不需要整个应用程序的上下文。

试试看

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);
使用
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

这基本上意味着它在当前类(NoteAdapter)或超类(BaseAdapter)中找不到getSystemService。是否已在文件顶部导入BaseAdapter?你能看到BaseAdapter的来源吗?伙计们,我是个白痴。我把这个类放在一个单独的文件中,但它应该在MainActivity中。(-‸ლ). 当你把它放在那里时,效果很好。谢谢你的回答。谢谢你的帖子,但我的条件是在片段中获取SystemServices,尽管你的帖子给了我一个提示,让我在片段this.getActivity().getSystemService(下载服务)中这样做
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  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