why RecyclerView adapter not working with ButterKnife ?
It's working with findViewById but not working with ButterKnife
Adapter Class
public class CategoriesAdapter extends RecyclerView.Adapter<CategoriesAdapter.viewHolder>{
Context context;
List<CategoriesModels> categoriesModels;
public CategoriesAdapter(Context context,List<CategoriesModels> categoriesModels){
this.context = context;
this.categoriesModels = categoriesModels;
}
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_categories, viewGroup, false);
viewHolder viewHolder = new viewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull viewHolder viewHolder, int i) {
}
@Override
public int getItemCount() {
return categoriesModels.size();
}
class viewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.img_flag)
ImageView img_flag;
@BindView(R.id.txt_title)
TextView txt_title;
@BindView(R.id.img_correct)
ImageView img_correct;
public viewHolder(View view) {
super(view);
ButterKnife.bind(context,view);
}
}
}
MainActivity Class
public class CategoriesActivity extends AppCompatActivity {
@BindView(R.id.txt_select_language)
TextViewFont txt_select_language;
@BindView(R.id.rv_categories)
RecyclerView rv_categories;
List<CategoriesModels> categoriesModels = new ArrayList<>();
CategoriesAdapter categoriesAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categories);
ButterKnife.bind(CategoriesActivity.this);
//show data
ShowDate();
}
private void ShowDate() {
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://*****").addConverterFactory(GsonConverterFactory.create()).build();
RetrofitService retrofitService = retrofit.create(RetrofitService.class);
Call<List<CategoriesModels>> call = retrofitService.get_categories();
call.enqueue(new Callback<List<CategoriesModels>>() {
@Override
public void onResponse(Call<List<CategoriesModels>> call, Response<List<CategoriesModels>> response) {
List<CategoriesModels> list = response.body();
for (CategoriesModels i : list) {
categoriesModels.add(new CategoriesModels(i.getId(), i.getTitle(), i.getPhoto(), i.getShortcut()));
}
categoriesAdapter = new CategoriesAdapter(CategoriesActivity.this, categoriesModels);
rv_categories.setLayoutManager(new LinearLayoutManager(CategoriesActivity.this, LinearLayoutManager.VERTICAL, false));
rv_categories.setAdapter(categoriesAdapter);
}
@Override
public void onFailure(Call<List<CategoriesModels>> call, Throwable t) {
}
});
}
}
I tried a lot before putting up the subject but I did not find a solution yet....................................................................................
inside viewHolder constructor replace context to be this
ButterKnife.bind(this,view);
this refer to ViewHolder class which contain view you want to inject!\
Take a look to ButterKnife Examples. https://jakewharton.github.io/butterknife/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With