Roboguice injection into adapter

I have an adapter that extends the ArrayAdapter<T> and wants to insert a LayoutInflater into it. The code is below, but the blower is always null

 public abstract class MyAdapter<T> extends ArrayAdapter<T> { @Inject protected LayoutInflater inflater; @Override public View getView(int position, View convertView, ViewGroup parent) { // inflater here is null } } 
+4
source share
2 answers

You may have created an instance of MyAdapter with new instead of typing it.

In this case, I would not recommend introducing LayoutInflater if you do not want to use different implementations of this class, an example is mock LayoutInflater for testing.

Get an instance in the constructor:

 inflater = LayoutInflater.from(context); 

That would be more efficient. I do not see any benefit from LayoutInflater injections.
Enabling dependencies is fine, but don't use it when it's not needed and slower.

+5
source

Invests in any class

  RoboGuice.getInjector(context).injectMembers(this); 

use in the constructor, you just need context, great work for me

+5
source

Source: https://habr.com/ru/post/1434156/


All Articles