2016-04-12 64 views
0

我正在通過約翰霍頓的Android編程初學者,我目前正在嘗試創建一個筆記應用程序。霍頓剛剛推出ListViews。但是,我有麻煩adapterclass無法解析ListView適配器中的getSystemService方法

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方法,在這裏我試圖inflatelayoutAndroid Studio throws an error: 'Cannot resolve getSystemService(java.lang.String)'.

作爲一個完整的新手剛剛通過這本書以下我不知道該從哪裏出發或想要解決什麼問題 - 任何人都可以幫忙嗎?

+0

這基本上意味着它不能在當前類(NoteAdapter)或超類(BaseAdapter)找到getSystemService。你是否在文件的頂部導入了BaseAdapter?你能看到BaseAdapter的來源嗎? –

+0

夥計們,我是個白癡。我把這個類放在一個單獨的文件中,但它應該在_MainActivity_中。 (-‸ლ)。當你把它放在那裏時,工作正常。感謝您的答案。 – flurpleplurple

回答

3

獲得LayoutInflater的最佳方法是致電getLayoutInflater(),致電Activity。這樣,活動的主題就被考慮進去了。如果NoteAdapterActivity內定義,請致電getLayoutInflater()。如果NoteAdapter在其自己的獨立Java類文件中定義,則通過構造函數傳入LayoutInflater

要更直接地解決您的問題,任何View,如ListView,可以撥打getContext()得到Context。這就是定義getSystemService()的地方。所以,用viewGroup.getContext().getSystemService()代替getSystemService()就行了。

+0

謝謝你的答案。這對我有效 – Smaran

0

你應該通過語境你的適配器,然後替換此行:

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

我希望這將有助於。

0

創建一個類變量和構造您的適配器:

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

然後初始化layoutinflater方式如下:

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

//如果在片段

OR

NoteAdapter noteA = new NoteAdapter(getApplicationContext);

//將工作,但不需要使用它。 bcz這是整個應用程序的上下文。對於適配器,您不需要整個應用程序的上下文。

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

} 
0

首先使適配器的構造:像如下:

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

現在使用此背景:

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

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

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