无法解析ListView适配器

时间:2016-04-12 13:12:05

标签: java android listview

我正在通过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)'.

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

8 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

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

我希望这会有所帮助。

答案 3 :(得分:0)

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

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

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

LayoutInflater inflater = LayoutInflater.from(context);

答案 4 :(得分:0)

在我看来,如果你正在学习,那么学习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);

OR

NoteAdapter noteA = new NoteAdapter(getContext());

OR

NoteAdapter noteA = new NoteAdapter(getActivity);

//如果在Fragment中

OR

NoteAdapter noteA = new NoteAdapter(getApplicationContext);

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

答案 5 :(得分:0)

尝试

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 :(得分:0)

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

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

现在使用此上下文:

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

答案 7 :(得分:0)

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