i'm new to dependency injection and Dagger 2 library for android mainly my question is in 2 parts the first is it a good practice to use dagger 2 to set a list for my custom adapter say i have like this
package com.example.a3adel.vogellaexample;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import javax.inject.Inject;
public class ListAdapter extends BaseAdapter{
LayoutInflater inflater;
@Inject
ArrayList<String> listArray;
public ListAdapter(Activity context){
MyApplication app = (MyApplication) context.getApplication();
app.getComponent().inject(this);
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return listArray.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=inflater.inflate(R.layout.listitem,parent,false);
return view;
}
}
and i initialize the arraylist in the main activity like this
@Provides
@Singleton
ArrayList<String> getList() {
ArrayList<String> list = new ArrayList<>();
list.add("test");
list.add("test2");
list.add("test3");
return list;
}
i have my component like this
@Singleton
@Component(modules = {MainActivity.class})
public interface DiComponent {
void inject(ListAdapter adapter);
}
and if it's a good practice is this the best way to be done ?? thanks
Aucun commentaire:
Enregistrer un commentaire