0
我學習依賴注入在我的android代碼中使用Dagger2,我沒有任何麻煩,直到我嘗試注入演示者在我的CustomAdapter ListView中,它總是返回空值。目的是當有人點擊ListView項目時,我想執行Presenter功能。這是我CustomAdapter樣子:Android Dagger2依賴注入CustomAdapter
public class CustomAdapter extends BaseAdapter {
ArrayList<String> contactNameList, listGroup;
Context context;
ArrayList<Bitmap> contactImages;
ArrayList<Integer> listContactId;
private static LayoutInflater inflater=null;
@Inject
DetailScreenPresenter detailScreenPresenter;
public CustomAdapter(MainActivity mainActivity, ArrayList<String> contactNameList, ArrayList<Bitmap> contactImages, ArrayList<String> listGroup, ArrayList<Integer> listContactId)
{
this.contactNameList = contactNameList;
context = mainActivity;
this.contactImages = contactImages;
this.listGroup = listGroup;
this.listContactId = listContactId;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return contactNameList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class Holder
{
public Holder(View view,Context context)
{
ButterKnife.bind(this,view);
}
@BindView(R.id.txtName)
TextView tvName;
@BindView(R.id.txtGroup)
TextView tvGroup;
@BindView(R.id.imageProfile)
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView;
rowView = inflater.inflate(R.layout.contact_list, null);
Holder holder=new Holder(rowView, context);
holder.tvName.setText(contactNameList.get(position));
holder.img.setImageBitmap(contactImages.get(position));
holder.tvGroup.setText(listGroup.get(position));
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
detailScreenPresenter.loadDetail(listContactId.get(position));
}
});
return rowView;
}
}
,這是觸發CustomAdapter
public class MainActivity extends AppCompatActivity implements MainScreenContact.View {
@BindView(R.id.listContact)
ListView listView;
ArrayList<String> listName;
ArrayList<Bitmap> listImage;
ArrayList<String> listGroup;
ArrayList<Integer> listContactId;
HashMap<Integer, Bitmap> tempImageArray;
String[] letter={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
@Inject
MainScreenPresenter mainPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
listName = new ArrayList<>();
listImage = new ArrayList<>();
tempImageArray = new HashMap<>();
listGroup = new ArrayList<>();
listContactId = new ArrayList<>();
DaggerMainScreenComponent.builder()
.netComponent(((App) getApplicationContext()).getNetComponent())
.mainScreenModule(new MainScreenModule(this, new ContactDatabaseHelper(this)))
.build().inject(this);
mainPresenter.loadPost();
}
@Override
public void showPosts(List<Post> posts) {
Collections.sort(posts, new Comparator<Post>() {
@Override
public int compare(Post post1, Post post2) {
String name1=post1.getFirstName()+" "+post1.getLastName();
String name2=post2.getFirstName()+" "+post2.getLastName();
return name1.compareToIgnoreCase(name2);
}
});
int indexGroup=0;
for (int i = 0; i < posts.size(); i++) {
if(posts.get(i).getProfilePic().contains("http"))
mainPresenter.loadImage(posts.get(i).getProfilePic(), i);
else
mainPresenter.loadImage("http://example.com", i);
if(Arrays.binarySearch(letter,Character.toString(posts.get(i).getFirstName().charAt(0)).toUpperCase())==-1)
{
if(listGroup.size()==0)
listGroup.add("*");
else
listGroup.add(" ");
}
else
{
if(!listGroup.get(indexGroup).equalsIgnoreCase(Character.toString(posts.get(i).getFirstName().charAt(0)))) {
listGroup.add(Character.toString(posts.get(i).getFirstName().charAt(0)).toUpperCase());
indexGroup = i;
}
else
listGroup.add(" ");
}
listContactId.add(i, posts.get(i).getId());
listName.add(i, posts.get(i).getFirstName() + " " + posts.get(i).getLastName());
}
}
@Override
public void showError(String message) {
Toast.makeText(getApplicationContext(), "Error" + message, Toast.LENGTH_LONG).show();
}
@Override
public void showComplete() {
Toast.makeText(getApplicationContext(), "Complete", Toast.LENGTH_LONG).show();
}
@Override
public void setImageProfile(Bitmap profileBmp, int x) {
tempImageArray.put(x, profileBmp);
if(tempImageArray.size()==listName.size())
{
SortedSet<Integer> keys = new TreeSet<Integer>(tempImageArray.keySet());
for(Integer key : keys)
{
listImage.add(tempImageArray.get(key));
}
listView.setAdapter(new CustomAdapter(this, listName, listImage, listGroup, listContactId));
}
}
}
請幫助如何注入DetailScreenPresenter主持人在MainActivity。
非常感謝
您必須在適配器構造函數 – EpicPandaForce
中調用'component.inject(this);'添加到兩個答案中。你需要這樣的東西:DaggerMainScreenComponent.builder() .netComponent(((App)getApplicationContext())。getNetComponent()) .mainScreenModule(newNew MainScreenModule(this,new ContactDatabaseHelper(this))) .build()。注入(本);在適配器的構造函數中。 – dazza5000