Reputation: 1325
I am working through John Horton's Android Programming for Beginners, and am currently attempting to create a note-taking app. Horton has just introduced ListViews
. However, I am having trouble with the 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;
}
}
The problem is in the getView
method, where I'm attempting to inflate
the layout
: Android Studio throws an error: 'Cannot resolve getSystemService(java.lang.String)'.
As a complete newcomer just following through the book I have no idea where to go from here or what to try to resolve it - can anyone help?
Upvotes: 1
Views: 2380
Reputation: 872
mContext is Context which you pass to Custom Adapter
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
Upvotes: 0
Reputation: 419
In my views, if you are learning then learn RecyclerView. bcz it is better than ListView. i am not saying that ListView has been depricated. But there alot of internal things in which RecyclerView is better.
Following is example of Adapter
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;
}
}
Inside MainActivity.java
NoteAdapter noteA = new NoteAdapter(MainActivity.this);
OR
NoteAdapter noteA = new NoteAdapter(getContext());
OR
NoteAdapter noteA = new NoteAdapter(getActivity);
// if in Fragment
OR
NoteAdapter noteA = new NoteAdapter(getApplicationContext);
// will work but no need to use it. bcz this is context of whole application. For an adapter you don't need context of whole application.
Upvotes: 0
Reputation: 1007399
The best way to get a LayoutInflater
is by calling getLayoutInflater()
on an Activity
. That way, the activity's theme is taken into account. If NoteAdapter
is defined inside of an Activity
, just call getLayoutInflater()
. If NoteAdapter
is defined in its own separate Java class file, pass in a LayoutInflater
via the constructor.
To more directly address your question, any View
, like ListView
, can call getContext()
to get a Context
. That is where getSystemService()
is defined. So, replacing getSystemService()
with viewGroup.getContext().getSystemService()
would work.
Upvotes: 4
Reputation: 51
Use
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.listitem, viewGroup,false);
Upvotes: 1
Reputation: 1114
First make the constructor of Adapter: like follow :
Context context;
public NoteAdapter(Context context)
{
this.context = context
}
Now use this context:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Upvotes: 0
Reputation: 297
Try
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;
}
}
Upvotes: 0
Reputation: 1388
Create a class variable and a Constructor for your adapter:
Context context;
public NoteAdapter(Context context){
this.context = context;
}
Then initialize the layoutinflater the following way:
LayoutInflater inflater = LayoutInflater.from(context);
Upvotes: 0
Reputation: 593
You should pass Context to your adapter and then replace this line:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
I hope this will help.
Upvotes: 1