【问题标题】:Cannot resolve getSystemService method in ListView adapter无法解析 ListView 适配器中的 getSystemService 方法
【发布时间】:2016-04-12 13:12:04
【问题描述】:

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

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;
    }

}

问题出在getView 方法中,我正在尝试inflate layoutAndroid Studio throws an error: 'Cannot resolve getSystemService(java.lang.String)'.

作为一个刚看完这本书的新手,我不知道从哪里开始或尝试解决什么问题 - 有人可以帮忙吗?

【问题讨论】:

  • 这基本上意味着它在当前类(NoteAdapter)或超类(BaseAdapter)中找不到getSystemService。您是否在文件顶部导入了 BaseAdapter?你能看到BaseAdapter的来源吗?
  • 伙计们,我是个白痴。我把这个类放在一个单独的文件中,但它应该在 MainActivity 中。 (-‸ლ)。当你把它放在那里时工作正常。感谢您的回答。

标签: java android listview


【解决方案1】:

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

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

【讨论】:

  • 感谢您的帖子,但我的条件是在片段中获取系统服务,尽管您的帖子在我的片段 this.getActivity().getSystemService(DOWNLOAD_SERVICE) 中给了我一个提示,可以像这样解决问题
【解决方案2】:

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

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

我希望这会有所帮助。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

      然后按如下方式初始化layoutinflater:

      LayoutInflater inflater = LayoutInflater.from(context);
      

      【讨论】:

        【解决方案5】:

        试试

        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;
            }
        
        }
        

        【讨论】:

          【解决方案6】:

          首先制作Adapter的构造函数:如下:

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

          现在使用这个上下文:

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

          【讨论】:

            【解决方案7】:

            在我看来,如果您正在学习,请学习 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;
                }
            
            } 
            

            MainActivity.java 内部

            NoteAdapter noteA = new NoteAdapter(MainActivity.this);
            

            NoteAdapter noteA = new NoteAdapter(getContext());
            

            NoteAdapter noteA = new NoteAdapter(getActivity);

            //如果在片段中

            NoteAdapter noteA = new NoteAdapter(getApplicationContext);

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

            【讨论】:

              【解决方案8】:

              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
              

              【讨论】:

                猜你喜欢
                • 2019-02-13
                • 2015-05-29
                • 1970-01-01
                • 2023-03-13
                • 1970-01-01
                • 2017-04-20
                • 1970-01-01
                • 1970-01-01
                • 2019-04-13
                相关资源
                最近更新 更多