In your code
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
The getLayoutInflater () method cannot be used in the adapter's inner class. Therefore, you should try to create a variable to have an inflatable layout object of the parent class and, therefore, access it, or you can send it in the context of the calling class in the adapter class constructor.
Therefore, you can use either like this:
public class ActivityMainWish extends Activity { LayoutInflate inflater; private List<Wish> myWishs = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { inflater=getLayoutInflater() ... }
and use this inflater variable here: itemView = inflater.inflate (R.layout.item_view, parent, false);
Or like this:
private class MyListAdapter extends ArrayAdapter<Wish>{ private Context mContext; public MyListAdapter(Context ctx){ super(ActivityMainWish.this, R.layout.item_view, myWishs); mContext=ctx; } @Override public View getView(int position, View convertView, ViewGroup parent) { View itemView = convertView; if (itemView==null) itemView = LayoutInflater.from(mContext).inflate(R.layout.item_view, parent, false);
and send the Context object, for example:
ArrayAdapter<Wish> adapter = new MyListAdapter(getApplicationContext());
source share